|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Jabe\Engine\Impl\Json; |
|
4
|
|
|
|
|
5
|
|
|
use Jabe\Engine\Impl\{ |
|
6
|
|
|
Direction, |
|
7
|
|
|
QueryEntityRelationCondition, |
|
8
|
|
|
QueryOrderingProperty, |
|
9
|
|
|
VariableOrderProperty |
|
10
|
|
|
}; |
|
11
|
|
|
use Jabe\Engine\Impl\Util\JsonUtil; |
|
12
|
|
|
|
|
13
|
|
|
class JsonQueryOrderingPropertyConverter extends JsonObjectConverter |
|
14
|
|
|
{ |
|
15
|
|
|
|
|
16
|
|
|
protected static $INSTANCE; |
|
17
|
|
|
|
|
18
|
|
|
protected static $ARRAY_CONVERTER; |
|
19
|
|
|
|
|
20
|
|
|
public const RELATION = "relation"; |
|
21
|
|
|
public const QUERY_PROPERTY = "queryProperty"; |
|
22
|
|
|
public const QUERY_PROPERTY_FUNCTION = "queryPropertyFunction"; |
|
23
|
|
|
public const DIRECTION = "direction"; |
|
24
|
|
|
public const RELATION_CONDITIONS = "relationProperties"; |
|
25
|
|
|
|
|
26
|
|
|
public static function instance(): JsonQueryOrderingPropertyConverter |
|
27
|
|
|
{ |
|
28
|
|
|
if (self::$INSTANCE === null) { |
|
29
|
|
|
self::$INSTANCE = new JsonQueryOrderingPropertyConverter(); |
|
30
|
|
|
} |
|
31
|
|
|
return self::$INSTANCE; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
public static function arrayConverter(): JsonArrayOfObjectsConverter |
|
35
|
|
|
{ |
|
36
|
|
|
if (self::$ARRAY_CONVERTER === null) { |
|
37
|
|
|
self::$ARRAY_CONVERTER = new JsonArrayOfObjectsConverter(self::instance()); |
|
38
|
|
|
} |
|
39
|
|
|
return self::$ARRAY_CONVERTER; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
public function toJsonObject(/*QueryOrderingProperty*/$property, bool $isOrQueryActive = false): ?\stdClass |
|
43
|
|
|
{ |
|
44
|
|
|
$jsonObject = JsonUtil::createObject(); |
|
45
|
|
|
|
|
46
|
|
|
JsonUtil::addField($jsonObject, self::RELATION, $property->getRelation()); |
|
47
|
|
|
|
|
48
|
|
|
$queryProperty = $property->getQueryProperty(); |
|
49
|
|
|
if ($queryProperty !== null) { |
|
50
|
|
|
JsonUtil::addField($jsonObject, self::QUERY_PROPERTY, $queryProperty->getName()); |
|
51
|
|
|
JsonUtil::addField($jsonObject, self::QUERY_PROPERTY_FUNCTION, $queryProperty->getFunction()); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
$direction = $property->getDirection(); |
|
55
|
|
|
if ($direction !== null) { |
|
56
|
|
|
JsonUtil::addField($jsonObject, self::DIRECTION, $direction->getName()); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
if ($property->hasRelationConditions()) { |
|
60
|
|
|
$relationConditionsJson = JsonQueryFilteringPropertyConverter::arrayConverter() |
|
61
|
|
|
->toJsonArray($property->getRelationConditions()); |
|
62
|
|
|
JsonUtil::addField($jsonObject, self::RELATION_CONDITIONS, $relationConditionsJson); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
return $jsonObject; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
public function toObject(\stdClass $jsonObject, bool $isOrQuery = false) |
|
69
|
|
|
{ |
|
70
|
|
|
$relation = null; |
|
71
|
|
|
if (property_exists($jsonObject, self::RELATION)) { |
|
72
|
|
|
$relation = JsonUtil::getString($jsonObject, self::RELATION); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
$property = null; |
|
76
|
|
|
if (QueryOrderingProperty::RELATION_VARIABLE == $relation) { |
|
77
|
|
|
$property = new VariableOrderProperty(); |
|
78
|
|
|
} else { |
|
79
|
|
|
$property = new QueryOrderingProperty(); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
$property->setRelation($relation); |
|
|
|
|
|
|
83
|
|
|
|
|
84
|
|
|
if (property_exists($jsonObject, self::QUERY_PROPERTY)) { |
|
85
|
|
|
$propertyName = JsonUtil::getString($jsonObject, self::QUERY_PROPERTY); |
|
86
|
|
|
$propertyFunction = null; |
|
87
|
|
|
if (property_exists($jsonObject, self::QUERY_PROPERTY_FUNCTION)) { |
|
88
|
|
|
$propertyFunction = JsonUtil::getString($jsonObject, self::QUERY_PROPERTY_FUNCTION); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
$queryProperty = new QueryPropertyImpl($propertyName, $propertyFunction); |
|
92
|
|
|
$property->setQueryProperty($queryProperty); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
if (property_exists($jsonObject, self::DIRECTION)) { |
|
96
|
|
|
$direction = JsonUtil::getString($jsonObject, self::DIRECTION); |
|
97
|
|
|
$property->setDirection(Direction::findByName($direction)); |
|
|
|
|
|
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
if (property_exists($jsonObject, self::RELATION_CONDITIONS)) { |
|
101
|
|
|
$relationConditions = |
|
102
|
|
|
JsonQueryFilteringPropertyConverter::arrayConverter()->toObject(JsonUtil::getArray($jsonObject, self::RELATION_CONDITIONS)); |
|
103
|
|
|
$property->setRelationConditions($relationConditions); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
return $property; |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|