<?php
namespace SabaiApps\Framework\Model\EntityCollection;

use SabaiApps\Framework\Model\Model;

/**
 * ArrayEntityCollection class for managing collections of entities stored in an array.
 * Provides methods for iterating and accessing entities within the array.
 */
class ArrayEntityCollection extends AbstractEntityCollection {

	private $_entities;

	/**
	 * Constructor.
	 *
	 * @param Model  $model Model instance.
	 * @param string $name Name of the entity.
	 * @param array  $entities Array of entities.
	 */
	public function __construct( Model $model, $name, array $entities = array() ) {
		parent::__construct( $model, $name );
		$this->_entities = array_values( $entities ); // Reindex array.
	}

	#[\ReturnTypeWillChange]
	public function valid() {
		return array_key_exists( $this->_key, $this->_entities );
	}

	/**
	 * Gets the current entity by index.
	 *
	 * @param int $index Index of the entity.
	 * @return mixed
	 */
	public function getCurrent( $index ) {
		return $this->_entities[ $index ];
	}

	#[\ReturnTypeWillChange]
	public function count() {
		return count( $this->_entities );
	}
}
