|
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
|
|
|
|