Completed
Push — master ( a787fa...dc9af5 )
by Vincent
05:35
created

AssertRelationshipsObject   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 9
eloc 31
c 0
b 0
f 0
dl 0
loc 84
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A assertIsValidRelationshipsObject() 0 7 2
A assertIsValidRelationshipObject() 0 26 5
A assertIsValidRelationshipLinksObject() 0 18 2
1
<?php
2
declare (strict_types = 1);
3
4
namespace VGirol\JsonApiAssert\Asserts\Structure;
5
6
use PHPUnit\Framework\Assert as PHPUnit;
7
use VGirol\JsonApiAssert\Members;
8
9
/**
10
 * Assertions relating to the relationships object
11
 */
12
trait AssertRelationshipsObject
13
{
14
    /**
15
     * Asserts that a json fragment is a valid relationships object.
16
     *
17
     * @param array     $json
18
     * @param boolean   $strict         If true, unsafe characters are not allowed when checking members name.
19
     * @return void
20
     * @throws \PHPUnit\Framework\ExpectationFailedException
21
     */
22
    public static function assertIsValidRelationshipsObject($json, bool $strict): void
23
    {
24
        static::assertIsNotArrayOfObjects($json);
25
26
        foreach ($json as $key => $relationship) {
27
            static::assertIsValidMemberName($key, $strict);
28
            static::assertIsValidRelationshipObject($relationship, $strict);
29
        }
30
    }
31
32
    /**
33
     * Asserts that a json fragment is a valid relationship object.
34
     *
35
     * @param array    $json
36
     * @param boolean       $strict         If true, unsafe characters are not allowed when checking members name.
37
     * @return void
38
     * @throws \PHPUnit\Framework\ExpectationFailedException
39
     */
40
    public static function assertIsValidRelationshipObject($json, bool $strict): void
41
    {
42
        PHPUnit::assertIsArray($json);
43
44
        static::assertContainsAtLeastOneMember(
45
            [
46
                Members::LINKS,
47
                Members::DATA,
48
                Members::META
49
            ],
50
            $json
51
        );
52
53
        if (isset($json[Members::DATA])) {
54
            $data = $json[Members::DATA];
55
            static::assertIsValidResourceLinkage($data, $strict);
56
        }
57
58
        if (isset($json[Members::LINKS])) {
59
            $links = $json[Members::LINKS];
60
            $withPagination = isset($json[Members::DATA]) && static::isArrayOfObjects($json[Members::DATA]);
61
            static::assertIsValidRelationshipLinksObject($links, $withPagination, $strict);
62
        }
63
64
        if (isset($json[Members::META])) {
65
            static::assertIsValidMetaObject($json[Members::META], $strict);
0 ignored issues
show
Bug introduced by
The method assertIsValidMetaObject() does not exist on VGirol\JsonApiAssert\Ass...sertRelationshipsObject. Did you maybe mean assertIsValidRelationshipObject()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

65
            static::/** @scrutinizer ignore-call */ 
66
                    assertIsValidMetaObject($json[Members::META], $strict);

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.

Loading history...
66
        }
67
    }
68
69
    /**
70
     * Asserts that a json fragment is a valid link object extracted from a relationship object.
71
     *
72
     * @param array     $json
73
     * @param boolean   $withPagination
74
     * @param boolean   $strict         If true, unsafe characters are not allowed when checking members name.
75
     * @return void
76
     * @throws \PHPUnit\Framework\ExpectationFailedException
77
     */
78
    public static function assertIsValidRelationshipLinksObject($json, bool $withPagination, bool $strict): void
79
    {
80
        $allowed = [
81
            Members::SELF,
82
            Members::RELATED
83
        ];
84
        if ($withPagination) {
85
            $allowed = array_merge(
86
                $allowed,
87
                [
88
                    Members::FIRST,
89
                    Members::LAST,
90
                    Members::NEXT,
91
                    Members::PREV
92
                ]
93
            );
94
        }
95
        static::assertIsValidLinksObject($json, $allowed, $strict);
96
    }
97
}
98