4 * Basic infrastructure of the field definition.
6 * Specific engines should extend this class and at at least,
7 * override the getMapping method, but can reuse other parts.
11 abstract class SearchIndexFieldDefinition
implements SearchIndexField
{
19 * Type of the field, one of the constants above
25 * Bit flags for the field.
32 * @var SearchIndexFieldDefinition[]
34 protected $subfields = [];
39 private $mergeCallback;
42 * SearchIndexFieldDefinition constructor.
43 * @param string $name Field name
44 * @param int $type Index type
46 public function __construct( $name, $type ) {
55 public function getName() {
63 public function getIndexType() {
68 * Set global flag for this field.
70 * @param int $flag Bit flag to set/unset
71 * @param bool $unset True if flag should be unset, false by default
74 public function setFlag( $flag, $unset = false ) {
76 $this->flags
&= ~
$flag;
78 $this->flags |
= $flag;
84 * Check if flag is set.
86 * @return int 0 if unset, !=0 if set
88 public function checkFlag( $flag ) {
89 return $this->flags
& $flag;
93 * Merge two field definitions if possible.
95 * @param SearchIndexField $that
96 * @return SearchIndexField|false New definition or false if not mergeable.
98 public function merge( SearchIndexField
$that ) {
99 if ( !empty( $this->mergeCallback
) ) {
100 return call_user_func( $this->mergeCallback
, $this, $that );
102 // TODO: which definitions may be compatible?
103 if ( ( $that instanceof self
) && $this->type
=== $that->type
&&
104 $this->flags
=== $that->flags
&& $this->type
!== self
::INDEX_TYPE_NESTED
113 * @return SearchIndexFieldDefinition[]
115 public function getSubfields() {
116 return $this->subfields
;
121 * @param SearchIndexFieldDefinition[] $subfields
124 public function setSubfields( array $subfields ) {
125 $this->subfields
= $subfields;
130 * @param SearchEngine $engine
134 abstract public function getMapping( SearchEngine
$engine );
137 * Set field-specific merge strategy.
138 * @param callable $callback
140 public function setMergeCallback( $callback ) {
141 $this->mergeCallback
= $callback;