|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* the abstract base model |
|
4
|
|
|
*/ |
|
5
|
|
|
namespace Phile\Model; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Abstract model which implements the ArrayAccess interface |
|
9
|
|
|
* |
|
10
|
|
|
* @author Frank Nägler |
|
11
|
|
|
* @link https://philecms.com |
|
12
|
|
|
* @license http://opensource.org/licenses/MIT |
|
13
|
|
|
* @package Phile\Model |
|
14
|
|
|
*/ |
|
15
|
|
|
class AbstractModel implements \ArrayAccess |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* @var array the storage |
|
19
|
|
|
*/ |
|
20
|
|
|
protected $data = array(); |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* get value for given key |
|
24
|
|
|
* |
|
25
|
|
|
* @param $key |
|
26
|
|
|
* |
|
27
|
|
|
* @return null|mixed |
|
28
|
|
|
*/ |
|
29
|
9 |
|
public function get($key) |
|
30
|
|
|
{ |
|
31
|
9 |
|
return (isset($this->data[$key])) ? $this->data[$key] : null; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* get all entries |
|
36
|
|
|
* |
|
37
|
|
|
* @return array |
|
38
|
|
|
*/ |
|
39
|
1 |
|
public function getAll() |
|
40
|
|
|
{ |
|
41
|
1 |
|
return $this->data; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* set value for given key |
|
46
|
|
|
* |
|
47
|
|
|
* @param string $key the key |
|
48
|
|
|
* @param mixed $value the value |
|
49
|
|
|
*/ |
|
50
|
18 |
|
public function set($key, $value) |
|
51
|
|
|
{ |
|
52
|
18 |
|
$this->data[$key] = $value; |
|
53
|
18 |
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* magic method to get value |
|
57
|
|
|
* |
|
58
|
|
|
* @param $name |
|
59
|
|
|
* |
|
60
|
|
|
* @return null|mixed |
|
61
|
|
|
*/ |
|
62
|
|
|
public function __get($name) |
|
63
|
|
|
{ |
|
64
|
|
|
return $this->get($name); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* magic method to set value |
|
69
|
|
|
* |
|
70
|
|
|
* @param string $name |
|
71
|
|
|
* @param mixed $value |
|
72
|
|
|
*/ |
|
73
|
|
|
public function __set($name, $value) |
|
74
|
|
|
{ |
|
75
|
|
|
$this->set($name, $value); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* magic method to access properties by getter / setter |
|
80
|
|
|
* |
|
81
|
|
|
* @param string $name the name of method |
|
82
|
|
|
* @param array $args the arguments of the method |
|
83
|
|
|
* |
|
84
|
|
|
* @return mixed|null|void |
|
85
|
|
|
*/ |
|
86
|
1 |
|
public function __call($name, $args) |
|
87
|
|
|
{ |
|
88
|
1 |
|
if (strpos($name, 'get') !== false) { |
|
89
|
|
|
$name = substr($name, 3); |
|
90
|
|
|
|
|
91
|
|
|
return $this->get($name); |
|
92
|
|
|
} |
|
93
|
1 |
|
if (strpos($name, 'set') !== false) { |
|
94
|
|
|
$name = substr($name, 3); |
|
95
|
|
|
|
|
96
|
|
|
return $this->set($name, $args[0]); |
|
97
|
|
|
} |
|
98
|
1 |
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* (PHP 5 >= 5.0.0) |
|
102
|
|
|
* Whether a offset exists |
|
103
|
|
|
* |
|
104
|
|
|
* @link http://php.net/manual/en/arrayaccess.offsetexists.php |
|
105
|
|
|
* |
|
106
|
|
|
* @param mixed $offset |
|
107
|
|
|
* An offset to check for. |
|
108
|
|
|
* |
|
109
|
|
|
* @return boolean true on success or false on failure. |
|
110
|
|
|
* The return value will be casted to boolean if non-boolean was returned. |
|
111
|
|
|
*/ |
|
112
|
1 |
|
public function offsetExists($offset) |
|
113
|
|
|
{ |
|
114
|
1 |
|
return (isset($this->data[$offset])); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* (PHP 5 >= 5.0.0) |
|
119
|
|
|
* Offset to retrieve |
|
120
|
|
|
* |
|
121
|
|
|
* @link http://php.net/manual/en/arrayaccess.offsetget.php |
|
122
|
|
|
* |
|
123
|
|
|
* @param mixed $offset |
|
124
|
|
|
* The offset to retrieve. |
|
125
|
|
|
* |
|
126
|
|
|
* @return mixed Can return all value types. |
|
127
|
|
|
*/ |
|
128
|
2 |
|
public function offsetGet($offset) |
|
129
|
|
|
{ |
|
130
|
2 |
|
return $this->get($offset); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* (PHP 5 >= 5.0.0) |
|
135
|
|
|
* Offset to set |
|
136
|
|
|
* |
|
137
|
|
|
* @link http://php.net/manual/en/arrayaccess.offsetset.php |
|
138
|
|
|
* |
|
139
|
|
|
* @param mixed $offset |
|
140
|
|
|
* The offset to assign the value to. |
|
141
|
|
|
* @param mixed $value |
|
142
|
|
|
* The value to set. |
|
143
|
|
|
* |
|
144
|
|
|
* @return void |
|
145
|
|
|
*/ |
|
146
|
|
|
public function offsetSet($offset, $value) |
|
147
|
|
|
{ |
|
148
|
|
|
$this->set($offset, $value); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* (PHP 5 >= 5.0.0) |
|
153
|
|
|
* Offset to unset |
|
154
|
|
|
* |
|
155
|
|
|
* @link http://php.net/manual/en/arrayaccess.offsetunset.php |
|
156
|
|
|
* |
|
157
|
|
|
* @param mixed $offset |
|
158
|
|
|
* The offset to unset. |
|
159
|
|
|
* |
|
160
|
|
|
* @return void |
|
161
|
|
|
*/ |
|
162
|
|
|
public function offsetUnset($offset) |
|
163
|
|
|
{ |
|
164
|
|
|
if ($this->offsetExists($offset)) { |
|
165
|
|
|
unset($this->data[$offset]); |
|
166
|
|
|
} |
|
167
|
|
|
} |
|
168
|
|
|
} |
|
169
|
|
|
|