1 | <?php |
||
15 | class JailedDocument implements \ArrayAccess, \IteratorAggregate, \JsonSerializable |
||
16 | { |
||
17 | /** @var string[] */ |
||
18 | private $whiteListFunctions; |
||
19 | |||
20 | /** @var string[] */ |
||
21 | private $jailedFunctions; |
||
22 | |||
23 | /** @var TemplateReadyDocument */ |
||
24 | private $object; |
||
25 | |||
26 | /** |
||
27 | * JailObject constructor. |
||
28 | * |
||
29 | * @param TemplateReadyDocument $object The object that will be jailed. |
||
30 | * @param array $whiteListFunctions A list of function names that can be called. |
||
31 | * @param array $jailedFunctions A list of functions that will be redirected to another function. |
||
32 | */ |
||
33 | 41 | public function __construct(TemplateReadyDocument &$object, array $whiteListFunctions, array $jailedFunctions = array()) |
|
39 | |||
40 | 11 | public function __call($name, $arguments) |
|
41 | { |
||
42 | // White listed functions will always be getter functions, so somehow get the name of a possible getter function |
||
43 | // name. |
||
44 | 11 | $lcName = lcfirst($name); |
|
45 | 11 | $getFxnCall = ($lcName[0] === 'g' && strpos($lcName, 'get') === 0) ? $lcName : sprintf('get%s', ucfirst($name)); |
|
46 | |||
47 | // Check if our function call is a jailed call, meaning the function should be mapped to special "jailed" |
||
48 | // jailed version of the function call. |
||
49 | 11 | if (array_key_exists($getFxnCall, $this->jailedFunctions)) |
|
50 | { |
||
51 | 3 | return call_user_func_array(array($this->object, $this->jailedFunctions[$getFxnCall]), $arguments); |
|
52 | } |
||
53 | |||
54 | // Otherwise, test to see if the function call is in our white list and call it |
||
55 | 8 | if (in_array($getFxnCall, $this->whiteListFunctions)) |
|
56 | { |
||
57 | 7 | return call_user_func_array(array($this->object, $getFxnCall), $arguments); |
|
58 | } |
||
59 | |||
60 | 1 | throw new \BadMethodCallException(); |
|
61 | } |
||
62 | |||
63 | /** |
||
64 | * Check if the jailed object is an instance of a given class. |
||
65 | * |
||
66 | * @param string $class |
||
67 | * |
||
68 | * @return bool |
||
69 | */ |
||
70 | public function coreInstanceOf($class) |
||
74 | |||
75 | /// |
||
76 | // ArrayAccess Implementation |
||
77 | /// |
||
78 | |||
79 | /** |
||
80 | * {@inheritdoc} |
||
81 | */ |
||
82 | 18 | public function offsetExists($offset) |
|
86 | |||
87 | /** |
||
88 | * {@inheritdoc} |
||
89 | */ |
||
90 | 28 | public function offsetGet($offset) |
|
94 | |||
95 | /** |
||
96 | * {@inheritdoc} |
||
97 | */ |
||
98 | public function offsetSet($offset, $value) |
||
102 | |||
103 | /** |
||
104 | * {@inheritdoc} |
||
105 | */ |
||
106 | public function offsetUnset($offset) |
||
110 | |||
111 | /// |
||
112 | // IteratorAggregate implementation |
||
113 | /// |
||
114 | |||
115 | /** |
||
116 | * {@inheritdoc} |
||
117 | */ |
||
118 | 19 | public function getIterator() |
|
122 | |||
123 | /// |
||
124 | // JsonSerializable implementation |
||
125 | /// |
||
126 | |||
127 | /** |
||
128 | * {@inheritdoc} |
||
129 | */ |
||
130 | public function jsonSerialize() |
||
134 | } |
||
135 |