|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace VGirol\JsonApiStructure\Concern; |
|
6
|
|
|
|
|
7
|
|
|
use VGirol\JsonApiStructure\Messages; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Assertions relating to the attributes object |
|
11
|
|
|
*/ |
|
12
|
|
|
trait ValidateAttributesObject |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* Asserts that a json fragment is a valid attributes object. |
|
16
|
|
|
* |
|
17
|
|
|
* It will do the following checks : |
|
18
|
|
|
* 1) asserts that attributes object is not an array of objects (@see mustNotBeArrayOfObjects). |
|
19
|
|
|
* 2) asserts that attributes object has no member with forbidden name (@see fieldHasNoForbiddenMemberName). |
|
20
|
|
|
* 3) asserts that each member name of the attributes object is valid (@see validateMemberName). |
|
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
|
81 |
|
public function validateAttributesObject($json, bool $strict): void |
|
29
|
|
|
{ |
|
30
|
81 |
|
$this->mustNotBeArrayOfObjects($json, Messages::ATTRIBUTES_OBJECT_MUST_BE_ARRAY, 403); |
|
|
|
|
|
|
31
|
|
|
|
|
32
|
75 |
|
$this->fieldHasNoForbiddenMemberName($json); |
|
33
|
|
|
|
|
34
|
72 |
|
foreach (\array_keys($json) as $key) { |
|
35
|
72 |
|
$this->validateMemberName($key, $strict); |
|
|
|
|
|
|
36
|
|
|
} |
|
37
|
63 |
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Asserts that a field object has no forbidden member name. |
|
41
|
|
|
* |
|
42
|
|
|
* Asserts that a field object (i.e., a resource object’s attributes or one of its relationships) |
|
43
|
|
|
* has no forbidden member name. |
|
44
|
|
|
* |
|
45
|
|
|
* It will do the following checks : |
|
46
|
|
|
* 1) asserts that each member name of the field is not a forbidden name (@see isNotForbiddenMemberName). |
|
47
|
|
|
* 2) if the field has nested objects, it will checks each all. |
|
48
|
|
|
* |
|
49
|
|
|
* @param mixed $field |
|
50
|
|
|
* |
|
51
|
|
|
* @return void |
|
52
|
|
|
* @throws \VGirol\JsonApiStructure\Exception\ValidationException |
|
53
|
|
|
*/ |
|
54
|
84 |
|
public function fieldHasNoForbiddenMemberName($field): void |
|
55
|
|
|
{ |
|
56
|
84 |
|
if (!\is_array($field)) { |
|
57
|
84 |
|
return; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
84 |
|
foreach ($field as $key => $value) { |
|
61
|
|
|
// For objects, $key is a string |
|
62
|
|
|
// For arrays of objects, $key is an integer |
|
63
|
84 |
|
if (\is_string($key)) { |
|
64
|
84 |
|
$this->isNotForbiddenMemberName($key); |
|
65
|
|
|
} |
|
66
|
84 |
|
$this->fieldHasNoForbiddenMemberName($value); |
|
67
|
|
|
} |
|
68
|
75 |
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Asserts that a member name is not forbidden (like "relationships" or "links"). |
|
72
|
|
|
* |
|
73
|
|
|
* @param string $name |
|
74
|
|
|
* |
|
75
|
|
|
* @return void |
|
76
|
|
|
* @throws \VGirol\JsonApiStructure\Exception\ValidationException |
|
77
|
|
|
*/ |
|
78
|
96 |
|
public function isNotForbiddenMemberName($name): void |
|
79
|
|
|
{ |
|
80
|
96 |
|
$this->isValidArgument(1, 'string', $name); |
|
|
|
|
|
|
81
|
|
|
|
|
82
|
93 |
|
if (\in_array($name, $this->getRule('MemberName.Forbidden'))) { |
|
|
|
|
|
|
83
|
15 |
|
$this->throw(Messages::MEMBER_NAME_NOT_ALLOWED, 403); |
|
|
|
|
|
|
84
|
|
|
} |
|
85
|
87 |
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|