1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* MslsGetSet |
4
|
|
|
* @author Dennis Ploetner <[email protected]> |
5
|
|
|
* @since 0.9.8 |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace lloc\Msls; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Generic class for overloading properties |
12
|
|
|
* |
13
|
|
|
* @example https://gist.githubusercontent.com/lloc/2c232cef3f910acf692f/raw/f4eb70f4b1f8dc90c212d85d65af40c6604a32b9/MslsGetSet.php |
14
|
|
|
* |
15
|
|
|
* @package Msls |
16
|
|
|
*/ |
17
|
|
|
class MslsGetSet extends MslsRegistryInstance { |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Generic container for all properties of an instance |
21
|
|
|
* @var array $arr |
22
|
|
|
*/ |
23
|
|
|
protected $arr = []; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Overloads the set method. |
27
|
|
|
* |
28
|
|
|
* @param string $key |
29
|
|
|
* @param mixed $value |
30
|
|
|
*/ |
31
|
|
|
public function __set( $key, $value ) { |
32
|
|
|
$this->arr[ $key ] = $value; |
33
|
|
|
|
34
|
|
|
if ( empty( $this->arr[ $key ] ) ) { |
35
|
|
|
unset( $this->arr[ $key ] ); |
36
|
|
|
} |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Overloads the get method. |
41
|
|
|
* |
42
|
|
|
* @param string $key |
43
|
|
|
* @return mixed |
44
|
|
|
*/ |
45
|
|
|
public function __get( $key ) { |
46
|
|
|
return isset( $this->arr[ $key ] ) ? $this->arr[ $key ] : null; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Overloads the isset method. |
51
|
|
|
* |
52
|
|
|
* @param string $key |
53
|
|
|
* @return bool |
54
|
|
|
*/ |
55
|
|
|
public function __isset( $key ) { |
56
|
|
|
return isset( $this->arr[ $key ] ); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Overloads the unset method. |
61
|
|
|
* |
62
|
|
|
* @param string $key |
63
|
|
|
*/ |
64
|
|
|
public function __unset( $key ) { |
65
|
|
|
if ( isset( $this->arr[ $key ] ) ) { |
66
|
|
|
unset( $this->arr[ $key ] ); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Resets the properties container to an empty array. |
72
|
|
|
* |
73
|
|
|
* @return MslsGetSet |
74
|
|
|
*/ |
75
|
|
|
public function reset() { |
76
|
|
|
$this->arr = []; |
77
|
|
|
|
78
|
|
|
return $this; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Checks if the array has an non empty item with the specified key name. |
83
|
|
|
* |
84
|
|
|
* This is method is similar to the overloaded __isset-method since |
85
|
|
|
* __set cleans empty properties but I use for example |
86
|
|
|
* |
87
|
|
|
* $obj->has_value( $temp ) |
88
|
|
|
* |
89
|
|
|
* and not |
90
|
|
|
* |
91
|
|
|
* isset( $obj->$temp ) |
92
|
|
|
* |
93
|
|
|
* which is the same but in my opinion a little bit ugly. |
94
|
|
|
* |
95
|
|
|
* @param string $key |
96
|
|
|
* @return bool |
97
|
|
|
*/ |
98
|
|
|
public function has_value( $key ) { |
99
|
|
|
return ! empty( $this->arr[ $key ] ); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Checks if the properties-container is empty. |
104
|
|
|
* |
105
|
|
|
* @return bool |
106
|
|
|
*/ |
107
|
|
|
public function is_empty() { |
108
|
|
|
return empty( $this->arr ); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Gets the complete properties-container as an array. |
113
|
|
|
* |
114
|
|
|
* @return array |
115
|
|
|
*/ |
116
|
|
|
public function get_arr() { |
117
|
|
|
return $this->arr; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
} |
121
|
|
|
|