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 resource linkage |
||||||||
12 | */ |
||||||||
13 | trait ValidateResourceLinkage |
||||||||
14 | { |
||||||||
15 | /** |
||||||||
16 | * Asserts that a json fragment is a valid resource linkage object. |
||||||||
17 | * |
||||||||
18 | * It will do the following checks : |
||||||||
19 | * 1) asserts that the provided resource linkage is either an object, an array of objects or the `null` value. |
||||||||
20 | * 2) asserts that the resource linkage or the collection of resource linkage is valid |
||||||||
21 | * (@see validateResourceIdentifierObject). |
||||||||
22 | * |
||||||||
23 | * @param array|null $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 | 66 | public function validateResourceLinkage($json, bool $strict): void |
|||||||
30 | { |
||||||||
31 | 66 | if ($json === null) { |
|||||||
32 | 6 | return; |
|||||||
33 | } |
||||||||
34 | |||||||||
35 | 60 | if (!\is_array($json)) { |
|||||||
0 ignored issues
–
show
introduced
by
![]() |
|||||||||
36 | 3 | $this->throw(Messages::RESOURCE_LINKAGE_BAD_TYPE, 403); |
|||||||
37 | } |
||||||||
38 | |||||||||
39 | 57 | if (\count($json) == 0) { |
|||||||
40 | 9 | return; |
|||||||
41 | } |
||||||||
42 | |||||||||
43 | 48 | if (!$this->isArrayOfObjects($json)) { |
|||||||
0 ignored issues
–
show
It seems like
isArrayOfObjects() must be provided by classes using this trait. How about adding it as abstract method to this trait?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||||||
44 | 36 | $json = [$json]; |
|||||||
45 | } |
||||||||
46 | 48 | foreach ($json as $resource) { |
|||||||
47 | 48 | $this->validateResourceIdentifierObject($resource, $strict); |
|||||||
48 | } |
||||||||
49 | 36 | } |
|||||||
50 | |||||||||
51 | /** |
||||||||
52 | * Asserts that a json fragment is a valid resource identifier object. |
||||||||
53 | * |
||||||||
54 | * It will do the following checks : |
||||||||
55 | * 1) asserts that the resource has "id" member(@see validateResourceIdMember). |
||||||||
56 | * 2) asserts that the resource has "type" (@see validateResourceTypeMember) members. |
||||||||
57 | * 3) asserts that it contains only the following allowed members : "id", "type" and "meta" |
||||||||
58 | * (@see containsOnlyAllowedMembers). |
||||||||
59 | * |
||||||||
60 | * Optionaly, if presents, it will checks : |
||||||||
61 | * 4) asserts that the meta object is valid (@see validateMetaObject). |
||||||||
62 | * |
||||||||
63 | * @param array $resource |
||||||||
64 | * @param boolean $strict If true, unsafe characters are not allowed when checking members name. |
||||||||
65 | * |
||||||||
66 | * @return void |
||||||||
67 | * @throws \VGirol\JsonApiStructure\Exception\ValidationException |
||||||||
68 | */ |
||||||||
69 | 96 | public function validateResourceIdentifierObject($resource, bool $strict): void |
|||||||
70 | { |
||||||||
71 | 96 | if (!\is_array($resource)) { |
|||||||
0 ignored issues
–
show
|
|||||||||
72 | 3 | $this->throw(Messages::RESOURCE_IDENTIFIER_MUST_BE_ARRAY, 403); |
|||||||
73 | } |
||||||||
74 | |||||||||
75 | 93 | $this->validateResourceIdMember($resource); |
|||||||
0 ignored issues
–
show
The method
validateResourceIdMember() does not exist on VGirol\JsonApiStructure\...ValidateResourceLinkage . Did you maybe mean validateResourceLinkage() ?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. ![]() |
|||||||||
76 | 75 | $this->validateResourceTypeMember($resource, $strict); |
|||||||
0 ignored issues
–
show
The method
validateResourceTypeMember() does not exist on VGirol\JsonApiStructure\...ValidateResourceLinkage . Did you maybe mean validateResourceLinkage() ?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. ![]() |
|||||||||
77 | |||||||||
78 | 69 | $this->containsOnlyAllowedMembers($this->getRule('ResourceIdentifierObject.Allowed'), $resource); |
|||||||
0 ignored issues
–
show
It seems like
containsOnlyAllowedMembers() must be provided by classes using this trait. How about adding it as abstract method to this trait?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() It seems like
getRule() must be provided by classes using this trait. How about adding it as abstract method to this trait?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||||||
79 | |||||||||
80 | 57 | if (\array_key_exists(Members::META, $resource)) { |
|||||||
81 | 9 | $this->validateMetaObject($resource[Members::META], $strict); |
|||||||
0 ignored issues
–
show
It seems like
validateMetaObject() must be provided by classes using this trait. How about adding it as abstract method to this trait?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||||||
82 | } |
||||||||
83 | 51 | } |
|||||||
84 | } |
||||||||
85 |