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