<?php

namespace Application\Service\Extension\Params;

class ParamsArrayAccess implements \Iterator, \Countable
{
    private array $values = [];
    public function __construct(array $values = [])
    {
        $this->values = $values;
    }

    public function current()
    {
        return current($this->values);
    }

    public function next()
    {
        return next($this->values);
    }

    public function key()
    {
        return key($this->values);
    }

    public function valid()
    {
        return ($this->key() !== null);
    }

    public function rewind()
    {
        reset($this->values);
    }

    public function count()
    {
        return count($this->values);
    }

    public function toArray()
    {
        /** @var ParamSet $value */
        foreach ($this->values as &$value) {
            $value = $value->value();
        }

        return $this->values;
    }

    public function toFormatArray()
    {
        /** @var ParamSet $value */
        foreach ($this->values as &$value) {
            $value = $value->formatValue();
        }

        return $this->values;
    }

    public function __get($name)
    {
        return current($this->values->{$name});
    }
}