This commit is contained in:
Paolo A
2024-08-13 13:44:16 +00:00
parent 1bbb23088d
commit e796d76612
4001 changed files with 30101 additions and 40075 deletions

View File

@@ -19,6 +19,8 @@ use Ramsey\Collection\Exception\NoSuchElementException;
use Ramsey\Collection\Tool\TypeTrait;
use Ramsey\Collection\Tool\ValueToStringTrait;
use function array_key_first;
/**
* This class provides a basic implementation of `QueueInterface`, to minimize
* the effort required to implement this interface.
@@ -32,29 +34,15 @@ class Queue extends AbstractArray implements QueueInterface
use TypeTrait;
use ValueToStringTrait;
/**
* The type of elements stored in this queue.
*
* A queue's type is immutable once it is set. For this reason, this
* property is set private.
*/
private string $queueType;
/**
* The index of the head of the queue.
*/
protected int $index = 0;
/**
* Constructs a queue object of the specified type, optionally with the
* specified data.
*
* @param string $queueType The type (FQCN) associated with this queue.
* @param array<array-key, T> $data The initial items to store in the collection.
* @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.
*/
public function __construct(string $queueType, array $data = [])
public function __construct(private readonly string $queueType, array $data = [])
{
$this->queueType = $queueType;
parent::__construct($data);
}
@@ -65,9 +53,9 @@ class Queue extends AbstractArray implements QueueInterface
* serves only to fulfill the `ArrayAccess` interface requirements. It is
* invoked by other operations when adding values to the queue.
*
* @throws InvalidArgumentException if $value is of the wrong type
* @throws InvalidArgumentException if $value is of the wrong type.
*/
public function offsetSet($offset, $value): void
public function offsetSet(mixed $offset, mixed $value): void
{
if ($this->checkType($this->getType(), $value) === false) {
throw new InvalidArgumentException(
@@ -80,11 +68,9 @@ class Queue extends AbstractArray implements QueueInterface
}
/**
* @throws InvalidArgumentException if $value is of the wrong type
*
* @inheritDoc
* @throws InvalidArgumentException if $value is of the wrong type.
*/
public function add($element): bool
public function add(mixed $element): bool
{
$this[] = $element;
@@ -92,74 +78,67 @@ class Queue extends AbstractArray implements QueueInterface
}
/**
* @inheritDoc
* @return T
*
* @throws NoSuchElementException if this queue is empty.
*/
public function element()
public function element(): mixed
{
$element = $this->peek();
if ($element === null) {
throw new NoSuchElementException(
'Can\'t return element from Queue. Queue is empty.',
);
}
return $element;
return $this->peek() ?? throw new NoSuchElementException(
'Can\'t return element from Queue. Queue is empty.',
);
}
/**
* @inheritDoc
*/
public function offer($element): bool
public function offer(mixed $element): bool
{
try {
return $this->add($element);
} catch (InvalidArgumentException $e) {
} catch (InvalidArgumentException) {
return false;
}
}
/**
* @inheritDoc
* @return T | null
*/
public function peek()
public function peek(): mixed
{
if ($this->count() === 0) {
$index = array_key_first($this->data);
if ($index === null) {
return null;
}
return $this[$this->index];
return $this[$index];
}
/**
* @inheritDoc
* @return T | null
*/
public function poll()
public function poll(): mixed
{
if ($this->count() === 0) {
$index = array_key_first($this->data);
if ($index === null) {
return null;
}
$head = $this[$this->index];
unset($this[$this->index]);
$this->index++;
$head = $this[$index];
unset($this[$index]);
return $head;
}
/**
* @inheritDoc
* @return T
*
* @throws NoSuchElementException if this queue is empty.
*/
public function remove()
public function remove(): mixed
{
$head = $this->poll();
if ($head === null) {
throw new NoSuchElementException('Can\'t return element from Queue. Queue is empty.');
}
return $head;
return $this->poll() ?? throw new NoSuchElementException(
'Can\'t return element from Queue. Queue is empty.',
);
}
public function getType(): string