Passed
Push — master ( c15889...08ecf6 )
by Sergei
03:03
created

FormModel::readAttributeValue()   B

Complexity

Conditions 7
Paths 7

Size

Total Lines 36
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 23
CRAP Score 7

Importance

Changes 0
Metric Value
cc 7
eloc 23
nc 7
nop 1
dl 0
loc 36
ccs 23
cts 23
cp 1
crap 7
rs 8.6186
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Form\YiisoftFormModel;
6
7
use InvalidArgumentException;
8
use ReflectionClass;
9
use ReflectionException;
10
use Yiisoft\Form\Exception\PropertyNotSupportNestedValuesException;
11
use Yiisoft\Form\Exception\StaticObjectPropertyException;
12
use Yiisoft\Form\Exception\UndefinedArrayElementException;
13
use Yiisoft\Form\Exception\UndefinedObjectPropertyException;
14
use Yiisoft\Form\Exception\ValueNotFoundException;
15
use Yiisoft\Hydrator\Validator\ValidatedInputTrait;
16
use Yiisoft\Strings\Inflector;
17
use Yiisoft\Strings\StringHelper;
18
19
use function array_key_exists;
20
use function array_slice;
21
use function is_array;
22
use function is_object;
23
use function str_contains;
24
use function strrchr;
25
use function substr;
26
27
/**
28
 * Form model represents an HTML form: its data, validation and presentation.
29
 */
30
abstract class FormModel implements FormModelInterface
31
{
32
    use ValidatedInputTrait;
33
34
    private const META_LABEL = 1;
35
    private const META_HINT = 2;
36
    private const META_PLACEHOLDER = 3;
37
38
    private static ?Inflector $inflector = null;
39
40
    /**
41
     * Returns the text hint for the specified attribute.
42
     *
43
     * @param string $attribute the attribute name.
44
     *
45
     * @return string the attribute hint.
46
     */
47 413
    public function getAttributeHint(string $attribute): string
48
    {
49 413
        return $this->readAttributeMetaValue(self::META_HINT, $attribute) ?? '';
50
    }
51
52
    /**
53
     * Returns the attribute hints.
54
     *
55
     * Attribute hints are mainly used for display purpose. For example, given an attribute `isPublic`, we can declare
56
     * a hint `Whether the post should be visible for not logged-in users`, which provides user-friendly description of
57
     * the attribute meaning and can be displayed to end users.
58
     *
59
     * Unlike label hint will not be generated, if its explicit declaration is omitted.
60
     *
61
     * Note, in order to inherit hints defined in the parent class, a child class needs to merge the parent hints with
62
     * child hints using functions such as `array_merge()`.
63
     *
64
     * @return array attribute hints (name => hint)
65
     *
66
     * @psalm-return array<string,string>
67
     */
68 102
    public function getAttributeHints(): array
69
    {
70 102
        return [];
71
    }
72
73
    /**
74
     * Returns the text label for the specified attribute.
75
     *
76
     * @param string $attribute The attribute name.
77
     *
78
     * @return string The attribute label.
79
     */
80 216
    public function getAttributeLabel(string $attribute): string
81
    {
82 216
        return $this->readAttributeMetaValue(self::META_LABEL, $attribute) ?? $this->generateAttributeLabel($attribute);
83
    }
84
85
    /**
86
     * Returns the attribute labels.
87
     *
88
     * Attribute labels are mainly used for display purpose. For example, given an attribute `firstName`, we can
89
     * declare a label `First Name` which is more user-friendly and can be displayed to end users.
90
     *
91
     * By default, an attribute label is generated automatically. This method allows you to
92
     * explicitly specify attribute labels.
93
     *
94
     * Note, in order to inherit labels defined in the parent class, a child class needs to merge the parent labels
95
     * with child labels using functions such as `array_merge()`.
96
     *
97
     * @return array attribute labels (name => label)
98
     *
99
     * {@see \Yiisoft\Form\FormModel::getAttributeLabel()}
100
     *
101
     * @psalm-return array<string,string>
102
     */
103 8
    public function getAttributeLabels(): array
104
    {
105 8
        return [];
106
    }
107
108
    /**
109
     * Returns the text placeholder for the specified attribute.
110
     *
111
     * @param string $attribute the attribute name.
112
     *
113
     * @return string the attribute placeholder.
114
     */
115 187
    public function getAttributePlaceholder(string $attribute): string
116
    {
117 187
        return $this->readAttributeMetaValue(self::META_PLACEHOLDER, $attribute) ?? '';
118
    }
119
120 393
    public function getAttributeValue(string $attribute): mixed
121
    {
122
        try {
123 393
            return $this->readAttributeValue($attribute);
124 8
        } catch (PropertyNotSupportNestedValuesException $exception) {
125 3
            return $exception->getValue() === null
126 2
                ? null
127 2
                : throw $exception;
128 5
        } catch (UndefinedArrayElementException) {
129 3
            return null;
130
        }
131
    }
132
133
    /**
134
     * Returns the attribute placeholders.
135
     *
136
     * @return array attribute placeholder (name => placeholder)
137
     *
138
     * @psalm-return array<string,string>
139
     */
140 123
    public function getAttributePlaceholders(): array
141
    {
142 123
        return [];
143
    }
144
145
    /**
146
     * Returns the form name that this model class should use.
147
     *
148
     * The form name is mainly used by {@see \Yiisoft\Form\InputData\HtmlForm} to determine how to name the input
149
     * fields for the attributes in a model.
150
     * If the form name is "A" and an attribute name is "b", then the corresponding input name would be "A[b]".
151
     * If the form name is an empty string, then the input name would be "b".
152
     *
153
     * The purpose of the above naming schema is that for forms which contain multiple different models, the attributes
154
     * of each model are grouped in sub-arrays of the POST-data, and it is easier to differentiate between them.
155
     *
156
     * By default, this method returns the model class name (without the namespace part) as the form name. You may
157
     * override it when the model is used in different forms.
158
     *
159
     * @return string The form name of this model class.
160
     */
161 424
    public function getFormName(): string
162
    {
163 424
        if (str_contains(static::class, '@anonymous')) {
164 3
            return '';
165
        }
166
167 422
        $className = strrchr(static::class, '\\');
168 422
        if ($className === false) {
169 1
            return static::class;
170
        }
171
172 421
        return substr($className, 1);
173
    }
174
175
    /**
176
     * If there is such attribute in the set.
177
     */
178 478
    public function hasAttribute(string $attribute): bool
179
    {
180
        try {
181 478
            $this->readAttributeValue($attribute);
182 7
        } catch (ValueNotFoundException) {
183 7
            return false;
184
        }
185 473
        return true;
186
    }
187
188
    public function isValid(): bool
189
    {
190
        return (bool) $this->getValidationResult()?->isValid();
191
    }
192
193
    /**
194
     * @throws UndefinedArrayElementException
195
     * @throws UndefinedObjectPropertyException
196
     * @throws StaticObjectPropertyException
197
     * @throws PropertyNotSupportNestedValuesException
198
     * @throws ValueNotFoundException
199
     */
200 499
    private function readAttributeValue(string $attribute): mixed
201
    {
202 499
        $path = $this->normalizePath($attribute);
203
204 499
        $value = $this;
205 499
        $keys = [[static::class, $this]];
206 499
        foreach ($path as $key) {
207 499
            $keys[] = [$key, $value];
208
209 499
            if (is_array($value)) {
210 5
                if (array_key_exists($key, $value)) {
211 2
                    $value = $value[$key];
212 2
                    continue;
213
                }
214 3
                throw new UndefinedArrayElementException($this->makePropertyPathString($keys));
215
            }
216
217 499
            if (is_object($value)) {
218 499
                $class = new ReflectionClass($value);
219
                try {
220 499
                    $property = $class->getProperty($key);
221 5
                } catch (ReflectionException) {
222 5
                    throw new UndefinedObjectPropertyException($this->makePropertyPathString($keys));
223
                }
224 498
                if ($property->isStatic()) {
225 1
                    throw new StaticObjectPropertyException($this->makePropertyPathString($keys));
226
                }
227 498
                $value = $property->getValue($value);
228 498
                continue;
229
            }
230
231 3
            array_pop($keys);
232 3
            throw new PropertyNotSupportNestedValuesException($this->makePropertyPathString($keys), $value);
233
        }
234
235 493
        return $value;
236
    }
237
238 450
    private function readAttributeMetaValue(int $metaKey, string $attribute): ?string
239
    {
240 450
        $path = $this->normalizePath($attribute);
241
242 450
        $value = $this;
243 450
        $n = 0;
244 450
        foreach ($path as $key) {
245 450
            if ($value instanceof self) {
246 450
                $nestedAttribute = implode('.', array_slice($path, $n));
247 450
                $data = match ($metaKey) {
248 450
                    self::META_LABEL => $value->getAttributeLabels(),
249 450
                    self::META_HINT => $value->getAttributeHints(),
250 450
                    self::META_PLACEHOLDER => $value->getAttributePlaceholders(),
251 450
                    default => throw new InvalidArgumentException('Invalid meta key.'),
252 450
                };
253 450
                if (array_key_exists($nestedAttribute, $data)) {
254 219
                    return $data[$nestedAttribute];
255
                }
256
            }
257
258 350
            $class = new ReflectionClass($value);
259
            try {
260 350
                $property = $class->getProperty($key);
261 3
            } catch (ReflectionException) {
262 3
                return null;
263
            }
264 347
            if ($property->isStatic()) {
265
                return null;
266
            }
267
268 347
            $value = $property->getValue($value);
269 347
            if (!is_object($value)) {
270 343
                return null;
271
            }
272
273 4
            $n++;
274
        }
275
276 1
        return null;
277
    }
278
279
    /**
280
     * Generates a user-friendly attribute label based on the give attribute name.
281
     *
282
     * This is done by replacing underscores, dashes and dots with blanks and changing the first letter of each word to
283
     * upper case.
284
     *
285
     * For example, 'department_name' or 'DepartmentName' will generate 'Department Name'.
286
     *
287
     * @param string $attribute The attribute name.
288
     *
289
     * @return string The attribute label.
290
     */
291 28
    private function generateAttributeLabel(string $attribute): string
292
    {
293 28
        if (self::$inflector === null) {
294 1
            self::$inflector = new Inflector();
295
        }
296
297 28
        return StringHelper::uppercaseFirstCharacterInEachWord(
298 28
            self::$inflector->toWords($attribute)
0 ignored issues
show
Bug introduced by
The method toWords() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

298
            self::$inflector->/** @scrutinizer ignore-call */ 
299
                              toWords($attribute)

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
299 28
        );
300
    }
301
302
    /**
303
     * @return string[]
304
     */
305 505
    private function normalizePath(string $attribute): array
306
    {
307 505
        $attribute = str_replace(['][', '['], '.', rtrim($attribute, ']'));
308 505
        return StringHelper::parsePath($attribute);
309
    }
310
311
    /**
312
     * @psalm-param array<array-key, array{0:int|string, 1:mixed}> $keys
313
     */
314 11
    private function makePropertyPathString(array $keys): string
315
    {
316 11
        $path = '';
317 11
        foreach ($keys as $key) {
318 11
            if ($path !== '') {
319 11
                if (is_object($key[1])) {
320 11
                    $path .= '::' . $key[0];
321 3
                } elseif (is_array($key[1])) {
322 11
                    $path .= '[' . $key[0] . ']';
323
                }
324
            } else {
325 11
                $path = (string) $key[0];
326
            }
327
        }
328 11
        return $path;
329
    }
330
}
331