Change @inheritdoc to @inheritDoc
[lhc/web/wiklou.git] / includes / search / SearchIndexFieldDefinition.php
1 <?php
2
3 /**
4 * Basic infrastructure of the field definition.
5 *
6 * Specific engines should extend this class and at at least,
7 * override the getMapping method, but can reuse other parts.
8 *
9 * @since 1.28
10 */
11 abstract class SearchIndexFieldDefinition implements SearchIndexField {
12 /**
13 * Name of the field
14 *
15 * @var string
16 */
17 protected $name;
18 /**
19 * Type of the field, one of the constants above
20 *
21 * @var int
22 */
23 protected $type;
24 /**
25 * Bit flags for the field.
26 *
27 * @var int
28 */
29 protected $flags = 0;
30 /**
31 * Subfields
32 * @var SearchIndexFieldDefinition[]
33 */
34 protected $subfields = [];
35
36 /**
37 * @var callable
38 */
39 private $mergeCallback;
40
41 /**
42 * @param string $name Field name
43 * @param int $type Index type
44 */
45 public function __construct( $name, $type ) {
46 $this->name = $name;
47 $this->type = $type;
48 }
49
50 /**
51 * Get field name
52 * @return string
53 */
54 public function getName() {
55 return $this->name;
56 }
57
58 /**
59 * Get index type
60 * @return int
61 */
62 public function getIndexType() {
63 return $this->type;
64 }
65
66 /**
67 * Set global flag for this field.
68 *
69 * @param int $flag Bit flag to set/unset
70 * @param bool $unset True if flag should be unset, false by default
71 * @return $this
72 */
73 public function setFlag( $flag, $unset = false ) {
74 if ( $unset ) {
75 $this->flags &= ~$flag;
76 } else {
77 $this->flags |= $flag;
78 }
79 return $this;
80 }
81
82 /**
83 * Check if flag is set.
84 * @param $flag
85 * @return int 0 if unset, !=0 if set
86 */
87 public function checkFlag( $flag ) {
88 return $this->flags & $flag;
89 }
90
91 /**
92 * Merge two field definitions if possible.
93 *
94 * @param SearchIndexField $that
95 * @return SearchIndexField|false New definition or false if not mergeable.
96 */
97 public function merge( SearchIndexField $that ) {
98 if ( !empty( $this->mergeCallback ) ) {
99 return call_user_func( $this->mergeCallback, $this, $that );
100 }
101 // TODO: which definitions may be compatible?
102 if ( ( $that instanceof self ) && $this->type === $that->type &&
103 $this->flags === $that->flags && $this->type !== self::INDEX_TYPE_NESTED
104 ) {
105 return $that;
106 }
107 return false;
108 }
109
110 /**
111 * Get subfields
112 * @return SearchIndexFieldDefinition[]
113 */
114 public function getSubfields() {
115 return $this->subfields;
116 }
117
118 /**
119 * Set subfields
120 * @param SearchIndexFieldDefinition[] $subfields
121 * @return $this
122 */
123 public function setSubfields( array $subfields ) {
124 $this->subfields = $subfields;
125 return $this;
126 }
127
128 /**
129 * @param SearchEngine $engine
130 *
131 * @return array
132 */
133 abstract public function getMapping( SearchEngine $engine );
134
135 /**
136 * Set field-specific merge strategy.
137 * @param callable $callback
138 */
139 public function setMergeCallback( $callback ) {
140 $this->mergeCallback = $callback;
141 }
142
143 /**
144 * @inheritDoc
145 */
146 public function getEngineHints( SearchEngine $engine ) {
147 return [];
148 }
149 }