|
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
|
|
|
|
|
28
|
|
|
$result = ''; |
|
29
|
|
|
if (\in_array($fieldName, $tokens, true)) { |
|
30
|
|
|
$result = $this->getSingleValueAttributeCorrectlyTyped($index, $fieldName, $fieldValue); |
|
31
|
|
|
} |
|
32
|
|
|
// @todo Has one |
|
33
|
|
|
if (\in_array($fieldName, $hasManyKeys, true)) { |
|
34
|
|
|
$details = $hasManyFields[$fieldName]; |
|
35
|
|
|
$result = $this->getFieldValueCorrectlyTypedFor( |
|
|
|
|
|
|
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
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
return $result; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @param string|int|float|bool $fieldValue |
|
53
|
|
|
* @return float|int|string|bool |
|
54
|
|
|
*/ |
|
55
|
|
|
public function getSingleValueAttributeCorrectlyTyped(Index $index, string $fieldName, $fieldValue) |
|
56
|
|
|
{ |
|
57
|
|
|
return $this->getFieldValueCorrectlyTypedFor($index->getClass(), $fieldName, $fieldValue); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @param string|int|float|bool $fieldValue |
|
63
|
|
|
* @return float|int|string|bool |
|
64
|
|
|
*/ |
|
65
|
|
|
private function getFieldValueCorrectlyTypedFor(string $clazz, string $fieldName, $fieldValue) |
|
66
|
|
|
{ |
|
67
|
|
|
$singleton = \singleton($clazz); |
|
68
|
|
|
$schema = $singleton->getSchema(); |
|
69
|
|
|
$specs = $schema->fieldSpecs($clazz, DataObjectSchema::INCLUDE_CLASS); |
|
70
|
|
|
|
|
71
|
|
|
$type = $specs[$fieldName]; |
|
72
|
|
|
$type = \explode('.', $type)[1]; |
|
73
|
|
|
|
|
74
|
|
|
|
|
75
|
|
|
$value = (string) $fieldValue; |
|
76
|
|
|
switch ($type) { |
|
77
|
|
|
case 'Int': |
|
78
|
|
|
$value = \intval($fieldValue); |
|
79
|
|
|
|
|
80
|
|
|
break; |
|
81
|
|
|
case 'Float': |
|
82
|
|
|
$value = \floatval($fieldValue); |
|
83
|
|
|
|
|
84
|
|
|
break; |
|
85
|
|
|
case 'Boolean': |
|
86
|
|
|
$value = \boolval($fieldValue); |
|
87
|
|
|
|
|
88
|
|
|
break; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
return $value; |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|