<?php

namespace KupShop\KupShopBundle\Template;

use Doctrine\Common\Collections\Collection;

/**
 * Class ArrayObjectWrapper.
 *
 * @property Collection $object
 */
class ArrayObjectWrapper extends ObjectWrapper implements \Countable, \IteratorAggregate
{
    public function getIterator(): \Traversable
    {
        return $this->object->getIterator();
    }

    public function count(): int
    {
        return $this->object->count();
    }

    public function offsetGet($offset): mixed
    {
        if (BaseWrapper::offsetExists($offset)) {
            return BaseWrapper::offsetGet($offset);
        }

        return $this->object->offsetGet($offset);
    }

    public function offsetSet($offset, $value): void
    {
        $this->object->offsetSet($offset, $value);
    }

    public function offsetExists($offset): bool
    {
        return $this->object->offsetExists($offset) || BaseWrapper::offsetExists($offset);
    }

    public function offsetUnset($offset): void
    {
        $this->object->offsetUnset($offset);
    }

    protected function jsonSerializeObject()
    {
        return $this->object->toArray();
    }

    protected function jsonSerializeIntrospect()
    {
        $introspection = parent::jsonSerializeIntrospect();

        $f = new \ReflectionClass('Doctrine\Common\Collections\Collection');
        foreach ($f->getMethods() as $method) {
            if ($method->class != 'Doctrine\Common\Collections\Collection' || !$method->isPublic()) {
                continue;
            }

            $introspection['methods'][] = $method->name;
        }

        return $introspection;
    }
}
