<?php

 
 
 
 
 
 






namespace Google\Protobuf;

use Google\Protobuf\Internal\DescriptorPool;
use Google\Protobuf\Internal\GPBType;
use Google\Protobuf\Internal\GPBUtil;
use Google\Protobuf\Internal\Message;
use Google\Protobuf\Internal\RepeatedFieldIter;
use Traversable;









class RepeatedField implements \ArrayAccess, \IteratorAggregate, \Countable
{

    


    private $container;
    


    private $type;
    



    private $klass;

    






    public function __construct($type, $klass = null)
    {
        $this->container = [];
        $this->type = $type;
        if ($this->type == GPBType::MESSAGE) {
            $pool = DescriptorPool::getGeneratedPool();
            $desc = $pool->getDescriptorByClassName($klass);
            if ($desc == NULL) {
                new $klass;   
                $desc = $pool->getDescriptorByClassName($klass);
            }
            $this->klass = $desc->getClass();
        }
    }

    


    public function getType()
    {
        return $this->type;
    }

    



    public function getClass()
    {
        return $this->klass;
    }

    










    #[\ReturnTypeWillChange]
    public function offsetGet($offset)
    {
        return $this->container[$offset];
    }

    












    #[\ReturnTypeWillChange]
    public function offsetSet($offset, $value)
    {
        switch ($this->type) {
            case GPBType::SFIXED32:
            case GPBType::SINT32:
            case GPBType::INT32:
            case GPBType::ENUM:
                GPBUtil::checkInt32($value);
                break;
            case GPBType::FIXED32:
            case GPBType::UINT32:
                GPBUtil::checkUint32($value);
                break;
            case GPBType::SFIXED64:
            case GPBType::SINT64:
            case GPBType::INT64:
                GPBUtil::checkInt64($value);
                break;
            case GPBType::FIXED64:
            case GPBType::UINT64:
                GPBUtil::checkUint64($value);
                break;
            case GPBType::FLOAT:
                GPBUtil::checkFloat($value);
                break;
            case GPBType::DOUBLE:
                GPBUtil::checkDouble($value);
                break;
            case GPBType::BOOL:
                GPBUtil::checkBool($value);
                break;
            case GPBType::BYTES:
                GPBUtil::checkString($value, false);
                break;
            case GPBType::STRING:
                GPBUtil::checkString($value, true);
                break;
            case GPBType::MESSAGE:
                if (is_null($value)) {
                    throw new \TypeError("RepeatedField element cannot be null.");
                }
                GPBUtil::checkMessage($value, $this->klass);
                break;
            default:
                break;
        }
        if (is_null($offset)) {
            $this->container[] = $value;
        } else {
            $count = count($this->container);
            if (!is_numeric($offset) || $offset < 0 || $offset >= $count) {
                trigger_error(
                    "Cannot modify element at the given index",
                    E_USER_ERROR);
                return;
            }
            $this->container[$offset] = $value;
        }
    }

    











    #[\ReturnTypeWillChange]
    public function offsetUnset($offset)
    {
        $count = count($this->container);
        if (!is_numeric($offset) || $count === 0 || $offset < 0 || $offset >= $count) {
            trigger_error(
                "Cannot remove element at the given index",
                E_USER_ERROR);
            return;
        }
        array_splice($this->container, $offset, 1);
    }

    








    public function offsetExists($offset): bool
    {
        return isset($this->container[$offset]);
    }

    


    public function getIterator(): Traversable
    {
        return new RepeatedFieldIter($this->container);
    }

    






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

    public function __debugInfo()
    {
        return array_map(
            function ($item) {
                if ($item instanceof Message || $item instanceof RepeatedField) {
                    return $item->__debugInfo();
                }
                return $item;
            },
            iterator_to_array($this)
        );
    }
}

class_alias(RepeatedField::class, Internal\RepeatedField::class);
