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