Completed
Push — master ( 38f43c...d78f52 )
by
unknown
14:23
created

Argument::getValue()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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