validateRelationshipLinksObject()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
nc 2
nop 3
dl 0
loc 7
ccs 5
cts 5
cp 1
crap 2
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 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);
0 ignored issues
show
Bug introduced by
It seems like mustNotBeArrayOfObjects() 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

31
        $this->/** @scrutinizer ignore-call */ 
32
               mustNotBeArrayOfObjects($json);
Loading history...
32
33 33
        foreach ($json as $key => $relationship) {
34 33
            $this->validateMemberName($key, $strict);
0 ignored issues
show
Bug introduced by
It seems like validateMemberName() 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

34
            $this->/** @scrutinizer ignore-call */ 
35
                   validateMemberName($key, $strict);
Loading history...
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);
0 ignored issues
show
Bug introduced by
It seems like isValidArgument() 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

59
        $this->/** @scrutinizer ignore-call */ 
60
               isValidArgument(1, 'array', $json);
Loading history...
60
61 54
        $this->containsAtLeastOneMember(
0 ignored issues
show
Bug introduced by
It seems like containsAtLeastOneMember() 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

61
        $this->/** @scrutinizer ignore-call */ 
62
               containsAtLeastOneMember(
Loading history...
62 54
            $this->getRule('RelationshipObject.AtLeast'),
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

62
            $this->/** @scrutinizer ignore-call */ 
63
                   getRule('RelationshipObject.AtLeast'),
Loading history...
63
            $json
64
        );
65
66 48
        if (!$this->isAutomatic() && ($this->isPost() || $this->isUpdate())
0 ignored issues
show
Bug introduced by
It seems like isAutomatic() 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

66
        if (!$this->/** @scrutinizer ignore-call */ isAutomatic() && ($this->isPost() || $this->isUpdate())
Loading history...
Bug introduced by
It seems like isUpdate() 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

66
        if (!$this->isAutomatic() && ($this->isPost() || $this->/** @scrutinizer ignore-call */ isUpdate())
Loading history...
Bug introduced by
It seems like isPost() 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

66
        if (!$this->isAutomatic() && ($this->/** @scrutinizer ignore-call */ isPost() || $this->isUpdate())
Loading history...
67 48
            && !\array_key_exists(Members::DATA, $json)
68
        ) {
69 6
            $this->throw(Messages::RELATIONSHIP_NO_DATA_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

69
            $this->/** @scrutinizer ignore-call */ 
70
                   throw(Messages::RELATIONSHIP_NO_DATA_MEMBER, 403);
Loading history...
70
        }
71
72 42
        if (\array_key_exists(Members::DATA, $json)) {
73 42
            $data = $json[Members::DATA];
74 42
            $this->validateResourceLinkage($data, $strict);
0 ignored issues
show
Bug introduced by
It seems like validateResourceLinkage() 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

74
            $this->/** @scrutinizer ignore-call */ 
75
                   validateResourceLinkage($data, $strict);
Loading history...
75
        }
76
77 39
        if (\array_key_exists(Members::LINKS, $json)) {
78 18
            $links = $json[Members::LINKS];
79 18
            $withPagination = $this->canBePaginated($json);
0 ignored issues
show
Bug introduced by
It seems like canBePaginated() 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

79
            /** @scrutinizer ignore-call */ 
80
            $withPagination = $this->canBePaginated($json);
Loading history...
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);
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

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

109
        $this->/** @scrutinizer ignore-call */ 
110
               validateLinksObject($json, $allowed, $strict);
Loading history...
110 18
    }
111
}
112