1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ProyectoTAU\TAU\Common; |
4
|
|
|
|
5
|
|
|
trait PropertiesBag |
6
|
|
|
{ |
7
|
|
|
private $attributes = []; |
8
|
|
|
private $data = []; |
9
|
|
|
|
10
|
100 |
|
protected function setPropertiesBag($attributes) |
11
|
|
|
{ |
12
|
100 |
|
$this->attributes = $attributes; |
13
|
100 |
|
} |
14
|
|
|
|
15
|
100 |
|
protected function getPropertiesBag() |
16
|
|
|
{ |
17
|
100 |
|
return $this->attributes; |
18
|
|
|
} |
19
|
|
|
|
20
|
6 |
|
protected function propertiesBagToString(): string |
21
|
|
|
{ |
22
|
6 |
|
$s = ''; |
23
|
6 |
|
foreach ($this->attributes as $attr) |
24
|
|
|
{ |
25
|
6 |
|
$s .= $attr . ': ' . $this->$attr . "\n"; |
26
|
|
|
} |
27
|
6 |
|
return $s; |
28
|
|
|
} |
29
|
|
|
|
30
|
108 |
|
protected function isPropertyAllowed($name): bool |
31
|
|
|
{ |
32
|
108 |
|
return in_array($name, $this->attributes, true); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param $name |
37
|
|
|
* @return bool |
38
|
|
|
*/ |
39
|
105 |
|
protected function isDefined($name): bool |
40
|
|
|
{ |
41
|
105 |
|
return array_key_exists($name, $this->data); |
42
|
|
|
} |
43
|
|
|
|
44
|
100 |
|
public function __set($name, $value) |
45
|
|
|
{ |
46
|
100 |
|
if( ! $this->isPropertyAllowed($name) ) { |
47
|
|
|
$trace = debug_backtrace(); |
48
|
|
|
trigger_error( |
49
|
|
|
'Disallowed property ' . $name . |
50
|
|
|
' in ' . $trace[0]['file'] . |
51
|
|
|
' on line ' . $trace[0]['line'], |
52
|
|
|
E_USER_NOTICE); |
53
|
|
|
return null; |
54
|
|
|
} |
55
|
|
|
|
56
|
100 |
|
$this->data[$name] = $value; |
57
|
100 |
|
} |
58
|
|
|
|
59
|
105 |
|
public function __get($name) |
60
|
|
|
{ |
61
|
105 |
|
if( ! $this->isPropertyAllowed($name) ) { |
62
|
|
|
$trace = debug_backtrace(); |
63
|
|
|
trigger_error( |
64
|
|
|
'Disallowed property ' . $name . |
65
|
|
|
' in ' . $trace[0]['file'] . |
66
|
|
|
' on line ' . $trace[0]['line'], |
67
|
|
|
E_USER_NOTICE); |
68
|
|
|
return null; |
69
|
|
|
} |
70
|
|
|
|
71
|
105 |
|
if( ! $this->isDefined($name)) { |
72
|
|
|
$trace = debug_backtrace(); |
73
|
|
|
trigger_error( |
74
|
|
|
'Undefined property ' . $name . |
75
|
|
|
' in ' . $trace[0]['file'] . |
76
|
|
|
' on line ' . $trace[0]['line'], |
77
|
|
|
E_USER_NOTICE); |
78
|
|
|
return null; |
79
|
|
|
} |
80
|
|
|
|
81
|
105 |
|
return $this->data[$name]; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function __isset($name) |
85
|
|
|
{ |
86
|
|
|
if( ! $this->isPropertyAllowed($name) ) { |
87
|
|
|
$trace = debug_backtrace(); |
88
|
|
|
trigger_error( |
89
|
|
|
'Disallowed property ' . $name . |
90
|
|
|
' in ' . $trace[0]['file'] . |
91
|
|
|
' on line ' . $trace[0]['line'], |
92
|
|
|
E_USER_NOTICE); |
93
|
|
|
return null; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
if( ! $this->isDefined($name)) { |
97
|
|
|
$trace = debug_backtrace(); |
98
|
|
|
trigger_error( |
99
|
|
|
'Undefined property ' . $name . |
100
|
|
|
' in ' . $trace[0]['file'] . |
101
|
|
|
' on line ' . $trace[0]['line'], |
102
|
|
|
E_USER_NOTICE); |
103
|
|
|
return null; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
return isset($this->data[$name]); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
public function __unset($name) |
110
|
|
|
{ |
111
|
|
|
if( ! $this->isPropertyAllowed($name) ) { |
112
|
|
|
$trace = debug_backtrace(); |
113
|
|
|
trigger_error( |
114
|
|
|
'Disallowed property ' . $name . |
115
|
|
|
' in ' . $trace[0]['file'] . |
116
|
|
|
' on line ' . $trace[0]['line'], |
117
|
|
|
E_USER_NOTICE); |
118
|
|
|
return null; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
if( ! $this->isDefined($name)) { |
122
|
|
|
$trace = debug_backtrace(); |
123
|
|
|
trigger_error( |
124
|
|
|
'Undefined property ' . $name . |
125
|
|
|
' in ' . $trace[0]['file'] . |
126
|
|
|
' on line ' . $trace[0]['line'], |
127
|
|
|
E_USER_NOTICE); |
128
|
|
|
return null; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
unset($this->data[$name]); |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|