|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* The MIT License |
|
5
|
|
|
* |
|
6
|
|
|
* Copyright 2020 Enjoys. |
|
7
|
|
|
* |
|
8
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
|
9
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
|
10
|
|
|
* in the Software without restriction, including without limitation the rights |
|
11
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
12
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
|
13
|
|
|
* furnished to do so, subject to the following conditions: |
|
14
|
|
|
* |
|
15
|
|
|
* The above copyright notice and this permission notice shall be included in |
|
16
|
|
|
* all copies or substantial portions of the Software. |
|
17
|
|
|
* |
|
18
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
19
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
20
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
21
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
22
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
23
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
|
24
|
|
|
* THE SOFTWARE. |
|
25
|
|
|
*/ |
|
26
|
|
|
|
|
27
|
|
|
declare(strict_types=1); |
|
28
|
|
|
|
|
29
|
|
|
namespace Enjoys\Forms\Traits; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* |
|
33
|
|
|
* @author Enjoys |
|
34
|
|
|
*/ |
|
35
|
|
|
trait Fill |
|
36
|
|
|
{ |
|
37
|
|
|
|
|
38
|
|
|
private array $elements = []; |
|
39
|
|
|
private string $parentName = ''; |
|
40
|
|
|
// private bool $parent = true; |
|
41
|
|
|
|
|
42
|
|
|
public function setParentName(string $parentName): void |
|
43
|
|
|
{ |
|
44
|
|
|
$this->parentName = $parentName; |
|
45
|
|
|
// $this->parent = false; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
public function getParentName(): string |
|
49
|
|
|
{ |
|
50
|
|
|
return $this->parentName; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
// public function isParent(): bool |
|
54
|
|
|
// { |
|
55
|
|
|
// return $this->parent; |
|
56
|
|
|
// } |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @since 2.4.0 Изменен принцип установки value и id из индексированных массивов |
|
60
|
|
|
* т.е. [1,2] значения будут 1 и 2 сответсвенно, а не 0 и 1 как раньше. |
|
61
|
|
|
* Чтобы использовать число в качестве value отличное от title, необходимо |
|
62
|
|
|
* в массиве конуретно указать значение key. Например ["40 " => test] (обратите внимание на пробел). |
|
63
|
|
|
* Из-за того что php преобразует строки, содержащие целое число к int, приходится добавлять |
|
64
|
|
|
* пробел либо в начало, либо в конец ключа. В итоге пробелы в начале и в конце удаляются автоматически. |
|
65
|
|
|
* |
|
66
|
|
|
* @param array $data |
|
67
|
|
|
* @return $this |
|
68
|
|
|
*/ |
|
69
|
|
|
public function fill(array $data) |
|
70
|
|
|
{ |
|
71
|
|
|
|
|
72
|
|
|
foreach ($data as $value => $title) { |
|
73
|
|
|
$fillHandler = new \Enjoys\Forms\FillHandler($value, $title); |
|
74
|
|
|
|
|
75
|
|
|
$class = '\Enjoys\Forms\Elements\\' . \ucfirst($this->getType()); |
|
|
|
|
|
|
76
|
|
|
|
|
77
|
|
|
$element = new $class($fillHandler->getValue(), $fillHandler->getLabel()); |
|
78
|
|
|
$element->setParentName($this->getName()); |
|
|
|
|
|
|
79
|
|
|
$element->setAttributes($fillHandler->getAttributes(), 'fill'); |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @todo слишком много вложенности ифов. подумать как переделать |
|
83
|
|
|
*/ |
|
84
|
|
|
foreach ($element->getAttributes('fill') as $key => $value) { |
|
|
|
|
|
|
85
|
|
|
if (in_array($key, ['id', 'name', 'disabled', 'readonly'])) { |
|
86
|
|
|
if ($element->getAttribute($key, 'fill') !== false) { |
|
87
|
|
|
$element->setAttribute($key, $element->getAttribute($key, 'fill')); |
|
88
|
|
|
$element->removeAttribute($key, 'fill'); |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
|
|
94
|
|
|
$element->setDefault($this->defaults); |
|
95
|
|
|
|
|
96
|
|
|
$this->elements[] = $element; |
|
97
|
|
|
} |
|
98
|
|
|
return $this; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* |
|
103
|
|
|
* @return array |
|
104
|
|
|
*/ |
|
105
|
|
|
public function getElements(): array |
|
106
|
|
|
{ |
|
107
|
|
|
return $this->elements; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
// public function updateElement($key, \Enjoys\Forms\Element $element): void |
|
111
|
|
|
// { |
|
112
|
|
|
// if (array_key_exists($key, $this->elements)) { |
|
113
|
|
|
// $this->elements[$key] = $element; |
|
114
|
|
|
// } |
|
115
|
|
|
// } |
|
116
|
|
|
} |
|
117
|
|
|
|