Passed
Push — master ( 76df74...7115c3 )
by Gordon
02:05
created

SearchHelper   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
eloc 19
c 1
b 0
f 0
dl 0
loc 39
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A getTextFieldPayload() 0 33 6
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\ORM\DataObject;
13
14
class SearchHelper
15
{
16
    /**
17
     * @param \SilverStripe\ORM\DataObject $dataObject the dataobject to extra text from
18
     * @return array<string,array<string,string>>
19
     */
20
    public function getTextFieldPayload(DataObject $dataObject): array
21
    {
22
        $helper = new IndexingHelper();
23
        $fullPayload = $helper->getFieldsToIndex($dataObject);
24
25
        $textPayload = [];
26
27
        $keys = \array_keys($fullPayload);
28
        $specsHelper = new SpecsHelper();
29
30
        foreach ($keys as $key) {
31
            if ($fullPayload[$key] === []) {
32
                continue;
33
            }
34
35
            $textPayload[$key] = [];
36
            $specs = $specsHelper->getFieldSpecs($key);
37
38
            foreach (\array_keys($specs) as $field) {
39
                // skip link field
40
                if ($field === 'Link') {
41
                    continue;
42
                }
43
                $type = $specs[$field];
44
                if (!\in_array($type, ['Varchar', 'HTMLText'], true)) {
45
                    continue;
46
                }
47
48
                $textPayload[$key][$field] = (string) $fullPayload[$key][$field];
49
            }
50
        }
51
52
        return $textPayload;
53
    }
54
}
55