Passed
Push — master ( 71cd79...3a5308 )
by Vincent
02:53 queued 20s
created

AssertMemberName::memberNameConstraint()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
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\JsonApiAssert\Asserts\Structure;
6
7
use PHPUnit\Framework\Assert as PHPUnit;
8
use VGirol\JsonApiAssert\Constraint\MemberNameConstraint;
9
10
/**
11
 * Assertions relating to the member name
12
 */
13
trait AssertMemberName
14
{
15
    /**
16
     * Asserts that a member name is valid.
17
     *
18
     * It will do the following checks :
19
     * 1) asserts that the name is a string with at least one character.
20
     * 2) asserts that the name has only allowed characters.
21
     * 3) asserts that it starts and ends with a globally allowed character.
22
     *
23
     * @link https://jsonapi.org/format/#document-member-names-allowed-characters
24
     *
25
     * @param string  $name
26
     * @param boolean $strict If true, unsafe characters are not allowed when checking members name.
27
     *
28
     * @return void
29
     * @throws \PHPUnit\Framework\AssertionFailedError
30
     */
31 27
    public static function assertIsValidMemberName($name, bool $strict): void
32
    {
33 27
        PHPUnit::assertThat($name, self::memberNameConstraint($strict));
34 9
    }
35
36
    /**
37
     * Returns a new instance of the \VGirol\JsonApiAssert\Constraint\MemberNameConstraint class.
38
     *
39
     * @param bool $strict
40
     *
41
     * @return \VGirol\JsonApiAssert\Constraint\MemberNameConstraint
42
     */
43 27
    private static function memberNameConstraint(bool $strict): MemberNameConstraint
44
    {
45 27
        return new MemberNameConstraint($strict);
46
    }
47
}
48