Passed
Pull Request — master (#13)
by Gordon
01:41
created

IndexingHelper::getFieldsToIndex()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 31
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 16
c 1
b 0
f 0
dl 0
loc 31
rs 9.4222
cc 5
nc 5
nop 1
1
<?php declare(strict_types = 1);
2
3
/**
4
 * Created by PhpStorm.
5
 * User: gordon
6
 * Date: 25/3/2561
7
 * Time: 17:01 น.
8
 */
9
10
namespace Suilven\FreeTextSearch\Helper;
11
12
use SilverStripe\Core\Injector\Injector;
13
use SilverStripe\ORM\DataObject;
14
use Suilven\FreeTextSearch\Indexes;
15
use Suilven\FreeTextSearch\Interfaces\Indexer;
16
17
class IndexingHelper
18
{
19
    /**
20
     * Get the indexable fields for a given dataobject as an array
21
     *
22
     * @param \SilverStripe\ORM\DataObject $dataObject get the indexable fields for the provided data object
23
     * @return array<string>
24
     */
25
    public function getFieldsToIndex(DataObject $dataObject): array
26
    {
27
        $indexes = new Indexes();
28
        $indices = $indexes->getIndexes();
29
30
        $payload = [];
31
32
        /** @var \Suilven\FreeTextSearch\Index $indice */
33
        foreach ($indices as $indice) {
34
            $indicePayload = [];
35
36
            $clazz = $indice->getClass();
37
            $classes = $dataObject->getClassAncestry();
38
39
            foreach ($classes as $indiceClass) {
40
                if ($indiceClass !== $clazz) {
41
                    continue;
42
                }
43
44
                $fields = $indice->getFields();
45
                foreach ($fields as $field) {
46
                    $value = $dataObject->$field;
47
                    $indicePayload[$field] = $value;
48
                }
49
            }
50
            $payload[$indice->getName()] = $indicePayload;
51
        }
52
53
        // @todo a possible mutator, specific to different search engines, may be required here
54
55
        return $payload;
56
    }
57
}
58