-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathArrayArguments.php
More file actions
125 lines (97 loc) · 3.27 KB
/
ArrayArguments.php
File metadata and controls
125 lines (97 loc) · 3.27 KB
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
<?php
namespace Wave\Annotation;
use Wave\Annotation;
abstract class ArrayArguments extends Annotation
{
protected $parameters;
protected $errors = array();
public function __construct($key, $value, $from_class = null)
{
parent::__construct($key, $value, $from_class);
$this->parameters = $this->parseParameters();
$this->validate($from_class);
if (!empty($this->errors)) {
throw new InvalidAnnotationException('Annotation format error, ' . implode(', ', $this->errors), 0);
}
$this->build();
}
protected function validate($class)
{
}
protected function build()
{
}
protected function parseParameters()
{
$arguments = array();
foreach (explode(',', $this->value) as $argument) {
// attempt to explode the argument into a key:value
list($k, $value) = explode(':', $argument) + array(null, null);
// no value means it was just a plain argument, so just clean it and insert as normal
$k = trim($k, ' \'"');
if ($value === null) {
$arguments[] = $k;
} else {
$arguments[strtolower($k)] = trim($value, ' \'"');
}
}
return $arguments;
}
protected function acceptedKeys($keys)
{
foreach ($this->parameters as $key => $value) {
if (is_string($key) && !in_array($key, $keys)) {
$this->errors[] = "Invalid parameter: \"$key\".";
}
}
}
protected function requiredKeys($keys)
{
foreach ($keys as $key) {
if (!array_key_exists($key, $this->parameters)) {
$this->errors[] = get_class($this) . " requires a '$key' parameter.";
}
}
}
protected function acceptedKeylessValues($values)
{
foreach ($this->parameters as $key => $value) {
if (!is_string($key) && !in_array($value, $values)) {
$this->errors[] = "Unknown parameter: \"$value\".";
}
}
}
protected function acceptedIndexedValues($index, $values, $optional = true)
{
if ($optional && !isset($this->parameters[$index])) return;
if (!in_array($this->parameters[$index], $values)) {
$this->errors[] = "Parameter $index is set to \"" . $this->parameters[$index] . "\". Valid values: " . implode(', ', $values) . '.';
}
}
protected function acceptsNoKeylessValues()
{
$this->acceptedKeylessValues(array());
}
protected function acceptsNoKeyedValues()
{
$this->acceptedKeys(array());
}
protected function minimumParameterCount($count)
{
if (!(count($this->parameters) >= $count)) {
$this->errors[] = get_class($this) . " takes at least $count parameters.";
}
}
protected function maximumParameterCount($count)
{
if (!(count($this->parameters) <= $count)) {
$this->errors[] = get_class($this) . " takes at most $count parameters.";
}
}
protected function exactParameterCount($count)
{
if (count($this->parameters) != $count) {
$this->errors[] = get_class($this) . " requires exactly $count parameters.";
}
}
}