-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathAbstractParameter.php
178 lines (160 loc) · 4.39 KB
/
AbstractParameter.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
<?php declare(strict_types=1);
namespace MyENA\RGW;
use MyENA\RGW\Validator\NotEmptyValidator;
use MyENA\RGW\Validator\RequiredValidator;
/**
* Class AbstractParameter
* @package MyENA\RGW
*/
abstract class AbstractParameter implements Parameter
{
/** @var string */
protected $name = '';
/** @var string */
protected $location;
/** @var mixed */
protected $default;
/** @var \MyENA\RGW\Validator[] */
protected $validators = [];
/** @var \MyENA\RGW\Validator */
protected $failedValidator;
/**
* AbstractParameter constructor.
* @param string $name
* @param string $location
*/
public function __construct(string $name, string $location)
{
if ('' === ($name = trim($name))) {
throw new \InvalidArgumentException('name cannot be empty');
}
$this->name = $name;
if ($location !== self::IN_ROUTE && $location !== self::IN_QUERY) {
throw new \InvalidArgumentException(sprintf(
'location must be one of: ["%s"]',
implode('", "', [self::IN_ROUTE, self::IN_QUERY])
));
}
$this->location = $location;
if (self::IN_ROUTE === $location) {
$this->requireValue();
$this->requireNotEmpty();
}
}
/**
* Mark this parameter as "required", meaning it must either have a specific or default value
*
* @return \MyENA\RGW\Parameter\SingleParameter
*/
public function requireValue(): Parameter
{
$this->validators = [
RequiredValidator::NAME => Validators::Required(),
] + $this->validators;
return $this;
}
/**
* Marks this parameter as requiring its value to be "non-empty", if one is defined.
*
* @return \MyENA\RGW\Parameter
*/
public function requireNotEmpty(): Parameter
{
if (isset($this->validators[RequiredValidator::NAME])) {
$this->validators = [
RequiredValidator::NAME => Validators::Required(),
NotEmptyValidator::NAME => Validators::NotEmpty(),
] + $this->validators;
} else {
$this->validators = [
NotEmptyValidator::NAME => Validators::NotEmpty(),
] + $this->validators;
}
return $this;
}
/**
* @return string
*/
public function getName(): string
{
return $this->name;
}
/**
* @return string
*/
public function getLocation(): string
{
return $this->location;
}
/**
* Is this parameter required by the part?
*
* @return bool
*/
public function isRequired(): bool
{
return isset($this->validators[Validator\RequiredValidator::NAME]);
}
/**
* Sets a default value for this argument
*
* @param mixed $defaultValue
* @return static
*/
public function setDefaultValue($defaultValue): Parameter
{
if (isset($this->default) && $this->default !== $defaultValue) {
unset($this->failedValidator);
}
if (is_object($defaultValue)) {
$this->default = clone $defaultValue;
} else {
$this->default = $defaultValue;
}
return $this;
}
/**
* @return mixed|null
*/
public function getDefaultValue()
{
return $this->default ?? null;
}
/**
* @param \MyENA\RGW\Validator $validator
* @return \MyENA\RGW\Parameter\SingleParameter
*/
public function addValidator(Validator $validator): Parameter
{
$this->validators[$validator->name()] = $validator;
return $this;
}
/**
* Returns all validators registered with this argument
*
* @return \MyENA\RGW\Validator[]
*/
public function getValidators(): array
{
return $this->validators;
}
/**
* Attempts to return a specific validator
*
* @param string $name
* @return \MyENA\RGW\Validator|null
*/
public function getValidator(string $name): ?Validator
{
return $this->validators[$name] ?? null;
}
/**
* Returns the last validator to fail, if validation attempt was made
*
* @return \MyENA\RGW\Validator|null
*/
public function getFailedValidator(): ?Validator
{
return $this->failedValidator ?? null;
}
}