[CLEANUP] Replace strlen() with === for zero length check
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Classes / Mvc / Controller / Argument.php
1 <?php
2 namespace TYPO3\CMS\Extbase\Mvc\Controller;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use TYPO3\CMS\Extbase\Utility\TypeHandlingUtility;
18
19 /**
20 * A controller argument
21 *
22 * @api
23 */
24 class Argument {
25
26 /**
27 * @var \TYPO3\CMS\Extbase\Property\PropertyMapper
28 * @inject
29 */
30 protected $propertyMapper;
31
32 /**
33 * @var \TYPO3\CMS\Extbase\Mvc\Controller\MvcPropertyMappingConfiguration
34 * @inject
35 */
36 protected $propertyMappingConfiguration;
37
38 /**
39 * Name of this argument
40 *
41 * @var string
42 */
43 protected $name = '';
44
45 /**
46 * Short name of this argument
47 *
48 * @var string
49 */
50 protected $shortName = NULL;
51
52 /**
53 * Data type of this argument's value
54 *
55 * @var string
56 */
57 protected $dataType = NULL;
58
59 /**
60 * TRUE if this argument is required
61 *
62 * @var bool
63 */
64 protected $isRequired = FALSE;
65
66 /**
67 * Actual value of this argument
68 *
69 * @var object
70 */
71 protected $value = NULL;
72
73 /**
74 * Default value. Used if argument is optional.
75 *
76 * @var mixed
77 */
78 protected $defaultValue = NULL;
79
80 /**
81 * A custom validator, used supplementary to the base validation
82 *
83 * @var \TYPO3\CMS\Extbase\Validation\Validator\ValidatorInterface
84 */
85 protected $validator = NULL;
86
87 /**
88 * The validation results. This can be asked if the argument has errors.
89 *
90 * @var \TYPO3\CMS\Extbase\Error\Result
91 */
92 protected $validationResults = NULL;
93
94 /**
95 * Constructs this controller argument
96 *
97 * @param string $name Name of this argument
98 * @param string $dataType The data type of this argument
99 * @throws \InvalidArgumentException if $name is not a string or empty
100 * @api
101 */
102 public function __construct($name, $dataType) {
103 if (!is_string($name)) {
104 throw new \InvalidArgumentException('$name must be of type string, ' . gettype($name) . ' given.', 1187951688);
105 }
106 if ($name === '') {
107 throw new \InvalidArgumentException('$name must be a non-empty string.', 1232551853);
108 }
109 $this->name = $name;
110 $this->dataType = TypeHandlingUtility::normalizeType($dataType);
111 }
112
113 /**
114 * Returns the name of this argument
115 *
116 * @return string This argument's name
117 * @api
118 */
119 public function getName() {
120 return $this->name;
121 }
122
123 /**
124 * Sets the short name of this argument.
125 *
126 * @param string $shortName A "short name" - a single character
127 * @throws \InvalidArgumentException if $shortName is not a character
128 * @return \TYPO3\CMS\Extbase\Mvc\Controller\Argument $this
129 * @api
130 */
131 public function setShortName($shortName) {
132 if ($shortName !== NULL && (!is_string($shortName) || strlen($shortName) !== 1)) {
133 throw new \InvalidArgumentException('$shortName must be a single character or NULL', 1195824959);
134 }
135 $this->shortName = $shortName;
136 return $this;
137 }
138
139 /**
140 * Returns the short name of this argument
141 *
142 * @return string This argument's short name
143 * @api
144 */
145 public function getShortName() {
146 return $this->shortName;
147 }
148
149 /**
150 * Returns the data type of this argument's value
151 *
152 * @return string The data type
153 * @api
154 */
155 public function getDataType() {
156 return $this->dataType;
157 }
158
159 /**
160 * Marks this argument to be required
161 *
162 * @param bool $required TRUE if this argument should be required
163 * @return \TYPO3\CMS\Extbase\Mvc\Controller\Argument $this
164 * @api
165 */
166 public function setRequired($required) {
167 $this->isRequired = (bool)$required;
168 return $this;
169 }
170
171 /**
172 * Returns TRUE if this argument is required
173 *
174 * @return bool TRUE if this argument is required
175 * @api
176 */
177 public function isRequired() {
178 return $this->isRequired;
179 }
180
181 /**
182 * Sets the default value of the argument
183 *
184 * @param mixed $defaultValue Default value
185 * @return \TYPO3\CMS\Extbase\Mvc\Controller\Argument $this
186 * @api
187 */
188 public function setDefaultValue($defaultValue) {
189 $this->defaultValue = $defaultValue;
190 return $this;
191 }
192
193 /**
194 * Returns the default value of this argument
195 *
196 * @return mixed The default value
197 * @api
198 */
199 public function getDefaultValue() {
200 return $this->defaultValue;
201 }
202
203 /**
204 * Sets a custom validator which is used supplementary to the base validation
205 *
206 * @param \TYPO3\CMS\Extbase\Validation\Validator\ValidatorInterface $validator The actual validator object
207 * @return \TYPO3\CMS\Extbase\Mvc\Controller\Argument Returns $this (used for fluent interface)
208 * @api
209 */
210 public function setValidator(\TYPO3\CMS\Extbase\Validation\Validator\ValidatorInterface $validator) {
211 $this->validator = $validator;
212 return $this;
213 }
214
215 /**
216 * Returns the set validator
217 *
218 * @return \TYPO3\CMS\Extbase\Validation\Validator\ValidatorInterface The set validator, NULL if none was set
219 * @api
220 */
221 public function getValidator() {
222 return $this->validator;
223 }
224
225 /**
226 * Sets the value of this argument.
227 *
228 * @param mixed $rawValue The value of this argument
229 * @return \TYPO3\CMS\Extbase\Mvc\Controller\Argument
230 * @throws \TYPO3\CMS\Extbase\Mvc\Exception\InvalidArgumentValueException if the argument is not a valid object of type $dataType
231 */
232 public function setValue($rawValue) {
233 if ($rawValue === NULL) {
234 $this->value = NULL;
235 return $this;
236 }
237 if (is_object($rawValue) && $rawValue instanceof $this->dataType) {
238 $this->value = $rawValue;
239 return $this;
240 }
241 $this->value = $this->propertyMapper->convert($rawValue, $this->dataType, $this->propertyMappingConfiguration);
242 $this->validationResults = $this->propertyMapper->getMessages();
243 if ($this->validator !== NULL) {
244 // @todo Validation API has also changed!!!
245 $validationMessages = $this->validator->validate($this->value);
246 $this->validationResults->merge($validationMessages);
247 }
248 return $this;
249 }
250
251 /**
252 * Returns the value of this argument
253 *
254 * @return object The value of this argument - if none was set, NULL is returned
255 * @api
256 */
257 public function getValue() {
258 if ($this->value === NULL) {
259 return $this->defaultValue;
260 } else {
261 return $this->value;
262 }
263 }
264
265 /**
266 * Return the Property Mapping Configuration used for this argument; can be used by the initialize*action to modify the Property Mapping.
267 *
268 * @return \TYPO3\CMS\Extbase\Mvc\Controller\MvcPropertyMappingConfiguration
269 * @api
270 */
271 public function getPropertyMappingConfiguration() {
272 return $this->propertyMappingConfiguration;
273 }
274
275 /**
276 * @return bool TRUE if the argument is valid, FALSE otherwise
277 * @api
278 */
279 public function isValid() {
280 return !$this->validationResults->hasErrors();
281 }
282
283 /**
284 * @return \TYPO3\CMS\Extbase\Error\Result Validation errors which have occurred.
285 * @api
286 */
287 public function getValidationResults() {
288 return $this->validationResults;
289 }
290
291 /**
292 * Returns a string representation of this argument's value
293 *
294 * @return string
295 * @api
296 */
297 public function __toString() {
298 return (string)$this->value;
299 }
300
301 }