1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Yii\DataView\Helper; |
6
|
|
|
|
7
|
|
|
use InvalidArgumentException; |
8
|
|
|
|
9
|
|
|
final class Attribute |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* Generates an appropriate input name for the specified attribute name or expression. |
13
|
|
|
* |
14
|
|
|
* This method generates a name that can be used as the input name to collect user input for the specified |
15
|
|
|
* attribute. The name is generated according to the of the form and the given attribute name. For example, if the |
16
|
|
|
* form name of the `Post` form is `Post`, then the input name generated for the `content` attribute would be |
17
|
|
|
* `Post[content]`. |
18
|
|
|
* |
19
|
|
|
* @param string $formName The form name. |
20
|
|
|
* @param string $attribute The attribute name or expression. |
21
|
|
|
* |
22
|
|
|
* @throws InvalidArgumentException If the attribute name contains non-word characters or empty form name for |
23
|
|
|
* tabular inputs |
24
|
|
|
*/ |
25
|
26 |
|
public static function getInputName(string $formName, string $attribute): string |
26
|
|
|
{ |
27
|
26 |
|
$data = self::parseAttribute($attribute); |
28
|
|
|
|
29
|
25 |
|
if ($formName === '' && $data['prefix'] === '') { |
30
|
9 |
|
return $attribute; |
31
|
|
|
} |
32
|
|
|
|
33
|
16 |
|
if ($formName !== '') { |
34
|
15 |
|
return $formName . $data['prefix'] . '[' . $data['name'] . ']' . $data['suffix']; |
35
|
|
|
} |
36
|
|
|
|
37
|
1 |
|
throw new InvalidArgumentException('The form name cannot be empty for tabular inputs.'); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* This method parses an attribute expression and returns an associative array containing real attribute name, |
42
|
|
|
* prefix and suffix. |
43
|
|
|
* |
44
|
|
|
* For example: `['name' => 'content', 'prefix' => '', 'suffix' => '[0]']` |
45
|
|
|
* |
46
|
|
|
* An attribute expression is an attribute name prefixed and/or suffixed with array indexes. It is mainly used in |
47
|
|
|
* tabular data input and/or input of array type. Below are some examples: |
48
|
|
|
* |
49
|
|
|
* - `[0]content` is used in tabular data input to represent the "content" attribute for the first model in tabular |
50
|
|
|
* input; |
51
|
|
|
* - `dates[0]` represents the first array element of the "dates" attribute; |
52
|
|
|
* - `[0]dates[0]` represents the first array element of the "dates" attribute for the first model in tabular |
53
|
|
|
* input. |
54
|
|
|
* |
55
|
|
|
* @param string $attribute The attribute name or expression |
56
|
|
|
* |
57
|
|
|
* @throws InvalidArgumentException If the attribute name contains non-word characters. |
58
|
|
|
* |
59
|
|
|
* @psalm-return string[] |
60
|
|
|
*/ |
61
|
26 |
|
private static function parseAttribute(string $attribute): array |
62
|
|
|
{ |
63
|
26 |
|
if (!preg_match('/(^|.*\])([\w\.\+\-_]+)(\[.*|$)/u', $attribute, $matches)) { |
64
|
1 |
|
throw new InvalidArgumentException('Attribute name must contain word characters only.'); |
65
|
|
|
} |
66
|
|
|
|
67
|
25 |
|
return ['name' => $matches[2], 'prefix' => $matches[1], 'suffix' => $matches[3]]; |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|