1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @package domain |
4
|
|
|
* @subpackage models |
5
|
|
|
* @author marius orcsik <[email protected]> |
6
|
|
|
* @date 09.11.19 |
7
|
|
|
*/ |
8
|
|
|
namespace vsc\domain\models; |
9
|
|
|
|
10
|
|
|
use vsc\infrastructure\Base; |
11
|
|
|
use vsc\ExceptionUnimplemented; |
12
|
|
|
|
13
|
|
|
abstract class ModelA extends Base implements ModelInterface { |
14
|
|
|
use ArrayAccessTrait; |
15
|
|
|
use CountableTrait; |
16
|
|
|
use IteratorTrait; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @param string $sIncName |
20
|
|
|
* @return mixed |
21
|
|
|
*/ |
22
|
2 |
|
public function __get($sIncName) { |
23
|
|
|
try { |
24
|
2 |
|
$oProperty = new \ReflectionProperty($this, $sIncName); |
25
|
2 |
|
if (!$oProperty->isPublic()) { |
26
|
|
|
// try for a getter method |
27
|
1 |
|
$sGetterName = 'get' . ucfirst($sIncName); |
28
|
1 |
|
$oGetter = new \ReflectionMethod($this, $sGetterName); |
|
|
|
|
29
|
|
|
|
30
|
|
|
$this->_current = $sIncName; // ?? I wonder if setting the offset to the current read position is the right way |
31
|
|
|
return $oGetter->invoke($this, $sIncName); |
32
|
|
|
} else { |
33
|
1 |
|
$this->_current = $sIncName; // ?? I wonder if setting the offset to the current read position is the right way |
34
|
1 |
|
return $oProperty->getValue($this); |
35
|
|
|
} |
36
|
1 |
|
} catch (\ReflectionException $e) { |
37
|
|
|
// reflection issue |
38
|
1 |
|
return parent::__get($sIncName); |
39
|
|
|
} |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param string $sIncName |
44
|
|
|
* @param mixed $value |
45
|
|
|
* @throws ExceptionUnimplemented |
46
|
|
|
* @throws \ReflectionException |
47
|
|
|
*/ |
48
|
1 |
|
public function __set($sIncName, $value) { |
49
|
1 |
|
if (is_null($sIncName)) { |
50
|
|
|
throw new \ReflectionException('Can\'t set a value to a null property on the current object [' . get_class($this) . ']'); |
51
|
|
|
} |
52
|
|
|
try { |
53
|
1 |
|
$oProperty = new \ReflectionProperty($this, $sIncName); |
54
|
1 |
|
if (!$oProperty->isPublic()) { |
55
|
|
|
// trying for a setter |
56
|
|
|
$sSetterName = 'set' . ucfirst($sIncName); |
57
|
|
|
$oSetter = new \ReflectionMethod($this, $sSetterName); |
|
|
|
|
58
|
|
|
|
59
|
|
|
$oSetter->invoke($this, $value); |
60
|
|
|
} else { |
61
|
1 |
|
$oProperty->setValue($this, $value); |
62
|
|
|
} |
63
|
|
|
|
64
|
1 |
|
$this->_current = $sIncName; |
65
|
1 |
|
return; |
66
|
|
|
} catch (\ReflectionException $e) { |
67
|
|
|
// $this->_current = $sIncName; |
|
|
|
|
68
|
|
|
// $this->$sIncName = $value; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
parent::__set($sIncName, $value); |
72
|
|
|
} |
73
|
|
|
|
74
|
10 |
|
public function __construct() { |
75
|
10 |
|
$this->rewind(); |
76
|
10 |
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param bool $bAll |
80
|
|
|
* @return array |
81
|
|
|
*/ |
82
|
11 |
|
protected function getPropertyNames($bAll = false) { |
83
|
11 |
|
$aRet = array(); |
|
|
|
|
84
|
11 |
|
$oMirror = new \ReflectionObject($this); |
|
|
|
|
85
|
11 |
|
$aProperties = $oMirror->getProperties(); |
86
|
|
|
|
87
|
|
|
/* @var $oProperty \ReflectionProperty */ |
88
|
11 |
|
foreach ($aProperties as $oProperty) { |
89
|
|
|
// skip locally defined properties |
90
|
11 |
|
if ($oProperty->getDeclaringClass()->getName() == ModelA::class) { continue; } |
|
|
|
|
91
|
11 |
|
if ($bAll || (!$bAll && $oProperty->isPublic())) { |
92
|
11 |
|
$aRet[] = $oProperty->getName(); |
93
|
|
|
} |
94
|
|
|
} |
95
|
11 |
|
return $aRet; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @param bool $bIncludeProtected |
100
|
|
|
* @return array |
101
|
|
|
*/ |
102
|
2 |
|
protected function getProperties($bIncludeProtected = false) { |
103
|
2 |
|
$aRet = array(); |
|
|
|
|
104
|
2 |
|
$oMirror = new \ReflectionObject($this); |
|
|
|
|
105
|
2 |
|
$aProperties = $oMirror->getProperties(); |
106
|
|
|
|
107
|
|
|
/* @var $oProperty \ReflectionProperty */ |
108
|
2 |
|
foreach ($aProperties as $oProperty) { |
109
|
2 |
|
$sName = $oProperty->getName(); |
110
|
2 |
|
if ($oProperty->isPublic()) { |
111
|
2 |
|
$aRet[$sName] = $oProperty->getValue($this); |
112
|
2 |
|
} elseif ($bIncludeProtected) { |
113
|
1 |
|
$oProperty->setAccessible(true); |
114
|
2 |
|
$aRet[$sName] = $oProperty->getValue($this); |
115
|
|
|
} |
116
|
|
|
} |
117
|
2 |
|
return $aRet; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* recursively transform all properties into arrays |
122
|
|
|
*/ |
123
|
1 |
|
public function toArray() { |
124
|
1 |
|
$aRet = array(); |
|
|
|
|
125
|
1 |
|
$aProperties = $this->getProperties(); |
126
|
1 |
|
foreach ($aProperties as $sName => $oProperty) { |
127
|
1 |
|
if (ModelA::isValid($oProperty)) { |
|
|
|
|
128
|
|
|
/* @var ModelA $oProperty */ |
129
|
|
|
$aRet[$sName] = $oProperty->toArray(); |
130
|
1 |
|
} elseif (is_array($oProperty) || is_scalar($oProperty)) { |
131
|
1 |
|
$aRet[$sName] = $oProperty; |
132
|
1 |
|
} elseif (is_null($oProperty)) { |
133
|
1 |
|
$aRet[$sName] = $oProperty; |
134
|
|
|
} else { |
135
|
1 |
|
$aRet[$sName] = var_export($oProperty, true); |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|
139
|
1 |
|
return $aRet; |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.
To visualize
will produce issues in the first and second line, while this second example
will produce no issues.