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 | /** @var array */ |
||
27 | private $debugInfo; |
||
28 | |||
29 | /** |
||
30 | * JailObject constructor. |
||
31 | * |
||
32 | * @param TemplateReadyDocument $object the object that will be jailed |
||
33 | * @param array $whiteListFunctions a list of function names that can be called |
||
34 | * @param array $jailedFunctions a list of functions that will be redirected to another function |
||
35 | */ |
||
36 | 53 | public function __construct(TemplateReadyDocument &$object, array $whiteListFunctions, array $jailedFunctions = []) |
|
43 | |||
44 | 12 | public function __call($name, $arguments) |
|
66 | |||
67 | 1 | public function __debugInfo() |
|
|
|||
68 | { |
||
69 | 1 | if (!empty($this->debugInfo)) |
|
70 | { |
||
71 | return $this->debugInfo; |
||
72 | } |
||
73 | |||
74 | 1 | if ($this->object instanceof FrontMatterDocument) |
|
75 | { |
||
76 | 1 | $this->debugInfo = $this->object->getFrontMatter(true); |
|
77 | } |
||
78 | |||
79 | 1 | foreach ($this->whiteListFunctions as $function) |
|
80 | { |
||
81 | 1 | $value = preg_replace('/^(get|is)/', '', $function); |
|
82 | 1 | $value = lcfirst($value); |
|
83 | |||
84 | try |
||
85 | { |
||
86 | 1 | $this->debugInfo[$value] = call_user_func([$this, $function]); |
|
87 | } |
||
88 | 1 | catch (\BadMethodCallException $e) |
|
89 | { |
||
90 | // Just throw away this information because there's no point in listing an accessible value in this |
||
91 | // object that doesn't actually exist. |
||
92 | } |
||
93 | } |
||
94 | |||
95 | 1 | return $this->debugInfo; |
|
96 | } |
||
97 | |||
98 | 1 | public function __toString() |
|
104 | |||
105 | /** |
||
106 | * Check if the jailed object is an instance of a given class. |
||
107 | * |
||
108 | * @param string $class |
||
109 | * |
||
110 | * @return bool |
||
111 | */ |
||
112 | public function _coreInstanceOf($class) |
||
116 | |||
117 | /// |
||
118 | // ArrayAccess Implementation |
||
119 | /// |
||
120 | |||
121 | /** |
||
122 | * {@inheritdoc} |
||
123 | */ |
||
124 | 22 | public function offsetExists($offset) |
|
128 | |||
129 | /** |
||
130 | * {@inheritdoc} |
||
131 | */ |
||
132 | 32 | public function offsetGet($offset) |
|
136 | |||
137 | /** |
||
138 | * {@inheritdoc} |
||
139 | */ |
||
140 | public function offsetSet($offset, $value) |
||
144 | |||
145 | /** |
||
146 | * {@inheritdoc} |
||
147 | */ |
||
148 | public function offsetUnset($offset) |
||
152 | |||
153 | /// |
||
154 | // IteratorAggregate implementation |
||
155 | /// |
||
156 | |||
157 | /** |
||
158 | * {@inheritdoc} |
||
159 | */ |
||
160 | 1 | public function getIterator() |
|
164 | |||
165 | /// |
||
166 | // JsonSerializable implementation |
||
167 | /// |
||
168 | |||
169 | /** |
||
170 | * {@inheritdoc} |
||
171 | */ |
||
172 | public function jsonSerialize() |
||
176 | } |
||
177 |
Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a
@return
annotation as described here.