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 links object |
12
|
|
|
*/ |
13
|
|
|
trait ValidateLinksObject |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* Asserts that a json fragment is a valid links object. |
17
|
|
|
* |
18
|
|
|
* It will do the following checks : |
19
|
|
|
* 1) asserts that it contains only allowed members (@see assertContainsOnlyAllowedMembers). |
20
|
|
|
* 2) asserts that each member of the links object is a valid link object (@see assertIsValidLinkObject). |
21
|
|
|
* |
22
|
|
|
* @param array $json |
23
|
|
|
* @param array<string> $allowedMembers |
24
|
|
|
* @param boolean $strict If true, unsafe characters are not allowed when checking members name. |
25
|
|
|
* |
26
|
|
|
* @return void |
27
|
|
|
* @throws \VGirol\JsonApiStructure\Exception\ValidationException |
28
|
|
|
*/ |
29
|
78 |
|
public function validateLinksObject($json, array $allowedMembers, bool $strict): void |
30
|
|
|
{ |
31
|
78 |
|
if (!\is_array($json)) { |
|
|
|
|
32
|
6 |
|
$this->throw(Messages::LINKS_OBJECT_NOT_ARRAY, 403); |
33
|
|
|
} |
34
|
|
|
|
35
|
72 |
|
$this->containsOnlyAllowedMembers($allowedMembers, $json); |
|
|
|
|
36
|
|
|
|
37
|
42 |
|
foreach ($json as $link) { |
38
|
42 |
|
$this->validateLinkObject($link, $strict); |
39
|
|
|
} |
40
|
36 |
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Asserts that a json fragment is a valid link object. |
44
|
|
|
* |
45
|
|
|
* It will do the following checks : |
46
|
|
|
* 1) asserts that the link object is a string, an array or the `null` value. |
47
|
|
|
* 2) in case it is an array : |
48
|
|
|
* 3) asserts that it has the "href" member. |
49
|
|
|
* 4) asserts that it contains only the following allowed members : "href" and "meta" |
50
|
|
|
* (@see containsOnlyAllowedMembers). |
51
|
|
|
* 5) if present, asserts that the "meta" object is valid (@see validateMetaObject). |
52
|
|
|
* |
53
|
|
|
* @param array|string|null $json |
54
|
|
|
* @param boolean $strict If true, unsafe characters are not allowed when checking members name. |
55
|
|
|
* |
56
|
|
|
* @return void |
57
|
|
|
* @throws \VGirol\JsonApiStructure\Exception\ValidationException |
58
|
|
|
*/ |
59
|
66 |
|
public function validateLinkObject($json, bool $strict): void |
60
|
|
|
{ |
61
|
66 |
|
if (($json === null) || \is_string($json)) { |
62
|
42 |
|
return; |
63
|
|
|
} |
64
|
|
|
|
65
|
24 |
|
if (!\is_array($json)) { |
|
|
|
|
66
|
6 |
|
$this->throw(Messages::LINK_OBJECT_BAD_TYPE, 403); |
67
|
|
|
} |
68
|
|
|
|
69
|
18 |
|
if (!\array_key_exists(Members::LINK_HREF, $json)) { |
70
|
3 |
|
$this->throw(Messages::LINK_OBJECT_MISS_HREF_MEMBER, 403); |
|
|
|
|
71
|
|
|
} |
72
|
|
|
|
73
|
15 |
|
$this->containsOnlyAllowedMembers($this->getRule('LinkObject.Allowed'), $json); |
|
|
|
|
74
|
|
|
|
75
|
12 |
|
if (\array_key_exists(Members::META, $json)) { |
76
|
12 |
|
$this->validateMetaObject($json[Members::META], $strict); |
|
|
|
|
77
|
|
|
} |
78
|
3 |
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Check if document's primary data or relationship's data is a collection and can be paginated |
82
|
|
|
* |
83
|
|
|
* @param array $json |
84
|
|
|
* |
85
|
|
|
* @return boolean |
86
|
|
|
*/ |
87
|
30 |
|
private function canBePaginated($json): bool |
88
|
|
|
{ |
89
|
30 |
|
return \array_key_exists(Members::DATA, $json) |
90
|
30 |
|
&& \is_array($json[Members::DATA]) |
91
|
30 |
|
&& $this->isArrayOfObjects($json[Members::DATA]); |
|
|
|
|
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|