Passed
Push — master ( 1e6084...cf392e )
by Vincent
02:40
created

AssertMemberName::assertFieldHasNoForbiddenMemberName()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 6
nc 4
nop 1
dl 0
loc 13
ccs 7
cts 7
cp 1
crap 4
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 73
    public static function assertIsValidMemberName($name, $strict)
18
    {
19 73
        PHPUnit::assertIsString(
20 73
            $name,
21 73
            Messages::MEMBER_NAME_IS_NOT_STRING
22
        );
23
24 72
        PHPUnit::assertGreaterThanOrEqual(
25 72
            1,
26 72
            strlen($name),
27 72
            Messages::MEMBER_NAME_IS_TOO_SHORT
28
        );
29
30
        // Globally allowed characters
31 71
        $globally = '\x{0030}-\x{0039}\x{0041}-\x{005A}\x{0061}-\x{007A}';
32 71
        $globallyNotSafe = '\x{0080}-\x{FFFF}';
33
        // Allowed characters
34 71
        $allowed = '\x{002D}\x{005F}';
35 71
        $allowedNotSafe = '\x{0020}';
36
37 71
        $regex = $strict ? "/[^{$globally}{$allowed}]+/u" : "/[^{$globally}{$globallyNotSafe}{$allowed}{$allowedNotSafe}]+/u";
38 71
        PHPUnit::assertNotRegExp(
39 71
            $regex,
40 71
            $name,
41 71
            Messages::MEMBER_NAME_HAVE_RESERVED_CHARACTERS
42
        );
43
44 55
        $regex = $strict ? "/^[{$globally}]{1}(?:.*[{$globally}]{1})?$/u" : "/^[{$globally}{$globallyNotSafe}]{1}(?:.*[{$globally}{$globallyNotSafe}]{1})?$/u";
45 55
        PHPUnit::assertRegExp(
46 55
            $regex,
47 55
            $name,
48 55
            Messages::MEMBER_NAME_START_AND_END_WITH_ALLOWED_CHARACTERS
49
        );
50 53
    }
51
}
52