*/
protected $subfields = [];
+ /**
+ * @var callable
+ */
+ private $mergeCallback;
+
/**
* SearchIndexFieldDefinition constructor.
* @param string $name Field name
* @return SearchIndexField|false New definition or false if not mergeable.
*/
public function merge( SearchIndexField $that ) {
+ if ( !empty( $this->mergeCallback ) ) {
+ return call_user_func( $this->mergeCallback, $this, $that );
+ }
// TODO: which definitions may be compatible?
if ( ( $that instanceof self ) && $this->type === $that->type &&
$this->flags === $that->flags && $this->type !== self::INDEX_TYPE_NESTED
*/
abstract public function getMapping( SearchEngine $engine );
+ /**
+ * Set field-specific merge strategy.
+ * @param callable $callback
+ */
+ public function setMergeCallback( $callback ) {
+ $this->mergeCallback = $callback;
+ }
}
/**
* @dataProvider getMergeCases
+ * @param $t1
+ * @param $n1
+ * @param $t2
+ * @param $n2
+ * @param $result
*/
public function testMerge( $t1, $n1, $t2, $n2, $result ) {
- $field1 = $this->getMockBuilder( 'SearchIndexFieldDefinition' )
- ->setMethods( [ 'getMapping' ] )
- ->setConstructorArgs( [ $n1, $t1 ] )->getMock();
- $field2 = $this->getMockBuilder( 'SearchIndexFieldDefinition' )
- ->setMethods( [ 'getMapping' ] )
- ->setConstructorArgs( [ $n2, $t2 ] )->getMock();
+ $field1 =
+ $this->getMockBuilder( SearchIndexFieldDefinition::class )
+ ->setMethods( [ 'getMapping' ] )
+ ->setConstructorArgs( [ $n1, $t1 ] )
+ ->getMock();
+ $field2 =
+ $this->getMockBuilder( SearchIndexFieldDefinition::class )
+ ->setMethods( [ 'getMapping' ] )
+ ->setConstructorArgs( [ $n2, $t2 ] )
+ ->getMock();
if ( $result ) {
$this->assertNotFalse( $field1->merge( $field2 ) );
$field1->setFlag( 0xFF );
$this->assertFalse( $field1->merge( $field2 ) );
+
+ $field1->setMergeCallback(
+ function ( $this, $that ) {
+ return "test";
+ }
+ );
+ $this->assertEquals( "test", $field1->merge( $field2 ) );
+
}
+
}