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 relationships object |
12
|
|
|
*/ |
13
|
|
|
trait ValidateRelationshipsObject |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* Asserts that a json fragment is a valid relationships object. |
17
|
|
|
* |
18
|
|
|
* It will do the following checks : |
19
|
|
|
* 1) asserts that the relationships object is not an array of objects (@see mustNotBeArrayOfObjects). |
20
|
|
|
* 2) asserts that each relationship of the collection has a valid name (@see validateMemberName) |
21
|
|
|
* and is a valid relationship object (@see validateRelationshipObject). |
22
|
|
|
* |
23
|
|
|
* @param array $json |
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
|
36 |
|
public function validateRelationshipsObject($json, bool $strict): void |
30
|
|
|
{ |
31
|
36 |
|
$this->mustNotBeArrayOfObjects($json); |
|
|
|
|
32
|
|
|
|
33
|
33 |
|
foreach ($json as $key => $relationship) { |
34
|
33 |
|
$this->validateMemberName($key, $strict); |
|
|
|
|
35
|
27 |
|
$this->validateRelationshipObject($relationship, $strict); |
36
|
|
|
} |
37
|
18 |
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Asserts that a json fragment is a valid relationship object. |
41
|
|
|
* |
42
|
|
|
* It will do the following checks : |
43
|
|
|
* 1) asserts that the relationship object contains at least one of the following member : "links", "data", "meta" |
44
|
|
|
* (@see containsAtLeastOneMember). |
45
|
|
|
* |
46
|
|
|
* Optionaly, if presents, it will checks : |
47
|
|
|
* 2) asserts that the data member is valid (@see validateResourceLinkage). |
48
|
|
|
* 3) asserts that the links member is valid (@see validateRelationshipLinksObject). |
49
|
|
|
* 4) asserts that the meta object is valid (@see validateMetaObject). |
50
|
|
|
* |
51
|
|
|
* @param array $json |
52
|
|
|
* @param boolean $strict If true, unsafe characters are not allowed when checking members name. |
53
|
|
|
* |
54
|
|
|
* @return void |
55
|
|
|
* @throws \VGirol\JsonApiStructure\Exception\ValidationException |
56
|
|
|
*/ |
57
|
57 |
|
public function validateRelationshipObject($json, bool $strict): void |
58
|
|
|
{ |
59
|
57 |
|
$this->isValidArgument(1, 'array', $json); |
|
|
|
|
60
|
|
|
|
61
|
54 |
|
$this->containsAtLeastOneMember( |
|
|
|
|
62
|
54 |
|
$this->getRule('RelationshipObject.AtLeast'), |
|
|
|
|
63
|
|
|
$json |
64
|
|
|
); |
65
|
|
|
|
66
|
48 |
|
if (!$this->isAutomatic() && ($this->isPost() || $this->isUpdate()) |
|
|
|
|
67
|
48 |
|
&& !\array_key_exists(Members::DATA, $json) |
68
|
|
|
) { |
69
|
6 |
|
$this->throw(Messages::RELATIONSHIP_NO_DATA_MEMBER, 403); |
|
|
|
|
70
|
|
|
} |
71
|
|
|
|
72
|
42 |
|
if (\array_key_exists(Members::DATA, $json)) { |
73
|
42 |
|
$data = $json[Members::DATA]; |
74
|
42 |
|
$this->validateResourceLinkage($data, $strict); |
|
|
|
|
75
|
|
|
} |
76
|
|
|
|
77
|
39 |
|
if (\array_key_exists(Members::LINKS, $json)) { |
78
|
18 |
|
$links = $json[Members::LINKS]; |
79
|
18 |
|
$withPagination = $this->canBePaginated($json); |
|
|
|
|
80
|
18 |
|
$this->validateRelationshipLinksObject($links, $withPagination, $strict); |
81
|
|
|
} |
82
|
|
|
|
83
|
33 |
|
if (\array_key_exists(Members::META, $json)) { |
84
|
9 |
|
$this->validateMetaObject($json[Members::META], $strict); |
|
|
|
|
85
|
|
|
} |
86
|
30 |
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Asserts that a json fragment is a valid link object extracted from a relationship object. |
90
|
|
|
* |
91
|
|
|
* It will do the following checks : |
92
|
|
|
* 1) asserts that the links object is valid (@see assertIsValidLinksObject) |
93
|
|
|
* with the following allowed members : "self", "related" |
94
|
|
|
* and eventually pagination links ("first", "last", "prev" and "next"). |
95
|
|
|
* |
96
|
|
|
* @param array $json |
97
|
|
|
* @param boolean $withPagination |
98
|
|
|
* @param boolean $strict If true, unsafe characters are not allowed when checking members name. |
99
|
|
|
* |
100
|
|
|
* @return void |
101
|
|
|
* @throws \VGirol\JsonApiStructure\Exception\ValidationException |
102
|
|
|
*/ |
103
|
30 |
|
public function validateRelationshipLinksObject($json, bool $withPagination, bool $strict): void |
104
|
|
|
{ |
105
|
30 |
|
$allowed = $this->getRule('Document.LinksObject.Allowed'); |
106
|
30 |
|
if ($withPagination) { |
107
|
9 |
|
$allowed = array_merge($allowed, $this->getRule('LinksObject.Pagination')); |
108
|
|
|
} |
109
|
30 |
|
$this->validateLinksObject($json, $allowed, $strict); |
|
|
|
|
110
|
18 |
|
} |
111
|
|
|
} |
112
|
|
|
|