Files
apimacro/vendor/ramsey/collection/src/Queue.php

149 lines
3.6 KiB
PHP
Raw Normal View History

2024-05-07 12:17:25 +02:00
<?php
/**
* This file is part of the ramsey/collection library
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @copyright Copyright (c) Ben Ramsey <ben@benramsey.com>
* @license http://opensource.org/licenses/MIT MIT
*/
declare(strict_types=1);
namespace Ramsey\Collection;
use Ramsey\Collection\Exception\InvalidArgumentException;
use Ramsey\Collection\Exception\NoSuchElementException;
use Ramsey\Collection\Tool\TypeTrait;
use Ramsey\Collection\Tool\ValueToStringTrait;
2024-08-13 13:44:16 +00:00
use function array_key_first;
2024-05-07 12:17:25 +02:00
/**
* This class provides a basic implementation of `QueueInterface`, to minimize
* the effort required to implement this interface.
*
* @template T
* @extends AbstractArray<T>
* @implements QueueInterface<T>
*/
class Queue extends AbstractArray implements QueueInterface
{
use TypeTrait;
use ValueToStringTrait;
/**
* Constructs a queue object of the specified type, optionally with the
* specified data.
*
2024-08-13 13:44:16 +00:00
* @param string $queueType The type or class name associated with this queue.
* @param array<array-key, T> $data The initial items to store in the queue.
2024-05-07 12:17:25 +02:00
*/
2024-08-13 13:44:16 +00:00
public function __construct(private readonly string $queueType, array $data = [])
2024-05-07 12:17:25 +02:00
{
parent::__construct($data);
}
/**
* {@inheritDoc}
*
* Since arbitrary offsets may not be manipulated in a queue, this method
* serves only to fulfill the `ArrayAccess` interface requirements. It is
* invoked by other operations when adding values to the queue.
2024-05-17 12:24:19 +00:00
*
2024-08-13 13:44:16 +00:00
* @throws InvalidArgumentException if $value is of the wrong type.
2024-05-07 12:17:25 +02:00
*/
2024-08-13 13:44:16 +00:00
public function offsetSet(mixed $offset, mixed $value): void
2024-05-07 12:17:25 +02:00
{
if ($this->checkType($this->getType(), $value) === false) {
throw new InvalidArgumentException(
'Value must be of type ' . $this->getType() . '; value is '
2024-05-17 12:24:19 +00:00
. $this->toolValueToString($value),
2024-05-07 12:17:25 +02:00
);
}
$this->data[] = $value;
}
/**
2024-08-13 13:44:16 +00:00
* @throws InvalidArgumentException if $value is of the wrong type.
2024-05-07 12:17:25 +02:00
*/
2024-08-13 13:44:16 +00:00
public function add(mixed $element): bool
2024-05-07 12:17:25 +02:00
{
$this[] = $element;
return true;
}
/**
2024-08-13 13:44:16 +00:00
* @return T
*
* @throws NoSuchElementException if this queue is empty.
2024-05-07 12:17:25 +02:00
*/
2024-08-13 13:44:16 +00:00
public function element(): mixed
2024-05-07 12:17:25 +02:00
{
2024-08-13 13:44:16 +00:00
return $this->peek() ?? throw new NoSuchElementException(
'Can\'t return element from Queue. Queue is empty.',
);
2024-05-07 12:17:25 +02:00
}
2024-08-13 13:44:16 +00:00
public function offer(mixed $element): bool
2024-05-07 12:17:25 +02:00
{
try {
return $this->add($element);
2024-08-13 13:44:16 +00:00
} catch (InvalidArgumentException) {
2024-05-07 12:17:25 +02:00
return false;
}
}
/**
2024-08-13 13:44:16 +00:00
* @return T | null
2024-05-07 12:17:25 +02:00
*/
2024-08-13 13:44:16 +00:00
public function peek(): mixed
2024-05-07 12:17:25 +02:00
{
2024-08-13 13:44:16 +00:00
$index = array_key_first($this->data);
if ($index === null) {
2024-05-07 12:17:25 +02:00
return null;
}
2024-08-13 13:44:16 +00:00
return $this[$index];
2024-05-07 12:17:25 +02:00
}
/**
2024-08-13 13:44:16 +00:00
* @return T | null
2024-05-07 12:17:25 +02:00
*/
2024-08-13 13:44:16 +00:00
public function poll(): mixed
2024-05-07 12:17:25 +02:00
{
2024-08-13 13:44:16 +00:00
$index = array_key_first($this->data);
if ($index === null) {
2024-05-07 12:17:25 +02:00
return null;
}
2024-08-13 13:44:16 +00:00
$head = $this[$index];
unset($this[$index]);
2024-05-07 12:17:25 +02:00
return $head;
}
/**
2024-08-13 13:44:16 +00:00
* @return T
*
* @throws NoSuchElementException if this queue is empty.
2024-05-07 12:17:25 +02:00
*/
2024-08-13 13:44:16 +00:00
public function remove(): mixed
2024-05-07 12:17:25 +02:00
{
2024-08-13 13:44:16 +00:00
return $this->poll() ?? throw new NoSuchElementException(
'Can\'t return element from Queue. Queue is empty.',
);
2024-05-07 12:17:25 +02:00
}
public function getType(): string
{
return $this->queueType;
}
}