ValidateLinksObject::validateLinksObject()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 5
nc 4
nop 3
dl 0
loc 10
ccs 6
cts 6
cp 1
crap 3
rs 10
c 1
b 0
f 0
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)) {
0 ignored issues
show
introduced by
The condition is_array($json) is always true.
Loading history...
32 6
            $this->throw(Messages::LINKS_OBJECT_NOT_ARRAY, 403);
33
        }
34
35 72
        $this->containsOnlyAllowedMembers($allowedMembers, $json);
0 ignored issues
show
Bug introduced by
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 ignore-call  annotation

35
        $this->/** @scrutinizer ignore-call */ 
36
               containsOnlyAllowedMembers($allowedMembers, $json);
Loading history...
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)) {
0 ignored issues
show
introduced by
The condition is_array($json) is always true.
Loading history...
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);
0 ignored issues
show
Bug introduced by
It seems like throw() 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 ignore-call  annotation

70
            $this->/** @scrutinizer ignore-call */ 
71
                   throw(Messages::LINK_OBJECT_MISS_HREF_MEMBER, 403);
Loading history...
71
        }
72
73 15
        $this->containsOnlyAllowedMembers($this->getRule('LinkObject.Allowed'), $json);
0 ignored issues
show
Bug introduced by
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 ignore-call  annotation

73
        $this->containsOnlyAllowedMembers($this->/** @scrutinizer ignore-call */ getRule('LinkObject.Allowed'), $json);
Loading history...
74
75 12
        if (\array_key_exists(Members::META, $json)) {
76 12
            $this->validateMetaObject($json[Members::META], $strict);
0 ignored issues
show
Bug introduced by
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 ignore-call  annotation

76
            $this->/** @scrutinizer ignore-call */ 
77
                   validateMetaObject($json[Members::META], $strict);
Loading history...
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]);
0 ignored issues
show
Bug introduced by
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 ignore-call  annotation

91
                && $this->/** @scrutinizer ignore-call */ isArrayOfObjects($json[Members::DATA]);
Loading history...
92
    }
93
}
94