Files
kupshop/bundles/KupShop/KupShopBundle/Template/ObjectWrapper.php
2025-08-02 16:30:27 +02:00

87 lines
2.2 KiB
PHP

<?php
namespace KupShop\KupShopBundle\Template;
class ObjectWrapper extends BaseWrapper
{
protected $allowed_methods = [];
protected $allowed_fields = [];
/**
* Implements ArrayAccess interface.
*/
public function offsetSet($offset, $value): void
{
$this->object->{$offset} = $value;
}
public function offsetExists($offset): bool
{
return isset($this->object->{$offset}) || parent::offsetExists($offset);
}
public function offsetUnset($offset): void
{
unset($this->object->{$offset});
}
public function offsetGet($offset): mixed
{
$method = $this->offsetMethod($offset);
if (method_exists($this, $method)) {
$res = call_user_func([$this, $method]);
return $res;
}
if (isset($this->object->{$offset})) {
return $this->object->{$offset};
}
return null;
}
// protected function jsonSerializeObject()
// {
// $data = get_object_vars($this->object);
//
// $data = Filtering::withKeys($data, function ($key) {
// return array_search($key, $this->allowed_fields) !== false;
// });
//
// return $data;
// }
protected function jsonSerializeIntrospect()
{
$introspection = parent::jsonSerializeIntrospect();
if ($this->allowed_methods == ['*']) {
$this->allowed_methods = [];
$f = new \ReflectionClass($this->object);
foreach ($f->getMethods() as $method) {
if (!$method->isPublic()) {
continue;
}
$this->allowed_methods[] = $method->name;
}
}
$introspection['methods'] += $this->allowed_methods;
return $introspection;
}
public function __call($name, $arguments)
{
if (method_exists($this, $name)) {
return call_user_func_array([$this, $name], $arguments);
}
// Necham to provolat, aby to zuchlo samo
// kdyz je v objectu __call, nezavolalo by se to, method_exists vrati false
return call_user_func_array([$this->object, $name], $arguments);
}
}