1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Nip\View\Traits; |
6
|
|
|
|
7
|
|
|
use function array_key_exists; |
8
|
|
|
use function is_array; |
9
|
|
|
use function is_string; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Trait HasDataTrait. |
13
|
|
|
*/ |
14
|
|
|
trait HasDataTrait |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* Determine if a piece of data is bound. |
18
|
|
|
* |
19
|
|
|
* @param string $key |
20
|
|
|
*/ |
21
|
|
|
public function offsetExists($key): bool |
22
|
|
|
{ |
23
|
|
|
return array_key_exists($key, $this->data); |
|
|
|
|
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Get a piece of bound data to the view. |
28
|
|
|
* |
29
|
|
|
* @param string $key |
30
|
|
|
*/ |
31
|
|
|
public function offsetGet($key): mixed |
32
|
|
|
{ |
33
|
|
|
return $this->data[$key]; |
|
|
|
|
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Set a piece of data on the view. |
38
|
|
|
* |
39
|
|
|
* @param string $key |
40
|
|
|
* @param mixed $value |
41
|
|
|
*/ |
42
|
|
|
public function offsetSet($key, $value): void |
43
|
|
|
{ |
44
|
|
|
$this->with($key, $value); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Unset a piece of data from the view. |
49
|
|
|
* |
50
|
|
|
* @param string $key |
51
|
|
|
*/ |
52
|
|
|
public function offsetUnset($key): void |
53
|
|
|
{ |
54
|
|
|
unset($this->data[$key]); |
|
|
|
|
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @return mixed|null |
59
|
|
|
*/ |
60
|
|
|
public function &__get($key) |
61
|
|
|
{ |
62
|
1 |
|
$var = $this->get($key); |
63
|
|
|
|
64
|
1 |
|
return $var; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @return $this |
69
|
|
|
*/ |
70
|
|
|
public function __set($name, $value) |
71
|
|
|
{ |
72
|
|
|
return $this->set($name, $value); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param string $name |
77
|
|
|
* @param null $default |
|
|
|
|
78
|
|
|
* |
79
|
|
|
* @return mixed|null |
80
|
|
|
*/ |
81
|
|
|
public function get($name, $default = null) |
82
|
2 |
|
{ |
83
|
|
|
$data = $this->getData(); |
|
|
|
|
84
|
2 |
|
if (isset($data[$name])) { |
85
|
2 |
|
return $data[$name]; |
86
|
|
|
} else { |
87
|
|
|
return $default; |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @return bool |
93
|
|
|
*/ |
94
|
|
|
public function has(string $name) |
95
|
2 |
|
{ |
96
|
|
|
$data = $this->getData(); |
97
|
2 |
|
|
98
|
|
|
return isset($data[$name]); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @param null $value |
|
|
|
|
103
|
|
|
* |
104
|
|
|
* @return $this |
105
|
2 |
|
*/ |
106
|
|
|
public function with($key, $value = null) |
107
|
2 |
|
{ |
108
|
1 |
|
$data = (is_array($key)) ? $key : [$key => $value]; |
109
|
|
|
$this->addData($data); |
|
|
|
|
110
|
2 |
|
|
111
|
|
|
return $this; |
112
|
2 |
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @param string|array $key |
116
|
|
|
* @param mixed $value |
117
|
|
|
* |
118
|
|
|
* @return $this |
119
|
|
|
*/ |
120
|
|
|
public function set($key, $value) |
121
|
|
|
{ |
122
|
|
|
$this->data->add([$key => $value]); |
|
|
|
|
123
|
|
|
|
124
|
|
|
return $this; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @return bool |
129
|
|
|
*/ |
130
|
|
|
public function __isset($name) |
131
|
|
|
{ |
132
|
|
|
return isset($this->data[$name]); |
|
|
|
|
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
public function __unset($name) |
136
|
|
|
{ |
137
|
|
|
unset($this->data[$name]); |
|
|
|
|
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @param string $name |
142
|
|
|
* @param string $appended |
143
|
|
|
* |
144
|
|
|
* @return $this |
145
|
|
|
*/ |
146
|
|
|
public function append($name, $appended) |
147
|
|
|
{ |
148
|
|
|
$value = $this->has($name) ? $this->get($name) : ''; |
149
|
|
|
$value .= $appended; |
150
|
|
|
|
151
|
|
|
return $this->set($name, $value); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Assigns variables in bulk in the current scope. |
156
|
|
|
* |
157
|
|
|
* @param array $array |
158
|
|
|
* |
159
|
|
|
* @return $this |
160
|
|
|
*/ |
161
|
|
|
public function assign($array = []) |
162
|
2 |
|
{ |
163
|
|
|
foreach ($array as $key => $value) { |
164
|
2 |
|
if (is_string($key)) { |
165
|
|
|
$this->set($key, $value); |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
return $this; |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
|