Passed
Push — master ( 1ec89a...1818e9 )
by Vincent
06:54 queued 04:35
created

MemberName::default()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace VGirol\JsonApiStructure\Constraint;
6
7
use VGirol\JsonApiStructure\Messages;
8
9
class MemberName extends Constraint
10
{
11
    /**
12
     * @var bool
13
     */
14
    private $strict;
15
16
    /**
17
     * Class constructor.
18
     *
19
     * @param boolean $strict If true, unsafe characters are not allowed when checking members name.
20
     */
21 225
    public function __construct(bool $strict)
22
    {
23 225
        $this->strict = $strict;
24 225
    }
25
26
    /**
27
     * Returns a string representation of the constraint.
28
     */
29 81
    public function default(): string
30
    {
31 81
        return Messages::MEMBER_NAME_NOT_VALID;
32
    }
33
34
    /**
35
     * Evaluates the constraint for parameter $json. Returns true if the constraint is met, false otherwise.
36
     *
37
     * Asserts that a member name is valid.
38
     *
39
     * It will do the following checks :
40
     * 1) asserts that the name is a string with at least one character.
41
     * 2) asserts that the name has only allowed characters.
42
     * 3) asserts that it starts and ends with a globally allowed character.
43
     *
44
     * @link https://jsonapi.org/format/#document-member-names-allowed-characters
45
     *
46
     * @param mixed $name Value or object to evaluate
47
     *
48
     * @return bool
49
     * @throws \VGirol\JsonApiStructure\Exception\ValidationException
50
     */
51 225
    protected function handle($name): bool
52
    {
53 225
        if (!\is_string($name)) {
54 3
            $this->setFailureMessage(Messages::MEMBER_NAME_IS_NOT_STRING);
55
56 3
            return false;
57
        }
58
59 222
        if (\strlen($name) < 1) {
60 3
            $this->setFailureMessage(Messages::MEMBER_NAME_IS_TOO_SHORT);
61
62 3
            return false;
63
        }
64
65
        // Globally allowed characters
66 219
        $globally = '\x{0030}-\x{0039}\x{0041}-\x{005A}\x{0061}-\x{007A}';
67 219
        $globallyNotSafe = '\x{0080}-\x{FFFF}';
68
69
        // Allowed characters
70 219
        $allowed = '\x{002D}\x{005F}';
71 219
        $allowedNotSafe = '\x{0020}';
72
73 219
        $regex = "/[^{$globally}{$globallyNotSafe}{$allowed}{$allowedNotSafe}]+/u";
74 219
        $safeRegex = "/[^{$globally}{$allowed}]+/u";
75
76 219
        if (\preg_match($this->strict ? $safeRegex : $regex, $name) > 0) {
77 69
            $this->setFailureMessage(Messages::MEMBER_NAME_HAVE_RESERVED_CHARACTERS);
78
79 69
            return false;
80
        }
81
82 168
        $regex = "/^[{$globally}{$globallyNotSafe}]{1}(?:.*[{$globally}{$globallyNotSafe}]{1})?$/u";
83 168
        $safeRegex = "/^[{$globally}]{1}(?:.*[{$globally}]{1})?$/u";
84 168
        if (\preg_match($this->strict ? $safeRegex : $regex, $name) == 0) {
85 6
            $this->setFailureMessage(Messages::MEMBER_NAME_START_AND_END_WITH_ALLOWED_CHARACTERS);
86
87 6
            return false;
88
        }
89
90 162
        return true;
91
    }
92
}
93