|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Author: Nil Portugués Calderó <[email protected]> |
|
4
|
|
|
* Date: 11/27/15 |
|
5
|
|
|
* Time: 11:40 PM. |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
namespace NilPortugues\Api\JsonApi\Server\Data; |
|
11
|
|
|
|
|
12
|
|
|
use NilPortugues\Api\JsonApi\JsonApiSerializer; |
|
13
|
|
|
use NilPortugues\Api\JsonApi\JsonApiTransformer; |
|
14
|
|
|
use NilPortugues\Api\JsonApi\Server\Errors\ErrorBag; |
|
15
|
|
|
use NilPortugues\Api\JsonApi\Server\Errors\InvalidAttributeError; |
|
16
|
|
|
use NilPortugues\Api\JsonApi\Server\Errors\InvalidTypeError; |
|
17
|
|
|
use NilPortugues\Api\JsonApi\Server\Errors\MissingAttributesError; |
|
18
|
|
|
use NilPortugues\Api\JsonApi\Server\Errors\MissingDataError; |
|
19
|
|
|
use NilPortugues\Api\JsonApi\Server\Errors\MissingTypeError; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Class DataAssertions. |
|
23
|
|
|
*/ |
|
24
|
|
|
class DataAssertions |
|
25
|
|
|
{ |
|
26
|
|
|
/** |
|
27
|
|
|
* @param array $data |
|
28
|
|
|
* @param JsonApiSerializer $serializer |
|
29
|
|
|
* @param string $className |
|
30
|
|
|
* @param ErrorBag $errorBag |
|
31
|
|
|
*/ |
|
32
|
|
|
public static function assert($data, JsonApiSerializer $serializer, $className, ErrorBag $errorBag) |
|
33
|
|
|
{ |
|
34
|
|
|
self::assertItIsArray($data, $errorBag); |
|
35
|
|
|
self::assertItHasTypeMember($data, $errorBag); |
|
36
|
|
|
self::assertItTypeMemberIsExpectedValue($data, $serializer, $className, $errorBag); |
|
37
|
|
|
self::assertItHasAttributeMember($data, $errorBag); |
|
38
|
|
|
self::assertAttributesExists($data, $serializer, $errorBag); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @param $data |
|
43
|
|
|
* @param ErrorBag $errorBag |
|
44
|
|
|
* |
|
45
|
|
|
* @throws DataException |
|
46
|
|
|
*/ |
|
47
|
|
|
protected static function assertItIsArray($data, ErrorBag $errorBag) |
|
48
|
|
|
{ |
|
49
|
|
|
if (empty($data) || !is_array($data)) { |
|
50
|
|
|
$errorBag->offsetSet(null, new MissingDataError()); |
|
51
|
|
|
throw new DataException(); |
|
52
|
|
|
} |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @param array $data |
|
57
|
|
|
* @param ErrorBag $errorBag |
|
58
|
|
|
* |
|
59
|
|
|
* @throws DataException |
|
60
|
|
|
*/ |
|
61
|
|
View Code Duplication |
protected static function assertItHasTypeMember(array $data, ErrorBag $errorBag) |
|
|
|
|
|
|
62
|
|
|
{ |
|
63
|
|
|
if (empty($data[JsonApiTransformer::TYPE_KEY]) || !is_string($data[JsonApiTransformer::TYPE_KEY])) { |
|
64
|
|
|
$errorBag->offsetSet(null, new MissingTypeError()); |
|
65
|
|
|
throw new DataException(); |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @param array $data |
|
71
|
|
|
* @param JsonApiSerializer $serializer |
|
72
|
|
|
* @param $className |
|
73
|
|
|
* @param ErrorBag $errorBag |
|
74
|
|
|
* |
|
75
|
|
|
* @throws DataException |
|
76
|
|
|
*/ |
|
77
|
|
|
protected static function assertItTypeMemberIsExpectedValue( |
|
78
|
|
|
array $data, |
|
79
|
|
|
JsonApiSerializer $serializer, |
|
80
|
|
|
$className, |
|
81
|
|
|
ErrorBag $errorBag |
|
82
|
|
|
) { |
|
83
|
|
|
$mapping = $serializer->getTransformer()->getMappingByAlias($data[JsonApiTransformer::TYPE_KEY]); |
|
84
|
|
|
|
|
85
|
|
View Code Duplication |
if (null === $mapping || $mapping->getClassName() !== $className) { |
|
|
|
|
|
|
86
|
|
|
$errorBag->offsetSet(null, new InvalidTypeError($data[JsonApiTransformer::TYPE_KEY])); |
|
87
|
|
|
throw new DataException(); |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* @param $data |
|
93
|
|
|
* @param ErrorBag $errorBag |
|
94
|
|
|
* |
|
95
|
|
|
* @throws DataException |
|
96
|
|
|
*/ |
|
97
|
|
View Code Duplication |
protected static function assertItHasAttributeMember($data, ErrorBag $errorBag) |
|
|
|
|
|
|
98
|
|
|
{ |
|
99
|
|
|
if (empty($data[JsonApiTransformer::ATTRIBUTES_KEY]) || !is_array($data[JsonApiTransformer::ATTRIBUTES_KEY])) { |
|
100
|
|
|
$errorBag->offsetSet(null, new MissingAttributesError()); |
|
101
|
|
|
throw new DataException(); |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* @param array $data |
|
107
|
|
|
* @param JsonApiSerializer $serializer |
|
108
|
|
|
* @param ErrorBag $errorBag |
|
109
|
|
|
* |
|
110
|
|
|
* @throws DataException |
|
111
|
|
|
*/ |
|
112
|
|
|
protected static function assertAttributesExists(array $data, JsonApiSerializer $serializer, ErrorBag $errorBag) |
|
113
|
|
|
{ |
|
114
|
|
|
$inputAttributes = array_keys($data[JsonApiTransformer::ATTRIBUTES_KEY]); |
|
115
|
|
|
|
|
116
|
|
|
$mapping = $serializer->getTransformer()->getMappingByAlias($data[JsonApiTransformer::TYPE_KEY]); |
|
117
|
|
|
|
|
118
|
|
|
$properties = str_replace( |
|
119
|
|
|
array_keys($mapping->getAliasedProperties()), |
|
120
|
|
|
array_values($mapping->getAliasedProperties()), |
|
121
|
|
|
$mapping->getProperties() |
|
122
|
|
|
); |
|
123
|
|
|
$properties = array_diff($properties, $mapping->getIdProperties()); |
|
124
|
|
|
$properties = array_merge($properties, $mapping->getHiddenProperties()); |
|
125
|
|
|
|
|
126
|
|
|
$hasErrors = false; |
|
127
|
|
|
foreach ($inputAttributes as $property) { |
|
128
|
|
View Code Duplication |
if (false === in_array($property, $properties)) { |
|
|
|
|
|
|
129
|
|
|
$hasErrors = true; |
|
130
|
|
|
$errorBag->offsetSet(null, new InvalidAttributeError($property, $data[JsonApiTransformer::TYPE_KEY])); |
|
131
|
|
|
} |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
if ($hasErrors) { |
|
135
|
|
|
throw new DataException(); |
|
136
|
|
|
} |
|
137
|
|
|
} |
|
138
|
|
|
} |
|
139
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.