This repository has been archived by the owner on Feb 26, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
ArrayObject.php
133 lines (122 loc) · 3.27 KB
/
ArrayObject.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
<?php
/*
* ArrayObject class file
*/
namespace Moneybird;
use \ArrayObject as ParentArrayObject;
use Moneybird\ArrayObject\TypeMismatchException;
use Moneybird\ArrayObject\UndefinedMethodException;
/**
* Abstract class for array objects
*
* @abstract
*/
abstract class ArrayObject extends ParentArrayObject
{
/**
* Classnames of allowed childs
* @var string
*/
protected $childname = null;
/**
* Append an object to the array
* The object must match the type the array is meant for
*
* @param mixed $value
* @throws TypeMismatchException
* @return ArrayObject
*/
public function append($value)
{
$arrayType = $this->getChildName();
if (!($value instanceof $arrayType)) {
throw new TypeMismatchException('Passed argument is not an instance of ' . $arrayType);
} else {
parent::append($value);
}
return $this;
}
/**
* Merge another array of the same type into this array
*
* @param ArrayObject $array
* @throws TypeMismatchException
* @return ArrayObject
*/
public function merge(ArrayObject $array)
{
if (!($array instanceof $this)) {
throw new TypeMismatchException('Passed argument is not an instance of ' . get_class($this));
} else {
foreach ($array as $object) {
$this->append($object);
}
}
return $this;
}
/**
* If method does not exists on array, call method on children
* Returns an array with return values
*
* @param string $method
* @param array $arguments
* @throws UndefinedMethodException
* @return Array
*/
public function __call($method, Array $arguments)
{
$arrayType = $this->getChildName();
if (!method_exists($arrayType, $method)) {
throw new UndefinedMethodException('Fatal error: Call to undefined method ' . $arrayType . '::' . $method . '()');
}
$return = array();
foreach ($this as $object) {
$return[] = call_user_func_array(array($object, $method), $arguments);
}
return $return;
}
/**
* Get classname of expected children
*
* @access $protected
* @return string
*/
protected function getChildName()
{
if ($this->childname === null) {
$classname = get_class($this);
$pos = max(strrpos($classname, '_Array'), strrpos($classname, '\ArrayObject'));
$this->childname = substr($classname, 0, $pos);
}
return $this->childname;
}
/**
* Create JSON representation of array
*
* @return string
*/
public function toJson()
{
$array = array();
foreach ($this->disclose() as $key => $disclosure) {
$values = $disclosure->toArray();
foreach ($values as &$value) {
$value = utf8_encode($value);
}
$array[$key] = $values;
}
return json_encode($array);
}
/**
* Create PHP array
* @return Array
*/
public function toArray()
{
$array = array();
foreach ($this as $value) {
$array[] = $value;
}
return $array;
}
}