1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Nip\View\Traits; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Trait HasDataTrait |
7
|
|
|
* @package Nip\View\Traits |
8
|
|
|
*/ |
9
|
|
|
trait HasDataTrait |
10
|
|
|
{ |
11
|
|
|
protected $data = []; |
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) |
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) |
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) |
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) |
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 |
|
return $this->data[$key]; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param $name |
69
|
|
|
* @param $value |
70
|
|
|
* @return $this |
71
|
|
|
*/ |
72
|
|
|
public function __set($name, $value) |
73
|
|
|
{ |
74
|
|
|
return $this->set($name, $value); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param string $name |
79
|
|
|
* @param null $default |
|
|
|
|
80
|
|
|
* @return mixed|null |
81
|
|
|
*/ |
82
|
2 |
|
public function get($name, $default = null) |
83
|
|
|
{ |
84
|
2 |
|
if ($this->has($name)) { |
85
|
2 |
|
return $this->data[$name]; |
86
|
|
|
} else { |
87
|
|
|
return $default; |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @param string $name |
93
|
|
|
* @return bool |
94
|
|
|
*/ |
95
|
2 |
|
public function has(string $name) |
96
|
|
|
{ |
97
|
2 |
|
return isset($this->data[$name]); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @param $key |
102
|
|
|
* @param null $value |
|
|
|
|
103
|
|
|
* @return $this |
104
|
|
|
*/ |
105
|
2 |
|
public function with($key, $value = null) |
106
|
|
|
{ |
107
|
2 |
|
if (is_array($key)) { |
108
|
1 |
|
$this->data = array_merge($this->data, $key); |
109
|
|
|
} else { |
110
|
2 |
|
$this->data[$key] = $value; |
111
|
|
|
} |
112
|
2 |
|
return $this; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @param string|array $key |
117
|
|
|
* @param mixed $value |
118
|
|
|
* @return $this |
119
|
|
|
*/ |
120
|
|
|
public function set($key, $value) |
121
|
|
|
{ |
122
|
|
|
$this->data[$key] = $value; |
123
|
|
|
|
124
|
|
|
return $this; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @param $name |
129
|
|
|
* @return bool |
130
|
|
|
*/ |
131
|
|
|
public function __isset($name) |
132
|
|
|
{ |
133
|
|
|
return isset($this->data[$name]); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @param $name |
138
|
|
|
*/ |
139
|
|
|
public function __unset($name) |
140
|
|
|
{ |
141
|
|
|
unset($this->data[$name]); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @param string $name |
146
|
|
|
* @param string $appended |
147
|
|
|
* @return $this |
148
|
|
|
*/ |
149
|
|
|
public function append($name, $appended) |
150
|
|
|
{ |
151
|
|
|
$value = $this->has($name) ? $this->get($name) : ''; |
152
|
|
|
$value .= $appended; |
153
|
|
|
|
154
|
|
|
return $this->set($name, $value); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Get the array of view data. |
159
|
|
|
* |
160
|
|
|
* @return array |
161
|
|
|
*/ |
162
|
2 |
|
public function getData() |
163
|
|
|
{ |
164
|
2 |
|
return $this->data; |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
|