1
|
|
|
<?php |
2
|
|
|
namespace PhpBoot\Utils; |
3
|
|
|
|
4
|
|
|
class ArrayAdaptor implements \ArrayAccess |
5
|
|
|
{ |
6
|
|
|
/** |
7
|
|
|
* ArrayAdaptor constructor. |
8
|
|
|
* @param object|array $obj |
9
|
|
|
*/ |
10
|
7 |
|
public function __construct(&$obj) |
11
|
|
|
{ |
12
|
7 |
|
$this->obj = &$obj; |
13
|
7 |
|
} |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Whether a offset exists |
17
|
|
|
* @link http://php.net/manual/en/arrayaccess.offsetexists.php |
18
|
|
|
* @param mixed $offset <p> |
19
|
|
|
* An offset to check for. |
20
|
|
|
* </p> |
21
|
|
|
* @return boolean true on success or false on failure. |
22
|
|
|
* </p> |
23
|
|
|
* <p> |
24
|
|
|
* The return value will be casted to boolean if non-boolean was returned. |
25
|
|
|
* @since 5.0.0 |
26
|
|
|
*/ |
27
|
5 |
|
public function offsetExists($offset) |
28
|
|
|
{ |
29
|
5 |
|
if(is_array($this->obj)){ |
30
|
2 |
|
return array_key_exists($offset, $this->obj); |
31
|
3 |
|
}elseif(self::hasProperty($this->obj, $offset)){ |
32
|
|
|
return true; |
33
|
3 |
|
}elseif(method_exists($this->obj, 'has')){ |
34
|
2 |
|
return $this->obj->has($offset); |
35
|
1 |
|
}elseif(method_exists($this->obj, $method = 'has'.ucfirst($offset))){ |
36
|
1 |
|
return $this->obj->{$method}($offset); |
37
|
|
|
}elseif(method_exists($this->obj, $method = 'get'.ucfirst($offset))){ |
38
|
|
|
return $this->obj->{$method}() !== null; |
39
|
|
|
}elseif(method_exists($this->obj, 'get')){ |
40
|
|
|
return $this->obj->get($offset) !== null; |
41
|
|
|
}else{ |
42
|
|
|
return false; |
43
|
|
|
} |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Offset to retrieve |
48
|
|
|
* @link http://php.net/manual/en/arrayaccess.offsetget.php |
49
|
|
|
* @param mixed $offset <p> |
50
|
|
|
* The offset to retrieve. |
51
|
|
|
* </p> |
52
|
|
|
* @return mixed Can return all value types. |
53
|
|
|
* @since 5.0.0 |
54
|
|
|
*/ |
55
|
6 |
|
public function offsetGet($offset) |
56
|
|
|
{ |
57
|
6 |
|
$res = null; |
58
|
6 |
View Code Duplication |
if(is_array($this->obj)){ |
|
|
|
|
59
|
2 |
|
$res = &$this->obj[$offset]; |
60
|
6 |
|
}elseif(self::hasProperty($this->obj, $offset)){ |
61
|
1 |
|
$res = &$this->obj->{$offset}; |
62
|
4 |
|
}elseif(method_exists($this->obj, 'get')){ |
63
|
2 |
|
$res = $this->obj->get($offset); |
64
|
3 |
|
}elseif(method_exists($this->obj, $method = 'get'.ucfirst($offset))){ |
65
|
1 |
|
$res = $this->obj->{$method}(); |
66
|
1 |
|
}else{ |
67
|
|
|
throw new \InvalidArgumentException("offsetGet($offset) failed"); |
68
|
|
|
} |
69
|
6 |
|
if(is_array($res) || is_object($res)){ |
70
|
1 |
|
return new self($res); |
71
|
|
|
} |
72
|
6 |
|
return $res; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Offset to set |
77
|
|
|
* @link http://php.net/manual/en/arrayaccess.offsetset.php |
78
|
|
|
* @param mixed $offset <p> |
79
|
|
|
* The offset to assign the value to. |
80
|
|
|
* </p> |
81
|
|
|
* @param mixed $value <p> |
82
|
|
|
* The value to set. |
83
|
|
|
* </p> |
84
|
|
|
* @return void |
85
|
|
|
* @since 5.0.0 |
86
|
|
|
*/ |
87
|
4 |
|
public function offsetSet($offset, $value) |
88
|
|
|
{ |
89
|
4 |
View Code Duplication |
if(is_array($this->obj)){ |
|
|
|
|
90
|
1 |
|
$this->obj[$offset] = $value; |
91
|
4 |
|
}elseif(self::hasProperty($this->obj, $offset)){ |
92
|
1 |
|
$this->obj->{$offset} = $value; |
93
|
3 |
|
}elseif(method_exists($this->obj, 'set')){ |
94
|
1 |
|
$this->obj->set($offset, $value); |
95
|
2 |
|
}elseif(method_exists($this->obj, $method = 'set'.ucfirst($offset))){ |
96
|
1 |
|
$this->obj->{$method}($value); |
97
|
1 |
|
}else{ |
98
|
|
|
throw new \BadMethodCallException("can not set $offset"); |
99
|
|
|
} |
100
|
4 |
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Offset to unset |
104
|
|
|
* @link http://php.net/manual/en/arrayaccess.offsetunset.php |
105
|
|
|
* @param mixed $offset <p> |
106
|
|
|
* The offset to unset. |
107
|
|
|
* </p> |
108
|
|
|
* @return void |
109
|
|
|
* @since 5.0.0 |
110
|
|
|
*/ |
111
|
3 |
|
public function offsetUnset($offset) |
112
|
|
|
{ |
113
|
3 |
|
if(is_array($this->obj)){ |
114
|
1 |
|
unset($this->obj[$offset]); |
115
|
3 |
|
}elseif(self::hasProperty($this->obj, $offset)){ |
116
|
|
|
unset($this->obj->{$offset}); |
117
|
2 |
|
}elseif(method_exists($this->obj, 'remove')){ |
118
|
1 |
|
$this->obj->remove($offset); |
119
|
2 |
|
}elseif(method_exists($this->obj, $method = 'remove'.ucfirst($offset))){ |
120
|
1 |
|
$this->obj->$method(); |
121
|
1 |
|
}else{ |
122
|
|
|
throw new \InvalidArgumentException("offsetUnset($offset) failed"); |
123
|
|
|
} |
124
|
3 |
|
} |
125
|
1 |
|
static public function strip($obj){ |
|
|
|
|
126
|
1 |
|
if($obj instanceof self){ |
127
|
1 |
|
return $obj->obj; |
128
|
|
|
} |
129
|
1 |
|
return $obj; |
130
|
|
|
} |
131
|
4 |
|
static function hasProperty($object, $name) |
|
|
|
|
132
|
|
|
{ |
133
|
4 |
|
if(!is_object($object)){ |
134
|
|
|
return false; |
135
|
|
|
} |
136
|
4 |
|
$class = new \ReflectionClass($object); |
137
|
4 |
|
if(!$class->hasProperty($name)){ |
138
|
2 |
|
return false; |
139
|
|
|
} |
140
|
2 |
|
$property = $class->getProperty($name); |
141
|
2 |
|
if(!$property){ |
142
|
|
|
return false; |
143
|
|
|
} |
144
|
2 |
|
return $property->isPublic(); |
145
|
|
|
} |
146
|
|
|
private $obj; |
147
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.