| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* This file is part of the ramsey/collection library |
| 5 |
* |
| 6 |
* For the full copyright and license information, please view the LICENSE |
| 7 |
* file that was distributed with this source code. |
| 8 |
* |
| 9 |
* @copyright Copyright (c) Ben Ramsey <ben@benramsey.com> |
| 10 |
* @license http://opensource.org/licenses/MIT MIT |
| 11 |
*/ |
| 12 |
declare (strict_types=1); |
| 13 |
namespace Dudlewebs\WPMCS\GCP\Ramsey\Collection; |
| 14 |
|
| 15 |
use Dudlewebs\WPMCS\GCP\Ramsey\Collection\Exception\InvalidArgumentException; |
| 16 |
use Dudlewebs\WPMCS\GCP\Ramsey\Collection\Exception\NoSuchElementException; |
| 17 |
use Dudlewebs\WPMCS\GCP\Ramsey\Collection\Tool\TypeTrait; |
| 18 |
use Dudlewebs\WPMCS\GCP\Ramsey\Collection\Tool\ValueToStringTrait; |
| 19 |
use function array_key_first; |
| 20 |
/** |
| 21 |
* This class provides a basic implementation of `QueueInterface`, to minimize |
| 22 |
* the effort required to implement this interface. |
| 23 |
* |
| 24 |
* @template T |
| 25 |
* @extends AbstractArray<T> |
| 26 |
* @implements QueueInterface<T> |
| 27 |
*/ |
| 28 |
class Queue extends AbstractArray implements QueueInterface |
| 29 |
{ |
| 30 |
use TypeTrait; |
| 31 |
use ValueToStringTrait; |
| 32 |
/** |
| 33 |
* Constructs a queue object of the specified type, optionally with the |
| 34 |
* specified data. |
| 35 |
* |
| 36 |
* @param string $queueType The type or class name associated with this queue. |
| 37 |
* @param array<array-key, T> $data The initial items to store in the queue. |
| 38 |
*/ |
| 39 |
public function __construct(private readonly string $queueType, array $data = []) |
| 40 |
{ |
| 41 |
parent::__construct($data); |
| 42 |
} |
| 43 |
/** |
| 44 |
* {@inheritDoc} |
| 45 |
* |
| 46 |
* Since arbitrary offsets may not be manipulated in a queue, this method |
| 47 |
* serves only to fulfill the `ArrayAccess` interface requirements. It is |
| 48 |
* invoked by other operations when adding values to the queue. |
| 49 |
* |
| 50 |
* @throws InvalidArgumentException if $value is of the wrong type. |
| 51 |
*/ |
| 52 |
public function offsetSet(mixed $offset, mixed $value) : void |
| 53 |
{ |
| 54 |
if ($this->checkType($this->getType(), $value) === \false) { |
| 55 |
throw new InvalidArgumentException('Value must be of type ' . $this->getType() . '; value is ' . $this->toolValueToString($value)); |
| 56 |
} |
| 57 |
$this->data[] = $value; |
| 58 |
} |
| 59 |
/** |
| 60 |
* @throws InvalidArgumentException if $value is of the wrong type. |
| 61 |
*/ |
| 62 |
public function add(mixed $element) : bool |
| 63 |
{ |
| 64 |
$this[] = $element; |
| 65 |
return \true; |
| 66 |
} |
| 67 |
/** |
| 68 |
* @return T |
| 69 |
* |
| 70 |
* @throws NoSuchElementException if this queue is empty. |
| 71 |
*/ |
| 72 |
public function element() : mixed |
| 73 |
{ |
| 74 |
return $this->peek() ?? throw new NoSuchElementException('Can\'t return element from Queue. Queue is empty.'); |
| 75 |
} |
| 76 |
public function offer(mixed $element) : bool |
| 77 |
{ |
| 78 |
try { |
| 79 |
return $this->add($element); |
| 80 |
} catch (InvalidArgumentException) { |
| 81 |
return \false; |
| 82 |
} |
| 83 |
} |
| 84 |
/** |
| 85 |
* @return T | null |
| 86 |
*/ |
| 87 |
public function peek() : mixed |
| 88 |
{ |
| 89 |
$index = array_key_first($this->data); |
| 90 |
if ($index === null) { |
| 91 |
return null; |
| 92 |
} |
| 93 |
return $this[$index]; |
| 94 |
} |
| 95 |
/** |
| 96 |
* @return T | null |
| 97 |
*/ |
| 98 |
public function poll() : mixed |
| 99 |
{ |
| 100 |
$index = array_key_first($this->data); |
| 101 |
if ($index === null) { |
| 102 |
return null; |
| 103 |
} |
| 104 |
$head = $this[$index]; |
| 105 |
unset($this[$index]); |
| 106 |
return $head; |
| 107 |
} |
| 108 |
/** |
| 109 |
* @return T |
| 110 |
* |
| 111 |
* @throws NoSuchElementException if this queue is empty. |
| 112 |
*/ |
| 113 |
public function remove() : mixed |
| 114 |
{ |
| 115 |
return $this->poll() ?? throw new NoSuchElementException('Can\'t return element from Queue. Queue is empty.'); |
| 116 |
} |
| 117 |
public function getType() : string |
| 118 |
{ |
| 119 |
return $this->queueType; |
| 120 |
} |
| 121 |
} |
| 122 |
|