Debian Codegraph @

includes/search/SearchIndexFieldDefinition.php

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