|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Cypress\PatchManager\Request; |
|
4
|
|
|
|
|
5
|
|
|
use Cypress\PatchManager\Exception\InvalidJsonRequestContent; |
|
6
|
|
|
use Cypress\PatchManager\Exception\MissingOperationNameRequest; |
|
7
|
|
|
use Cypress\PatchManager\Exception\MissingOperationRequest; |
|
8
|
|
|
use Cypress\PatchManager\Request\Adapter; |
|
9
|
|
|
use PhpCollection\Sequence; |
|
10
|
|
|
|
|
11
|
|
|
class Operations |
|
12
|
|
|
{ |
|
13
|
|
|
const OP_KEY_NAME = 'op'; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @var Adapter |
|
17
|
|
|
*/ |
|
18
|
|
|
private $adapter; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @param Adapter $adapter |
|
22
|
|
|
*/ |
|
23
|
|
|
public function __construct(Adapter $adapter) |
|
24
|
|
|
{ |
|
25
|
|
|
$this->adapter = $adapter; |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* directly from stack overflow: http://stackoverflow.com/a/6041773 |
|
30
|
|
|
* check if a string is valid json, and returns the parsed content |
|
31
|
|
|
* |
|
32
|
|
|
* @param string $string |
|
33
|
|
|
* |
|
34
|
|
|
* @throws InvalidJsonRequestContent |
|
35
|
|
|
* @return array |
|
36
|
|
|
*/ |
|
37
|
|
|
private function parseJson($string) |
|
38
|
|
|
{ |
|
39
|
|
|
$parsedContent = json_decode($string, true); |
|
40
|
|
|
if (JSON_ERROR_NONE !== json_last_error()) { |
|
41
|
|
|
throw new InvalidJsonRequestContent; |
|
42
|
|
|
} |
|
43
|
|
|
return $parsedContent; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @param array $arr |
|
48
|
|
|
* @return bool |
|
49
|
|
|
*/ |
|
50
|
|
|
private function isAssociative($arr) |
|
51
|
|
|
{ |
|
52
|
|
|
return array_keys($arr) !== range(0, count($arr) - 1); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @throws InvalidJsonRequestContent |
|
57
|
|
|
* @throws MissingOperationNameRequest |
|
58
|
|
|
* @throws MissingOperationRequest |
|
59
|
|
|
* |
|
60
|
|
|
* @return Sequence |
|
61
|
|
|
*/ |
|
62
|
|
|
public function all() |
|
63
|
|
|
{ |
|
64
|
|
|
$operations = $this->parseJson($this->adapter->getRequestBody()); |
|
65
|
|
|
if (!is_array($operations)) { |
|
66
|
|
|
throw new MissingOperationRequest(); |
|
67
|
|
|
} |
|
68
|
|
|
$operations = new Sequence($this->isAssociative($operations) ? array($operations) : $operations); |
|
69
|
|
|
$operationsWithoutOpKey = $operations->filterNot($this->operationWithKey()); |
|
70
|
|
|
if (!$operationsWithoutOpKey->isEmpty()) { |
|
71
|
|
|
/** @var array $operationData */ |
|
72
|
|
|
$operationData = $operationsWithoutOpKey->first()->get(); |
|
73
|
|
|
throw new MissingOperationNameRequest($operationData); |
|
74
|
|
|
} |
|
75
|
|
|
return $operations; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @param string $key |
|
80
|
|
|
* @return \Closure |
|
81
|
|
|
*/ |
|
82
|
|
|
private function operationWithKey($key = self::OP_KEY_NAME) |
|
83
|
|
|
{ |
|
84
|
|
|
return function ($operationData) use ($key) { |
|
85
|
|
|
return array_key_exists($key, $operationData); |
|
86
|
|
|
}; |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|