|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace VGirol\JsonApiStructure\Concern; |
|
6
|
|
|
|
|
7
|
|
|
use VGirol\JsonApiConstant\Members; |
|
8
|
|
|
use VGirol\JsonApiStructure\Messages; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Assertions relating to the errors object |
|
12
|
|
|
*/ |
|
13
|
|
|
trait ValidateErrorsObject |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* Asserts that a json fragment is a valid errors object. |
|
17
|
|
|
* |
|
18
|
|
|
* It will do the following checks : |
|
19
|
|
|
* 1) asserts that the errors object is an array of objects (@see mustBeArrayOfObjects). |
|
20
|
|
|
* 2) asserts that each error object of the collection is valid (@see validateErrorObject). |
|
21
|
|
|
* |
|
22
|
|
|
* @param array $json |
|
23
|
|
|
* @param boolean $strict If true, unsafe characters are not allowed when checking members name. |
|
24
|
|
|
* |
|
25
|
|
|
* @return void |
|
26
|
|
|
* @throws \VGirol\JsonApiStructure\Exception\ValidationException |
|
27
|
|
|
*/ |
|
28
|
18 |
|
public function validateErrorsObject($json, bool $strict): void |
|
29
|
|
|
{ |
|
30
|
18 |
|
$this->mustBeArrayOfObjects($json, Messages::ERRORS_OBJECT_MUST_BE_ARRAY, 400); |
|
|
|
|
|
|
31
|
|
|
|
|
32
|
12 |
|
foreach ($json as $error) { |
|
33
|
12 |
|
$this->validateErrorObject($error, $strict); |
|
34
|
|
|
} |
|
35
|
6 |
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Asserts that a json fragment is a valid error object. |
|
39
|
|
|
* |
|
40
|
|
|
* It will do the following checks : |
|
41
|
|
|
* 1) asserts that the error object is not empty. |
|
42
|
|
|
* 2) asserts it contains only the following allowed members : |
|
43
|
|
|
* "id", "links", "status", "code", "title", "details", "source", "meta" (@see containsOnlyAllowedMembers). |
|
44
|
|
|
* |
|
45
|
|
|
* Optionaly, if presents, it will checks : |
|
46
|
|
|
* 3) asserts that the "status" member is a string. |
|
47
|
|
|
* 4) asserts that the "code" member is a string. |
|
48
|
|
|
* 5) asserts that the "title" member is a string. |
|
49
|
|
|
* 6) asserts that the "details" member is a string. |
|
50
|
|
|
* 7) asserts that the "source" member is valid(@see validateErrorSourceObject). |
|
51
|
|
|
* 8) asserts that the "links" member is valid(@see validateErrorLinksObject). |
|
52
|
|
|
* 9) asserts that the "meta" member is valid(@see validateMetaObject). |
|
53
|
|
|
* |
|
54
|
|
|
* @param array $json |
|
55
|
|
|
* @param boolean $strict If true, unsafe characters are not allowed when checking members name. |
|
56
|
|
|
* |
|
57
|
|
|
* @return void |
|
58
|
|
|
* @throws \VGirol\JsonApiStructure\Exception\ValidationException |
|
59
|
|
|
*/ |
|
60
|
54 |
|
public function validateErrorObject($json, bool $strict): void |
|
61
|
|
|
{ |
|
62
|
54 |
|
if (!\is_array($json)) { |
|
|
|
|
|
|
63
|
3 |
|
$this->throw(Messages::ERROR_OBJECT_MUST_BE_ARRAY, 400); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
51 |
|
if (\count($json) == 0) { |
|
67
|
3 |
|
$this->throw(Messages::ERROR_OBJECT_MUST_NOT_BE_EMPTY, 400); |
|
|
|
|
|
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
48 |
|
$this->containsOnlyAllowedMembers($this->getRule('ErrorObject.Allowed'), $json); |
|
|
|
|
|
|
71
|
|
|
|
|
72
|
|
|
$checks = [ |
|
73
|
45 |
|
Members::ERROR_STATUS => Messages::ERROR_OBJECT_STATUS_MEMBER_MUST_BE_STRING, |
|
74
|
|
|
Members::ERROR_CODE => Messages::ERROR_OBJECT_CODE_MEMBER_MUST_BE_STRING, |
|
75
|
|
|
Members::ERROR_TITLE => Messages::ERROR_OBJECT_TITLE_MEMBER_MUST_BE_STRING, |
|
76
|
|
|
Members::ERROR_DETAILS => Messages::ERROR_OBJECT_DETAILS_MEMBER_MUST_BE_STRING |
|
77
|
|
|
]; |
|
78
|
|
|
|
|
79
|
45 |
|
foreach ($checks as $member => $failureMsg) { |
|
80
|
45 |
|
if (\array_key_exists($member, $json) && !\is_string($json[$member])) { |
|
81
|
15 |
|
$this->throw($failureMsg, 400); |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
30 |
|
if (\array_key_exists(Members::ERROR_SOURCE, $json)) { |
|
86
|
15 |
|
$this->validateErrorSourceObject($json[Members::ERROR_SOURCE]); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
18 |
|
if (\array_key_exists(Members::LINKS, $json)) { |
|
90
|
6 |
|
$this->validateErrorLinksObject($json[Members::LINKS], $strict); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
15 |
|
if (\array_key_exists(Members::META, $json)) { |
|
94
|
9 |
|
$this->validateMetaObject($json[Members::META], $strict); |
|
|
|
|
|
|
95
|
|
|
} |
|
96
|
9 |
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Asserts that a json fragment is a valid error links object. |
|
100
|
|
|
* |
|
101
|
|
|
* It will do the following checks : |
|
102
|
|
|
* 1) asserts that le links object is valid (@see validateLinksObject with only "about" member allowed). |
|
103
|
|
|
* |
|
104
|
|
|
* @param array $json |
|
105
|
|
|
* @param boolean $strict If true, unsafe characters are not allowed when checking members name. |
|
106
|
|
|
* |
|
107
|
|
|
* @return void |
|
108
|
|
|
* @throws \VGirol\JsonApiStructure\Exception\ValidationException |
|
109
|
|
|
*/ |
|
110
|
12 |
|
public function validateErrorLinksObject($json, bool $strict): void |
|
111
|
|
|
{ |
|
112
|
12 |
|
$this->validateLinksObject($json, $this->getRule('ErrorObject.LinksObject.Allowed'), $strict); |
|
|
|
|
|
|
113
|
6 |
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* Asserts that a json fragment is a valid error source object. |
|
117
|
|
|
* |
|
118
|
|
|
* It will do the following checks : |
|
119
|
|
|
* 1) if the "pointer" member is present, asserts it is a string starting with a "/" character. |
|
120
|
|
|
* 2) if the "parameter" member is present, asserts that it is a string. |
|
121
|
|
|
* |
|
122
|
|
|
* @param array $json |
|
123
|
|
|
* |
|
124
|
|
|
* @return void |
|
125
|
|
|
* @throws \VGirol\JsonApiStructure\Exception\ValidationException |
|
126
|
|
|
*/ |
|
127
|
33 |
|
public function validateErrorSourceObject($json): void |
|
128
|
|
|
{ |
|
129
|
33 |
|
if (!\is_array($json)) { |
|
|
|
|
|
|
130
|
6 |
|
$this->throw(Messages::ERROR_OBJECT_SOURCE_OBJECT_MUST_BE_ARRAY, 400); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
27 |
|
if (\array_key_exists(Members::ERROR_POINTER, $json)) { |
|
134
|
18 |
|
if (!\is_string($json[Members::ERROR_POINTER])) { |
|
135
|
6 |
|
$this->throw(Messages::ERROR_SOURCE_POINTER_IS_NOT_STRING, 400); |
|
136
|
|
|
} |
|
137
|
12 |
|
if ($json[Members::ERROR_POINTER][0] != '/') { |
|
138
|
6 |
|
$this->throw(Messages::ERROR_SOURCE_POINTER_START, 400); |
|
139
|
|
|
} |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
15 |
|
if (\array_key_exists(Members::ERROR_PARAMETER, $json)) { |
|
143
|
9 |
|
if (!\is_string($json[Members::ERROR_PARAMETER])) { |
|
144
|
6 |
|
$this->throw(Messages::ERROR_SOURCE_PARAMETER_IS_NOT_STRING, 400); |
|
145
|
|
|
} |
|
146
|
|
|
} |
|
147
|
9 |
|
} |
|
148
|
|
|
} |
|
149
|
|
|
|