Completed
Push — master ( bfd868...ee3a72 )
by Vincent
12:29 queued 10:26
created

AssertMemberName::assertIsNotForbiddenMemberName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 1
dl 0
loc 9
ccs 7
cts 7
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
namespace VGirol\JsonApiAssert\Asserts;
3
4
use PHPUnit\Framework\Assert as PHPUnit;
5
use VGirol\JsonApiAssert\Messages;
6
7
trait AssertMemberName
8
{
9
    /**
10
     * Asserts that a member name is valid.
11
     *
12
     * @param string    $name
13
     * @param boolean   $strict
14
     *
15
     * @throws PHPUnit\Framework\ExpectationFailedException
16
     */
17 48
    public static function assertIsValidMemberName($name, $strict = false)
18
    {
19 48
        PHPUnit::assertIsString(
20 48
            $name,
21 48
            Messages::MEMBER_NAME_IS_NOT_STRING
22
        );
23
24 47
        PHPUnit::assertGreaterThanOrEqual(
25 47
            1,
26 47
            strlen($name),
27 47
            Messages::MEMBER_NAME_IS_TOO_SHORT
28
        );
29
30
        // Globally allowed characters
31 46
        $globally = '\x{0030}-\x{0039}\x{0041}-\x{005A}\x{0061}-\x{007A}';
32 46
        $globallyNotSafe = '\x{0080}-\x{FFFF}';
33
        // Allowed characters
34 46
        $allowed = '\x{002D}\x{005F}';
35 46
        $allowedNotSafe = '\x{0020}';
36
37 46
        $regex = $strict ? "/[^{$globally}{$allowed}]+/u" : "/[^{$globally}{$globallyNotSafe}{$allowed}{$allowedNotSafe}]+/u";
38 46
        PHPUnit::assertNotRegExp(
39 46
            $regex,
40 46
            $name,
41 46
            Messages::MEMBER_NAME_HAVE_RESERVED_CHARACTERS
42
        );
43
44 39
        $regex = $strict ? "/^[{$globally}]{1}.*[{$globally}]{1}$/u" : "/^[{$globally}{$globallyNotSafe}]{1}.*[{$globally}{$globallyNotSafe}]{1}$/u";
45 39
        PHPUnit::assertRegExp(
46 39
            $regex,
47 39
            $name,
48 39
            Messages::MEMBER_NAME_START_AND_END_WITH_ALLOWED_CHARACTERS
49
        );
50 37
    }
51
52
    /**
53
     * Asserts that a field object has no forbidden member name
54
     *
55
     * @param mixed $field
56
     *
57
     * @throws PHPUnit\Framework\ExpectationFailedException
58
     */
59 14
    public static function assertFieldHasNoForbiddenMemberName($field)
60
    {
61 14
        if (!is_array($field)) {
62 14
            return;
63
        }
64
65 14
        foreach ($field as $key => $value) {
66
            // For objects, $key is a string
67
            // For arrays of objects, $key is an integer
68 14
            if (is_string($key)) {
69 14
                static::assertIsNotForbiddenMemberName($key);
70
            }
71 14
            static::assertFieldHasNoForbiddenMemberName($value);
72
        }
73 13
    }
74
75
    /**
76
     * Asserts that a member name is not forbidden
77
     *
78
     * @param string $name
79
     *
80
     * @throws PHPUnit\Framework\ExpectationFailedException
81
     */
82 17
    public static function assertIsNotForbiddenMemberName($name)
83
    {
84 17
        PHPUnit::assertIsString($name);
85
86 17
        $forbidden = ['relationships', 'links'];
87 17
        PHPUnit::assertNotContains(
88 17
            $name,
89 17
            $forbidden,
90 17
            Messages::MEMBER_NAME_NOT_ALLOWED
91
        );
92 15
    }
93
}
94