Issues (31)

src/Helper/FieldHelper.php (1 issue)

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\DataObjectSchema;
13
use Suilven\FreeTextSearch\Index;
14
15
class FieldHelper
16
{
17
18
    /**
19
     * @param string|int|float|bool $fieldValue
20
     * @return float|int|string|bool
21
    */
22
    public function getFieldValueCorrectlyTyped(Index $index, string $fieldName, $fieldValue)
23
    {
24
        $tokens = $index->getTokens();
25
        $hasManyFields = $index->getHasManyFields();
26
        $hasManyKeys = \array_keys($hasManyFields);
27
        $hasOneFields = $index->getHasOneFields();
28
        $hasOneKeys = \array_keys($hasOneFields);
29
30
        $result = '';
31
        if (\in_array($fieldName, $tokens, true)) {
32
            $result = $this->getSingleValueAttributeCorrectlyTyped($index, $fieldName, $fieldValue);
33
        } elseif (\in_array($fieldName, $hasManyKeys, true)) {
34
            $details = $hasManyFields[$fieldName];
35
            $result = $this->getFieldValueCorrectlyTypedFor(
0 ignored issues
show
The assignment to $result is dead and can be removed.
Loading history...
36
                $details['class'],
37
                $details['field'],
38
                $fieldValue
39
            );
40
41
            $singleton = \singleton($details['class']);
42
            $objInContext = $singleton->get()->filter($details['field'], $fieldValue)->first();
43
44
            $result = $objInContext->ID;
45
        } elseif (\in_array($fieldName, $hasOneKeys, true)) {
46
            $result = \intval($fieldValue);
47
        }
48
49
        return $result;
50
    }
51
52
53
    /**
54
     * @param string|int|float|bool $fieldValue
55
     * @return float|int|string|bool
56
     */
57
    public function getSingleValueAttributeCorrectlyTyped(Index $index, string $fieldName, $fieldValue)
58
    {
59
        return $this->getFieldValueCorrectlyTypedFor($index->getClass(), $fieldName, $fieldValue);
60
    }
61
62
63
    /**
64
     * @param string|int|float|bool $fieldValue
65
     * @return float|int|string|bool
66
     */
67
    private function getFieldValueCorrectlyTypedFor(string $clazz, string $fieldName, $fieldValue)
68
    {
69
        $singleton = \singleton($clazz);
70
        $schema = $singleton->getSchema();
71
        $specs = $schema->fieldSpecs($clazz, DataObjectSchema::INCLUDE_CLASS);
72
73
        $type = $specs[$fieldName];
74
        $type = \explode('.', $type)[1];
75
76
77
        $value = (string) $fieldValue;
78
        switch ($type) {
79
            case 'Int':
80
                $value = \intval($fieldValue);
81
82
                break;
83
            case 'Float':
84
                $value = \floatval($fieldValue);
85
86
                break;
87
            case 'Boolean':
88
                $value = \boolval($fieldValue);
89
90
                break;
91
        }
92
93
        return $value;
94
    }
95
}
96