1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Nip\Form\Traits; |
4
|
|
|
|
5
|
|
|
use Nip\Form\Elements\AbstractElement; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Trait HasElementsTrait |
9
|
|
|
* @package Nip\Form\Traits |
10
|
|
|
*/ |
11
|
|
|
trait HasElementsTrait |
12
|
|
|
{ |
13
|
|
|
protected $_elements = []; |
14
|
|
|
protected $_elementsLabel; |
15
|
|
|
protected $_elementsOrder = []; |
16
|
|
|
|
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @param AbstractElement $element |
20
|
|
|
* @return $this |
21
|
|
|
*/ |
22
|
4 |
|
public function addElement(AbstractElement $element) |
23
|
|
|
{ |
24
|
4 |
|
$name = $element->getUniqueId(); |
25
|
4 |
|
$this->_elements[$name] = $element; |
26
|
4 |
|
$this->_elementsLabel[$element->getLabel()] = $name; |
27
|
4 |
|
$this->_elementsOrder[] = $name; |
28
|
|
|
|
29
|
4 |
|
return $this; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param $name |
34
|
|
|
* @return $this |
35
|
|
|
*/ |
36
|
|
|
public function removeElement($name) |
37
|
|
|
{ |
38
|
|
|
unset($this->_elements[$name]); |
39
|
|
|
|
40
|
|
|
$key = array_search($name, $this->_elementsOrder); |
41
|
|
|
if ($key) { |
42
|
|
|
unset($this->_elementsOrder[$key]); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
return $this; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param $name |
50
|
|
|
* @return AbstractElement |
51
|
|
|
*/ |
52
|
3 |
|
public function getElement($name) |
53
|
|
|
{ |
54
|
3 |
|
if (array_key_exists($name, $this->_elements)) { |
55
|
3 |
|
return $this->_elements[$name]; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
return null; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @return AbstractElement[] |
63
|
|
|
*/ |
64
|
1 |
|
public function getElements() |
65
|
|
|
{ |
66
|
1 |
|
$return = []; |
67
|
1 |
|
foreach ($this->_elementsOrder as $current) { |
68
|
1 |
|
$return[$current] = $this->_elements[$current]; |
69
|
|
|
} |
70
|
|
|
|
71
|
1 |
|
return $return; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param $name |
76
|
|
|
* @return bool |
77
|
|
|
*/ |
78
|
|
|
public function hasElement($name) |
79
|
|
|
{ |
80
|
|
|
return array_key_exists($name, $this->_elements); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param $label |
85
|
|
|
* @return AbstractElement |
86
|
|
|
*/ |
87
|
|
|
public function getElementByLabel($label) |
88
|
|
|
{ |
89
|
|
|
if (array_key_exists($label, $this->_elementsLabel)) { |
90
|
|
|
return $this->_elements[$this->_elementsLabel[$label]]; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
return null; |
94
|
|
|
} |
95
|
|
|
/** |
96
|
|
|
* @param $element |
97
|
|
|
* @param $neighbour |
98
|
|
|
* @param string $type |
99
|
|
|
* @return $this |
100
|
|
|
*/ |
101
|
|
|
public function setElementOrder($element, $neighbour, $type = 'bellow') |
102
|
|
|
{ |
103
|
|
|
if (in_array($element, $this->_elementsOrder) && in_array($neighbour, $this->_elementsOrder)) { |
104
|
|
|
$newOrder = []; |
105
|
|
|
foreach ($this->_elementsOrder as $current) { |
106
|
|
|
if ($current == $element) { |
107
|
|
|
} elseif ($current == $neighbour) { |
108
|
|
|
if ($type == 'above') { |
109
|
|
|
$newOrder[] = $element; |
110
|
|
|
$newOrder[] = $neighbour; |
111
|
|
|
} else { |
112
|
|
|
$newOrder[] = $neighbour; |
113
|
|
|
$newOrder[] = $element; |
114
|
|
|
} |
115
|
|
|
} else { |
116
|
|
|
$newOrder[] = $current; |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
$this->_elementsOrder = $newOrder; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
return $this; |
123
|
|
|
} |
124
|
|
|
/** |
125
|
|
|
* @param bool $params |
126
|
|
|
* @return array |
127
|
|
|
*/ |
128
|
|
|
public function findElements($params = false) |
129
|
|
|
{ |
130
|
|
|
$elements = []; |
131
|
|
|
foreach ($this->getElements() as $element) { |
132
|
|
|
if (isset($params['type'])) { |
133
|
|
|
if ($element->getType() != $params['type']) { |
134
|
|
|
continue; |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
if (isset($params['attribs']) && is_array($params['attribs'])) { |
138
|
|
|
foreach ($params['attribs'] as $name => $value) { |
139
|
|
|
if ($element->getAttrib($name) != $value) { |
|
|
|
|
140
|
|
|
continue(2); |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
$elements[$element->getUniqueId()] = $element; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
return $elements; |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|
This check looks for function or method calls that always return null and whose return value is used.
The method
getObject()
can return nothing but null, so it makes no sense to use the return value.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.