Merge "Improve documentation of constants throughout the codebase"
[lhc/web/wiklou.git] / includes / block / Restriction / AbstractRestriction.php
1 <?php
2 /**
3 * Abstract block restriction.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 */
22
23 namespace MediaWiki\Block\Restriction;
24
25 abstract class AbstractRestriction implements Restriction {
26
27 /**
28 * String constant identifying the type of restriction. Expected to be overriden in subclasses
29 * with a non-empty string value.
30 */
31 const TYPE = '';
32
33 /**
34 * Numeric type identifier. Expected to be overriden in subclasses with a non-zero integer
35 * number. Must not exceed 127 to fit into a TINYINT database field.
36 */
37 const TYPE_ID = 0;
38
39 /**
40 * @var int
41 */
42 protected $blockId;
43
44 /**
45 * @var int
46 */
47 protected $value;
48
49 /**
50 * Create Restriction.
51 *
52 * @since 1.33
53 * @param int $blockId
54 * @param int $value
55 */
56 public function __construct( $blockId, $value ) {
57 $this->blockId = (int)$blockId;
58 $this->value = (int)$value;
59 }
60
61 /**
62 * @inheritDoc
63 */
64 public static function getType() {
65 return static::TYPE;
66 }
67
68 /**
69 * @inheritDoc
70 */
71 public static function getTypeId() {
72 return static::TYPE_ID;
73 }
74
75 /**
76 * @inheritDoc
77 */
78 public function getBlockId() {
79 return $this->blockId;
80 }
81
82 /**
83 * @inheritDoc
84 */
85 public function setBlockId( $blockId ) {
86 $this->blockId = (int)$blockId;
87
88 return $this;
89 }
90
91 /**
92 * @inheritDoc
93 */
94 public function getValue() {
95 return $this->value;
96 }
97
98 /**
99 * @inheritDoc
100 */
101 public static function newFromRow( \stdClass $row ) {
102 // @phan-suppress-next-line PhanTypeInstantiateAbstract
103 return new static( $row->ir_ipb_id, $row->ir_value );
104 }
105
106 /**
107 * @inheritDoc
108 */
109 public function toRow() {
110 return [
111 'ir_ipb_id' => $this->getBlockId(),
112 'ir_type' => $this->getTypeId(),
113 'ir_value' => $this->getValue(),
114 ];
115 }
116
117 /**
118 * @inheritDoc
119 */
120 public function equals( Restriction $other ) {
121 return $this->getHash() === $other->getHash();
122 }
123
124 /**
125 * @inheritDoc
126 */
127 public function getHash() {
128 return $this->getType() . '-' . $this->getValue();
129 }
130 }