ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
PropPatch.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Sabre\DAV;
4 
6 
20 class PropPatch {
21 
30  protected $mutations;
31 
38  protected $result = [];
39 
45  protected $propertyUpdateCallbacks = [];
46 
52  protected $failed = false;
53 
59  function __construct(array $mutations) {
60 
61  $this->mutations = $mutations;
62 
63  }
64 
87  function handle($properties, callable $callback) {
88 
89  $usedProperties = [];
90  foreach ((array)$properties as $propertyName) {
91 
92  if (array_key_exists($propertyName, $this->mutations) && !isset($this->result[$propertyName])) {
93 
94  $usedProperties[] = $propertyName;
95  // HTTP Accepted
96  $this->result[$propertyName] = 202;
97  }
98 
99  }
100 
101  // Only registering if there's any unhandled properties.
102  if (!$usedProperties) {
103  return;
104  }
105  $this->propertyUpdateCallbacks[] = [
106  // If the original argument to this method was a string, we need
107  // to also make sure that it stays that way, so the commit function
108  // knows how to format the arguments to the callback.
109  is_string($properties) ? $properties : $usedProperties,
110  $callback
111  ];
112 
113  }
114 
123  function handleRemaining(callable $callback) {
124 
125  $properties = $this->getRemainingMutations();
126  if (!$properties) {
127  // Nothing to do, don't register callback
128  return;
129  }
130 
131  foreach ($properties as $propertyName) {
132  // HTTP Accepted
133  $this->result[$propertyName] = 202;
134 
135  $this->propertyUpdateCallbacks[] = [
136  $properties,
137  $callback
138  ];
139  }
140 
141  }
142 
150  function setResultCode($properties, $resultCode) {
151 
152  foreach ((array)$properties as $propertyName) {
153  $this->result[$propertyName] = $resultCode;
154  }
155 
156  if ($resultCode >= 400) {
157  $this->failed = true;
158  }
159 
160  }
161 
168  function setRemainingResultCode($resultCode) {
169 
170  $this->setResultCode(
171  $this->getRemainingMutations(),
172  $resultCode
173  );
174 
175  }
176 
185 
186  $remaining = [];
187  foreach ($this->mutations as $propertyName => $propValue) {
188  if (!isset($this->result[$propertyName])) {
189  $remaining[] = $propertyName;
190  }
191  }
192 
193  return $remaining;
194 
195  }
196 
204  function getRemainingValues() {
205 
206  $remaining = [];
207  foreach ($this->mutations as $propertyName => $propValue) {
208  if (!isset($this->result[$propertyName])) {
209  $remaining[$propertyName] = $propValue;
210  }
211  }
212 
213  return $remaining;
214 
215  }
216 
225  function commit() {
226 
227  // First we validate if every property has a handler
228  foreach ($this->mutations as $propertyName => $value) {
229 
230  if (!isset($this->result[$propertyName])) {
231  $this->failed = true;
232  $this->result[$propertyName] = 403;
233  }
234 
235  }
236 
237  foreach ($this->propertyUpdateCallbacks as $callbackInfo) {
238 
239  if ($this->failed) {
240  break;
241  }
242  if (is_string($callbackInfo[0])) {
243  $this->doCallbackSingleProp($callbackInfo[0], $callbackInfo[1]);
244  } else {
245  $this->doCallbackMultiProp($callbackInfo[0], $callbackInfo[1]);
246  }
247 
248  }
249 
254  if ($this->failed) {
255 
256  foreach ($this->result as $propertyName => $status) {
257  if ($status === 202) {
258  // Failed dependency
259  $this->result[$propertyName] = 424;
260  }
261  }
262 
263  }
264 
265  return !$this->failed;
266 
267  }
268 
276  private function doCallBackSingleProp($propertyName, callable $callback) {
277 
278  $result = $callback($this->mutations[$propertyName]);
279  if (is_bool($result)) {
280  if ($result) {
281  if (is_null($this->mutations[$propertyName])) {
282  // Delete
283  $result = 204;
284  } else {
285  // Update
286  $result = 200;
287  }
288  } else {
289  // Fail
290  $result = 403;
291  }
292  }
293  if (!is_int($result)) {
294  throw new UnexpectedValueException('A callback sent to handle() did not return an int or a bool');
295  }
296  $this->result[$propertyName] = $result;
297  if ($result >= 400) {
298  $this->failed = true;
299  }
300 
301  }
302 
310  private function doCallBackMultiProp(array $propertyList, callable $callback) {
311 
312  $argument = [];
313  foreach ($propertyList as $propertyName) {
314  $argument[$propertyName] = $this->mutations[$propertyName];
315  }
316 
317  $result = $callback($argument);
318 
319  if (is_array($result)) {
320  foreach ($propertyList as $propertyName) {
321  if (!isset($result[$propertyName])) {
322  $resultCode = 500;
323  } else {
324  $resultCode = $result[$propertyName];
325  }
326  if ($resultCode >= 400) {
327  $this->failed = true;
328  }
329  $this->result[$propertyName] = $resultCode;
330 
331  }
332  } elseif ($result === true) {
333 
334  // Success
335  foreach ($argument as $propertyName => $propertyValue) {
336  $this->result[$propertyName] = is_null($propertyValue) ? 204 : 200;
337  }
338 
339  } elseif ($result === false) {
340  // Fail :(
341  $this->failed = true;
342  foreach ($propertyList as $propertyName) {
343  $this->result[$propertyName] = 403;
344  }
345  } else {
346  throw new UnexpectedValueException('A callback sent to handle() did not return an array or a bool');
347  }
348 
349  }
350 
356  function getResult() {
357 
358  return $this->result;
359 
360  }
361 
367  function getMutations() {
368 
369  return $this->mutations;
370 
371  }
372 
373 }
doCallBackSingleProp($propertyName, callable $callback)
Executes a property callback with the single-property syntax.
Definition: PropPatch.php:276
This class represents a set of properties that are going to be updated.
Definition: PropPatch.php:20
if($state['core:TerminatedAssocId'] !==null) $remaining
setRemainingResultCode($resultCode)
Sets the result code for all properties that did not have a result yet.
Definition: PropPatch.php:168
doCallBackMultiProp(array $propertyList, callable $callback)
Executes a property callback with the multi-property syntax.
Definition: PropPatch.php:310
setResultCode($properties, $resultCode)
Sets the result code for one or more properties.
Definition: PropPatch.php:150
handle($properties, callable $callback)
Call this function if you wish to handle updating certain properties.
Definition: PropPatch.php:87
getRemainingMutations()
Returns the list of properties that don&#39;t have a result code yet.
Definition: PropPatch.php:184
getResult()
Returns the result of the operation.
Definition: PropPatch.php:356
getMutations()
Returns the full list of mutations.
Definition: PropPatch.php:367
handleRemaining(callable $callback)
Call this function if you wish to handle all properties that haven&#39;t been handled by anything else ye...
Definition: PropPatch.php:123
getRemainingValues()
Returns the list of properties that don&#39;t have a result code yet.
Definition: PropPatch.php:204
commit()
Performs the actual update, and calls all callbacks.
Definition: PropPatch.php:225
__construct(array $mutations)
Constructor.
Definition: PropPatch.php:59