1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: sjhc1170 |
5
|
|
|
* Date: 07/05/2018 |
6
|
|
|
* Time: 09:34 |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Iriven\Plugins\Form\Core\Libs; |
10
|
|
|
|
11
|
|
|
use \Iriven\Plugins\Form\Core\Interfaces\AttributesBuilderInterface; |
12
|
|
|
use \Iriven\Plugins\Form\Core\Libs\Traits\KeyNormalizer; |
13
|
|
|
|
14
|
|
|
class AttributesBuilder implements AttributesBuilderInterface |
15
|
|
|
{ |
16
|
|
|
use KeyNormalizer; |
17
|
|
|
|
18
|
|
|
private $attributes; |
19
|
|
|
private $ignore ; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* AttributesBuilder constructor. |
23
|
|
|
* |
24
|
|
|
* @param array $attributes |
25
|
|
|
* @param array $ignore |
26
|
|
|
*/ |
27
|
|
|
public function __construct(array $attributes, $ignore = []) |
28
|
|
|
{ |
29
|
|
|
$this->attributes = new Collection(); |
30
|
|
|
$this->ignore = new Collection(); |
31
|
|
|
if($attributes) |
|
|
|
|
32
|
|
|
{ |
33
|
|
|
is_array($attributes) or $attributes = array($attributes); |
34
|
|
|
$attributes = array_change_key_case($attributes,CASE_LOWER); |
35
|
|
|
$attributes = $this->array_map_keys([$this,'normalize'],$attributes); |
36
|
|
|
$this->attributes->add($attributes); |
37
|
|
|
} |
38
|
|
|
if($ignore) |
|
|
|
|
39
|
|
|
{ |
40
|
|
|
is_array($ignore) or $ignore = array($ignore); |
41
|
|
|
$ignore = array_map([$this,'normalize'],$ignore); |
42
|
|
|
$this->ignore->add($ignore); |
43
|
|
|
} |
44
|
|
|
return $this; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param $key |
49
|
|
|
* @return $this |
50
|
|
|
*/ |
51
|
|
|
public function createElementID($key) |
52
|
|
|
{ |
53
|
|
|
$key = ucfirst($this->normalize($key)); |
|
|
|
|
54
|
|
|
if(strpos($key,'input')!==0) |
55
|
|
|
$key = 'input'.$key; |
56
|
|
|
$this->set('id',$key); |
57
|
|
|
return $this; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param $token |
62
|
|
|
* @return $this |
63
|
|
|
*/ |
64
|
|
|
public function createFormID($token = null) |
65
|
|
|
{ |
66
|
|
|
$token or $token = microtime(true); |
67
|
|
|
$token = $this->normalize($token); |
68
|
|
|
if(strpos($token,'form-')!==0) |
|
|
|
|
69
|
|
|
$token = 'form-'.$token; |
|
|
|
|
70
|
|
|
$this->set('name',md5($token)); |
|
|
|
|
71
|
|
|
return $this; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param array $attributes |
76
|
|
|
* @return $this |
77
|
|
|
*/ |
78
|
|
|
public function add(array $attributes) |
79
|
|
|
{ |
80
|
|
|
is_array($attributes) or $attributes = array($attributes); |
81
|
|
|
$attributes = $this->array_map_keys([$this,'normalize'],$attributes); |
82
|
|
|
$this->attributes->add($attributes); |
83
|
|
|
return $this; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param $key |
88
|
|
|
* @param null $default |
|
|
|
|
89
|
|
|
* @return mixed |
90
|
|
|
*/ |
91
|
|
|
public function get($key,$default=null) |
92
|
|
|
{ |
93
|
|
|
$key = $this->normalize($key); |
94
|
|
|
return $this->attributes->get($key,$default); |
|
|
|
|
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @param $key |
99
|
|
|
* @return bool |
100
|
|
|
*/ |
101
|
|
|
public function has($key) |
102
|
|
|
{ |
103
|
|
|
$key = $this->normalize($key); |
104
|
|
|
return $this->attributes->has($key); |
|
|
|
|
105
|
|
|
} |
106
|
|
|
/** |
107
|
|
|
* @param $ignore |
108
|
|
|
* @return $this |
109
|
|
|
*/ |
110
|
|
|
public function Ignore($ignore) |
111
|
|
|
{ |
112
|
|
|
is_array($ignore) or $ignore = array($ignore); |
113
|
|
|
$ignore = array_merge($this->ignore->all(), array_map([$this,'normalize'],$ignore)); |
114
|
|
|
$this->ignore->replace($ignore); |
115
|
|
|
return $this; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @param $key |
120
|
|
|
* @return $this |
121
|
|
|
*/ |
122
|
|
|
public function remove($key) |
123
|
|
|
{ |
124
|
|
|
$key = $this->normalize($key); |
125
|
|
|
$this->attributes->remove($key); |
|
|
|
|
126
|
|
|
return $this; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @param $key |
131
|
|
|
* @param $value |
132
|
|
|
* @return $this |
133
|
|
|
*/ |
134
|
|
|
public function set($key,$value) |
135
|
|
|
{ |
136
|
|
|
$key = $this->normalize($key); |
137
|
|
|
$this->attributes->set($key,$value); |
|
|
|
|
138
|
|
|
return $this; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @return array |
143
|
|
|
*/ |
144
|
|
|
public function All() |
145
|
|
|
{ |
146
|
|
|
return $this->attributes->all(); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @return bool|null|string |
151
|
|
|
*/ |
152
|
|
|
public function RenderHtml() |
153
|
|
|
{ |
154
|
|
|
$output = []; |
155
|
|
|
if($attributes = $this->attributes->all()) |
156
|
|
|
{ |
157
|
|
|
AttributesMapper::filter($this); |
158
|
|
|
$filtered = array_keys($attributes); |
159
|
|
|
if($ignore = $this->ignore->all()) |
160
|
|
|
$filtered = array_diff($filtered, $ignore); |
161
|
|
|
$attributes = array_intersect_key($attributes, array_flip($filtered)); |
162
|
|
|
foreach($attributes as $key=>$value) |
163
|
|
|
{ |
164
|
|
|
if(is_array($value)): |
165
|
|
|
$output[]= $key.'="'.implode(' ',array_values($value)).'"'; |
166
|
|
|
else: |
167
|
|
|
if(is_numeric($key)) $output[]= $value; |
168
|
|
|
else $output[]= $key.'="'.$value.'"'; |
169
|
|
|
endif; |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
return count($output) ? ' ' . implode(' ', $output) : ''; |
173
|
|
|
} |
174
|
|
|
} |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.