*/ private $resources; /** * The original Iterator, for memoization. * * @var Iterator|null */ private $iterator; /** * @param Iterator|array $resources An array or iterator of Resources. */ public function __construct( $resources = [] ) { if ( $resources instanceof Iterator ) { $this->iterator = $resources; $resources = iterator_to_array( $resources ); } $this->resources = $resources; } /** * Adds a resource to the collection. * * @since 1.0.0 * * @param Resource $resource Resource instance. * * @return Resource */ public function add( Resource $resource ): Resource { if ( ! $this->offsetExists( $resource->get_slug() ) ) { $this->offsetSet( $resource->get_slug(), $resource ); } return $this->offsetGet( $resource->get_slug() ); } /** * @return Resource */ #[\ReturnTypeWillChange] public function current(): Resource { return current( $this->resources ); } /** * Gets the resource with the given path. * * @since 1.0.0 * * @param string $path Path to filter collection by. * @param Iterator|null $iterator Optional. Iterator to filter. * * @return self */ public function get_by_path( string $path, ?Iterator $iterator = null ): self { $results = new Filters\Path_FilterIterator( $iterator ?: $this->getIterator(), [ $path ] ); return new self( $results ); } /** * Gets the resource with the given paths. * * @since 1.0.0 * * @param array $paths Paths to filter collection by. * @param Iterator|null $iterator Optional. Iterator to filter. * * @return self */ public function get_by_paths( array $paths, ?Iterator $iterator = null ): self { $results = new Filters\Path_FilterIterator( $iterator ?: $this->getIterator(), $paths ); return new self( $results ); } /** * Gets the plugin resources. * * @since 1.0.0 * * @param Iterator|null $iterator Optional. Iterator to filter. * * @return self */ public function get_plugins( ?Iterator $iterator = null ): self { $results = new Filters\Plugin_FilterIterator( $iterator ?: $this->getIterator() ); return new self( $results ); } /** * Gets the service resources. * * @since 1.0.0 * * @param Iterator|null $iterator Optional. Iterator to filter. * * @return self */ public function get_services( ?Iterator $iterator = null ): self { $results = new Filters\Service_FilterIterator( $iterator ?: $this->getIterator() ); return new self( $results ); } /** * @return array-key|null */ #[\ReturnTypeWillChange] public function key() { return key( $this->resources ); } /** * @inheritDoc */ public function next(): void { next( $this->resources ); } /** * @inheritDoc */ public function offsetExists( $offset ): bool { return array_key_exists( $offset, $this->resources ); } /** * @return Resource|null */ #[\ReturnTypeWillChange] public function offsetGet( $offset ): ?Resource { return $this->resources[ $offset ] ?? null; } /** * @inheritDoc */ public function offsetSet( $offset, $value ): void { $this->resources[ $offset ] = $value; } /** * @inheritDoc */ public function offsetUnset( $offset ): void { unset( $this->resources[ $offset ] ); } /** * Helper function for removing a resource from the collection. * * @since 1.0.0 * * @param string $slug Resource slug. * * @return void */ public function remove( string $slug ): void { $this->offsetUnset( $slug ); } /** * @inheritDoc */ public function rewind(): void { reset( $this->resources ); } /** * Sets a resource in the collection. * * @since 1.0.0 * * @param string $slug Resource slug. * @param Resource $resource Resource instance. * * @return Resource|null */ public function set( string $slug, Resource $resource ): ?Resource { $this->offsetSet( $slug, $resource ); return $this->offsetGet( $slug ); } /** * @inheritDoc */ public function valid(): bool { return key( $this->resources ) !== null; } /** * @inheritDoc */ public function count(): int { return count( $this->resources ); } /** * Returns a clone of the underlying iterator. * * @return Iterator */ public function getIterator(): Iterator { if ( isset( $this->iterator ) ) { return $this->iterator; } return $this->iterator = new ArrayIterator( $this->resources ); } }