|
1
|
|
|
<?php declare(strict_types = 1); |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Created by PhpStorm. |
|
5
|
|
|
* User: gordon |
|
6
|
|
|
* Date: 24/3/2561 |
|
7
|
|
|
* Time: 20:36 น. |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace Suilven\FreeTextSearch\Base; |
|
11
|
|
|
|
|
12
|
|
|
use SilverStripe\ORM\DataObject; |
|
13
|
|
|
use Suilven\FreeTextSearch\Indexes; |
|
14
|
|
|
|
|
15
|
|
|
abstract class Indexer implements \Suilven\FreeTextSearch\Interfaces\Indexer |
|
16
|
|
|
{ |
|
17
|
|
|
/** @var string */ |
|
18
|
|
|
protected $index; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Index a single data objecct |
|
22
|
|
|
* |
|
23
|
|
|
* @param \SilverStripe\ORM\DataObject $dataObject |
|
24
|
|
|
*/ |
|
25
|
|
|
abstract public function index(DataObject $dataObject): void; |
|
26
|
|
|
|
|
27
|
|
|
|
|
28
|
|
|
/** @param string $newIndex the new index name */ |
|
29
|
|
|
public function setIndex(string $newIndex): void |
|
30
|
|
|
{ |
|
31
|
|
|
$this->index = $newIndex; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Get the indexable fields for a given dataobject as an array |
|
37
|
|
|
* |
|
38
|
|
|
* @param \SilverStripe\ORM\DataObject $dataObject get the indexable fields for the provided data object |
|
39
|
|
|
* @return array<string> |
|
40
|
|
|
*/ |
|
41
|
|
|
protected function getFieldsToIndex(DataObject $dataObject): array |
|
42
|
|
|
{ |
|
43
|
|
|
$indexes = new Indexes(); |
|
44
|
|
|
$indices = $indexes->getIndexes(); |
|
45
|
|
|
|
|
46
|
|
|
$payload = []; |
|
47
|
|
|
|
|
48
|
|
|
/** @var \Suilven\FreeTextSearch\Index $indice */ |
|
49
|
|
|
foreach ($indices as $indice) { |
|
50
|
|
|
$indicePayload = []; |
|
51
|
|
|
|
|
52
|
|
|
$clazz = $indice->getClass(); |
|
53
|
|
|
$classes = $dataObject->getClassAncestry(); |
|
54
|
|
|
|
|
55
|
|
|
foreach ($classes as $indiceClass) { |
|
56
|
|
|
if ($indiceClass !== $clazz) { |
|
57
|
|
|
continue; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
$fields = $indice->getFields(); |
|
61
|
|
|
foreach ($fields as $field) { |
|
62
|
|
|
$value = $dataObject->$field; |
|
63
|
|
|
$indicePayload[$field] = $value; |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
$payload[$indice->getName()] = $indicePayload; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
return $payload; |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
|