-
Notifications
You must be signed in to change notification settings - Fork 0
/
MarkupValidator.php
142 lines (122 loc) · 4.19 KB
/
MarkupValidator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
<?php
/**
* @author SignpostMarv
*/
declare(strict_types=1);
namespace SignpostMarv\DaftMarkup;
use InvalidArgumentException;
final class MarkupValidator
{
const BOOL_IN_ARRAY_STRICT = true;
/**
* @param array{!element:string} $markup
*/
public static function ValidateMarkup(array $markup) : void
{
self::MaybeThrowWhenValidatingMarkup($markup);
foreach (array_keys($markup) as $k) {
if ( ! in_array($k, Markup::SUPPORTED_ARRAY_ATTRIBUTES, self::BOOL_IN_ARRAY_STRICT)) {
throw new InvalidArgumentException(sprintf('Unsupported array key! (%s)', $k));
}
}
}
/**
* @param array{!element:string, !attributes?:array<string, scalar|array<int, scalar>>} $markup
*
* @return array<string, scalar|scalar[]>
*/
public static function ValidateMarkupAttributes(array $markup) : array
{
self::ValidateMarkup($markup);
if (isset($markup['!attributes'])) {
/**
* @var array<int|string, mixed>
*/
$attributes = $markup['!attributes'];
foreach (array_keys($attributes) as $attr) {
$attr = self::ValidateMarkupAttributeName($attr);
self::ValidateMarkupAttributeValue($attr, $attributes[$attr]);
}
/**
* @var array<string, scalar|scalar[]>
*/
$attributes = $attributes;
return $attributes;
}
return [];
}
/**
* @param array<int|string, mixed> $markup
*/
public static function MaybeThrowWhenValidatingMarkup(array $markup) : void
{
if ( ! array_key_exists('!element', $markup)) {
throw new InvalidArgumentException('Element not specified!');
} elseif ( ! is_string($markup['!element'])) {
throw new InvalidArgumentException('Element not specified as string!');
} elseif (preg_match(Markup::REGEX_ELEMENT_NAME, $markup['!element']) < 1) {
throw new InvalidArgumentException('Element not valid! (' . $markup['!element'] . ')');
} elseif (isset($markup['!content'])) {
self::ValidateContent($markup['!content']);
}
}
/**
* @param mixed $attr
*/
public static function ValidateMarkupAttributeName($attr) : string
{
if ( ! is_string($attr)) {
throw new InvalidArgumentException('Attribute keys must be strings!');
} elseif (preg_match(Markup::REGEX_ATTRIBUTE_NAME, $attr) < 1) {
throw new InvalidArgumentException(sprintf('Attribute name invalid! (%s)', $attr));
}
return $attr;
}
public static function ValidateMarkupAttributeArrayValue(string $attr, array $value) : void
{
/**
* @var array<int, int|string>
*/
$valueKeys = array_keys($value);
foreach ($valueKeys as $key) {
if ( ! is_scalar($value[$key])) {
throw new InvalidArgumentException(sprintf(
'Attribute %s contained a non-scalar array value!',
$attr
));
}
}
}
/**
* @param mixed $value
*/
public static function ValidateMarkupAttributeValue(string $attr, $value) : void
{
if (is_array($value)) {
static::ValidateMarkupAttributeArrayValue($attr, $value);
} elseif ( ! is_scalar($value)) {
throw new InvalidArgumentException(sprintf(
'Attribute %s contained a non-scalar value!',
$attr
));
}
}
/**
* @param mixed $markupContent
*/
final public static function ValidateContent($markupContent) : void
{
if ( ! is_array($markupContent)) {
throw new InvalidArgumentException('Element content must be specified as an array!');
}
/**
* @var array<int|string, mixed>
*/
$markupContent = $markupContent;
foreach (array_keys($markupContent) as $key) {
if ( ! is_scalar($markupContent[$key]) && ! is_array($markupContent[$key])) {
throw new InvalidArgumentException('Element content must be scalar or an array!');
}
}
}
}