1 | <?php |
||
20 | class ConstraintUtils |
||
21 | { |
||
22 | |||
23 | /** |
||
24 | * @var array |
||
25 | */ |
||
26 | private $entities = []; |
||
27 | |||
28 | /** |
||
29 | * @var \stdClass |
||
30 | */ |
||
31 | private $currentSchema; |
||
32 | |||
33 | /** |
||
34 | * @var \stdClass |
||
35 | */ |
||
36 | private $currentData; |
||
37 | |||
38 | /** |
||
39 | * @var RequestStack |
||
40 | */ |
||
41 | private $requestStack; |
||
42 | |||
43 | /** |
||
44 | * Constructor. |
||
45 | * |
||
46 | * @param DocumentManager $dm DocumentManager |
||
47 | * @param RestUtils $restUtils RestUtils |
||
48 | * @param RequestStack $requestStack RequestStack |
||
49 | * |
||
50 | */ |
||
51 | public function __construct(DocumentManager $dm, RestUtils $restUtils, RequestStack $requestStack) |
||
52 | { |
||
53 | $this->dm = $dm; |
||
54 | $this->restUtils = $restUtils; |
||
55 | $this->requestStack = $requestStack; |
||
56 | } |
||
57 | |||
58 | /** |
||
59 | * Gets a entity from the database as a generic object. All constraints that need the saved data to compare |
||
60 | * values or anything should call this function to get what they need. As this is cached in the instance, |
||
61 | * it will fetched only once even if multiple constraints need that object. |
||
62 | * |
||
63 | * @param string $documentClass document class |
||
64 | * @param string $recordId record id |
||
65 | * |
||
66 | * @throws \Doctrine\ODM\MongoDB\LockException |
||
67 | * @throws \Exception |
||
68 | * |
||
69 | * @return object|null entity |
||
70 | */ |
||
71 | public function getSerializedEntity($documentClass, $recordId) |
||
72 | { |
||
73 | if (!isset($this->entities[$documentClass][$recordId])) { |
||
74 | $current = $this->dm->getRepository($documentClass)->find($recordId); |
||
75 | |||
76 | if (is_null($current)) { |
||
77 | $this->entities[$documentClass][$recordId] = null; |
||
78 | } else { |
||
79 | $this->entities[$documentClass][$recordId] = json_decode($this->restUtils->serializeContent($current)); |
||
80 | } |
||
81 | } |
||
82 | |||
83 | return $this->entities[$documentClass][$recordId]; |
||
84 | } |
||
85 | |||
86 | /** |
||
87 | * Returns the current request entity (as \stdClass) if possible |
||
88 | * |
||
89 | * @return null|object |
||
90 | */ |
||
91 | public function getCurrentEntity() |
||
92 | { |
||
93 | $currentRecordId = null; |
||
94 | |||
95 | // first, let's the one from the payload.. |
||
96 | if (isset($this->currentData->id)) { |
||
97 | $currentRecordId = $this->currentData->id; |
||
98 | } |
||
99 | |||
100 | // if we have a request, it must override it.. |
||
101 | if ($this->requestStack->getCurrentRequest() instanceof Request && |
||
102 | $this->requestStack->getCurrentRequest()->attributes->has('id') |
||
103 | ) { |
||
104 | $currentRecordId = $this->requestStack->getCurrentRequest()->attributes->get('id'); |
||
105 | } |
||
106 | |||
107 | if (isset($this->currentSchema->{'x-documentClass'}) && |
||
108 | !empty($this->currentSchema->{'x-documentClass'}) && |
||
109 | !is_null($currentRecordId) |
||
110 | ) { |
||
111 | return $this->getSerializedEntity($this->currentSchema->{'x-documentClass'}, $currentRecordId); |
||
112 | } |
||
113 | |||
114 | return null; |
||
115 | } |
||
116 | |||
117 | /** |
||
118 | * gets the current schema. helpful for field schema validators that need access to the whole schema in some way. |
||
119 | * |
||
120 | * @return \stdClass |
||
121 | */ |
||
122 | public function getCurrentSchema() |
||
123 | { |
||
124 | return $this->currentSchema; |
||
125 | } |
||
126 | |||
127 | /** |
||
128 | * gets the current data from the client (the whole object). |
||
129 | * helpful for field schema validators that need access to the whole data in some way. |
||
130 | * |
||
131 | * @return \stdClass |
||
132 | */ |
||
133 | public function getCurrentData() |
||
134 | { |
||
135 | return $this->currentData; |
||
136 | } |
||
137 | |||
138 | /** |
||
139 | * own function to get standard path from a JsonPointer object |
||
140 | * |
||
141 | * @param JsonPointer|null $pointer pointer |
||
142 | * |
||
143 | * @return string path as string |
||
144 | */ |
||
145 | public function getNormalizedPathFromPointer(JsonPointer $pointer = null) |
||
155 | |||
156 | /** |
||
157 | * called on the first schema validation, before anything else. |
||
158 | * |
||
159 | * @param ConstraintEventSchema $event event |
||
160 | * |
||
161 | * @return void |
||
162 | */ |
||
163 | public function onSchemaValidation(ConstraintEventSchema $event) |
||
168 | } |
||
169 |
If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe: