|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Jabe\Engine\Impl\Json; |
|
4
|
|
|
|
|
5
|
|
|
use Jabe\Engine\Impl\{ |
|
6
|
|
|
QueryEntityRelationCondition, |
|
7
|
|
|
QueryPropertyImpl |
|
8
|
|
|
}; |
|
9
|
|
|
use Jabe\Engine\Impl\Util\JsonUtil; |
|
10
|
|
|
|
|
11
|
|
|
class JsonQueryFilteringPropertyConverter extends JsonObjectConverter |
|
12
|
|
|
{ |
|
13
|
|
|
protected static $INSTANCE;// = new JsonQueryFilteringPropertyConverter(); |
|
14
|
|
|
|
|
15
|
|
|
protected static $ARRAY_CONVERTER;// = new JsonArrayOfObjectsConverter<>(INSTANCE); |
|
16
|
|
|
|
|
17
|
|
|
public const BASE_PROPERTY = "baseField"; |
|
18
|
|
|
public const COMPARISON_PROPERTY = "comparisonField"; |
|
19
|
|
|
public const SCALAR_VALUE = "value"; |
|
20
|
|
|
|
|
21
|
|
|
public static function instance(): JsonQueryFilteringPropertyConverter |
|
22
|
|
|
{ |
|
23
|
|
|
if (self::$INSTANCE === null) { |
|
24
|
|
|
self::$INSTANCE = new JsonQueryFilteringPropertyConverter(); |
|
25
|
|
|
} |
|
26
|
|
|
return self::$INSTANCE; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
public static function arrayConverter(): JsonArrayOfObjectsConverter |
|
30
|
|
|
{ |
|
31
|
|
|
if (self::$ARRAY_CONVERTER === null) { |
|
32
|
|
|
self::$ARRAY_CONVERTER = new JsonArrayOfObjectsConverter(self::instance()); |
|
33
|
|
|
} |
|
34
|
|
|
return self::$ARRAY_CONVERTER; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
public function toJsonObject(/*QueryEntityRelationCondition*/$filteringProperty, bool $isOrQueryActive = false): ?\stdClass |
|
38
|
|
|
{ |
|
39
|
|
|
$jsonObject = JsonUtil::createObject(); |
|
40
|
|
|
|
|
41
|
|
|
JsonUtil::addField($jsonObject, self::BASE_PROPERTY, $filteringProperty->getProperty()->getName()); |
|
42
|
|
|
|
|
43
|
|
|
$comparisonProperty = $filteringProperty->getComparisonProperty(); |
|
44
|
|
|
if ($comparisonProperty !== null) { |
|
45
|
|
|
JsonUtil::addField($jsonObject, self::COMPARISON_PROPERTY, $comparisonProperty->getName()); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
$scalarValue = $filteringProperty->getScalarValue(); |
|
49
|
|
|
if ($scalarValue !== null) { |
|
50
|
|
|
JsonUtil::addFieldRawValue($jsonObject, self::SCALAR_VALUE, $scalarValue); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
return $jsonObject; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
public function toObject(\stdClass $jsonObject, bool $isOrQuery = false) |
|
57
|
|
|
{ |
|
58
|
|
|
// this is limited in that it allows only String values; |
|
59
|
|
|
// that is sufficient for current use case with task filters |
|
60
|
|
|
// but could be extended by a data type in the future |
|
61
|
|
|
$scalarValue = null; |
|
62
|
|
|
if (property_exists($jsonObject, self::SCALAR_VALUE)) { |
|
63
|
|
|
$scalarValue = JsonUtil::getString($jsonObject, self::SCALAR_VALUE); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
$baseProperty = null; |
|
67
|
|
|
if (property_exists($jsonObject, self::BASE_PROPERTY)) { |
|
68
|
|
|
$baseProperty = new QueryPropertyImpl(JsonUtil::getString($jsonObject, self::BASE_PROPERTY)); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
$comparisonProperty = null; |
|
72
|
|
|
if (property_exists($jsonObject, self::COMPARISON_PROPERTY)) { |
|
73
|
|
|
$comparisonProperty = new QueryPropertyImpl(JsonUtil::getString($jsonObject, self::COMPARISON_PROPERTY)); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
return new QueryEntityRelationCondition($baseProperty, $comparisonProperty, $scalarValue); |
|
|
|
|
|
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|