-
Notifications
You must be signed in to change notification settings - Fork 0
/
ObjectArray.php
executable file
·73 lines (71 loc) · 1.92 KB
/
ObjectArray.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
<?php
/*
* Object Array: Provides a kind of ArrayObject but with support for references and callable properties
* Jonas Raoni Soares da Silva <http://raoni.org>
* https://github.com/jonasraoni/php-object-array
*/
class ObjectArray implements IteratorAggregate, ArrayAccess, Countable{
protected $data;
public function __construct(&$data = null, $byReference = true){
$byReference ? $this->setArrayReference($data) : $this->setArray(empty($data) ? array() : $data);
}
public function &getArray(){
return $this->data;
}
public function setArrayReference(&$data){
if(!is_array($data))
throw new Exception('Array expected');
$this->data = &$data;
}
public function setArray($data){
if(!is_array($data))
throw new Exception('Array expected');
unset($this->data);
$this->data = $data;
}
public function &__get($n){
return $this->get($n);
}
public function __isset($n){
return isset($this->data[$n]);
}
public function __unset($n){
unset($this->data[$n]);
}
public function __set($n, $v){
$this->set($n, $v);
}
public function __call($n, $a){
return call_user_func_array($this->data[$n], $a);
}
public function &get($n, $default = null){
return isset($this->$n) ? $this->data[$n] : $default;
}
public function set($n, &$value){
$n === null ? $this->data[] = $value : $this->data[$n] = $value;
}
//Countable
public function count(){
return count($this->data);
}
//IteratorAggregate
public function getIterator(){
return new ArrayIterator($this->data);
}
//ArrayAccess
public function offsetExists($n){
return isset($this->data[$n]);
}
public function offsetGet($n){
return $this->get($n);
}
public function offsetSet($n, $v){
$this->set($n, $v);
}
public function offsetUnset($n){
unset($this->data[$n]);
}
public function __toString(){
return '[' . get_class($this) . '@' . spl_object_hash($this) . ']';
}
}