← All changes
|
includes/sdk/google/ramsey/collection/src/Queue.php
+37
-56
1.2.0
→
1.4.1
View file →
| @@ -9,14 +9,15 @@ | ||
| 9 | 9 | * @copyright Copyright (c) Ben Ramsey <ben@benramsey.com> |
| 10 | 10 | * @license http://opensource.org/licenses/MIT MIT |
| 11 | 11 | */ |
| 12 | 12 | declare (strict_types=1); |
| 13 | -namespace Dudlewebs\WPMCS\Ramsey\Collection; | |
| 13 | +namespace Dudlewebs\WPMCS\GCP\Ramsey\Collection; | |
| 14 | 14 | |
| 15 | -use Dudlewebs\WPMCS\Ramsey\Collection\Exception\InvalidArgumentException; | |
| 16 | -use Dudlewebs\WPMCS\Ramsey\Collection\Exception\NoSuchElementException; | |
| 17 | -use Dudlewebs\WPMCS\Ramsey\Collection\Tool\TypeTrait; | |
| 18 | -use Dudlewebs\WPMCS\Ramsey\Collection\Tool\ValueToStringTrait; | |
| 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; | |
| 19 | 20 | /** |
| 20 | 21 | * This class provides a basic implementation of `QueueInterface`, to minimize |
| 21 | 22 | * the effort required to implement this interface. |
| 22 | 23 | * |
| @@ -28,28 +29,16 @@ | ||
| 28 | 29 | { |
| 29 | 30 | use TypeTrait; |
| 30 | 31 | use ValueToStringTrait; |
| 31 | 32 | /** |
| 32 | - * The type of elements stored in this queue. | |
| 33 | - * | |
| 34 | - * A queue's type is immutable once it is set. For this reason, this | |
| 35 | - * property is set private. | |
| 36 | - */ | |
| 37 | - private string $queueType; | |
| 38 | - /** | |
| 39 | - * The index of the head of the queue. | |
| 40 | - */ | |
| 41 | - protected int $index = 0; | |
| 42 | - /** | |
| 43 | 33 | * Constructs a queue object of the specified type, optionally with the |
| 44 | 34 | * specified data. |
| 45 | 35 | * |
| 46 | - * @param string $queueType The type (FQCN) associated with this queue. | |
| 47 | - * @param array<array-key, T> $data The initial items to store in the collection. | |
| 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. | |
| 48 | 38 | */ |
| 49 | - public function __construct(string $queueType, array $data = []) | |
| 39 | + public function __construct(private readonly string $queueType, array $data = []) | |
| 50 | 40 | { |
| 51 | - $this->queueType = $queueType; | |
| 52 | 41 | parent::__construct($data); |
| 53 | 42 | } |
| 54 | 43 | /** |
| 55 | 44 | * {@inheritDoc} |
| @@ -57,11 +46,11 @@ | ||
| 57 | 46 | * Since arbitrary offsets may not be manipulated in a queue, this method |
| 58 | 47 | * serves only to fulfill the `ArrayAccess` interface requirements. It is |
| 59 | 48 | * invoked by other operations when adding values to the queue. |
| 60 | 49 | * |
| 61 | - * @throws InvalidArgumentException if $value is of the wrong type | |
| 50 | + * @throws InvalidArgumentException if $value is of the wrong type. | |
| 62 | 51 | */ |
| 63 | - public function offsetSet($offset, $value): void | |
| 52 | + public function offsetSet(mixed $offset, mixed $value) : void | |
| 64 | 53 | { |
| 65 | 54 | if ($this->checkType($this->getType(), $value) === \false) { |
| 66 | 55 | throw new InvalidArgumentException('Value must be of type ' . $this->getType() . '; value is ' . $this->toolValueToString($value)); |
| 67 | 56 | } |
| @@ -67,74 +56,66 @@ | ||
| 67 | 56 | } |
| 68 | 57 | $this->data[] = $value; |
| 69 | 58 | } |
| 70 | 59 | /** |
| 71 | - * @throws InvalidArgumentException if $value is of the wrong type | |
| 72 | - * | |
| 73 | - * @inheritDoc | |
| 60 | + * @throws InvalidArgumentException if $value is of the wrong type. | |
| 74 | 61 | */ |
| 75 | - public function add($element): bool | |
| 62 | + public function add(mixed $element) : bool | |
| 76 | 63 | { |
| 77 | 64 | $this[] = $element; |
| 78 | 65 | return \true; |
| 79 | 66 | } |
| 80 | 67 | /** |
| 81 | - * @inheritDoc | |
| 68 | + * @return T | |
| 69 | + * | |
| 70 | + * @throws NoSuchElementException if this queue is empty. | |
| 82 | 71 | */ |
| 83 | - public function element() | |
| 72 | + public function element() : mixed | |
| 84 | 73 | { |
| 85 | - $element = $this->peek(); | |
| 86 | - if ($element === null) { | |
| 87 | - throw new NoSuchElementException('Can\'t return element from Queue. Queue is empty.'); | |
| 88 | - } | |
| 89 | - return $element; | |
| 74 | + return $this->peek() ?? throw new NoSuchElementException('Can\'t return element from Queue. Queue is empty.'); | |
| 90 | 75 | } |
| 91 | - /** | |
| 92 | - * @inheritDoc | |
| 93 | - */ | |
| 94 | - public function offer($element): bool | |
| 76 | + public function offer(mixed $element) : bool | |
| 95 | 77 | { |
| 96 | 78 | try { |
| 97 | 79 | return $this->add($element); |
| 98 | - } catch (InvalidArgumentException $e) { | |
| 80 | + } catch (InvalidArgumentException) { | |
| 99 | 81 | return \false; |
| 100 | 82 | } |
| 101 | 83 | } |
| 102 | 84 | /** |
| 103 | - * @inheritDoc | |
| 85 | + * @return T | null | |
| 104 | 86 | */ |
| 105 | - public function peek() | |
| 87 | + public function peek() : mixed | |
| 106 | 88 | { |
| 107 | - if ($this->count() === 0) { | |
| 89 | + $index = array_key_first($this->data); | |
| 90 | + if ($index === null) { | |
| 108 | 91 | return null; |
| 109 | 92 | } |
| 110 | - return $this[$this->index]; | |
| 93 | + return $this[$index]; | |
| 111 | 94 | } |
| 112 | 95 | /** |
| 113 | - * @inheritDoc | |
| 96 | + * @return T | null | |
| 114 | 97 | */ |
| 115 | - public function poll() | |
| 98 | + public function poll() : mixed | |
| 116 | 99 | { |
| 117 | - if ($this->count() === 0) { | |
| 100 | + $index = array_key_first($this->data); | |
| 101 | + if ($index === null) { | |
| 118 | 102 | return null; |
| 119 | 103 | } |
| 120 | - $head = $this[$this->index]; | |
| 121 | - unset($this[$this->index]); | |
| 122 | - $this->index++; | |
| 104 | + $head = $this[$index]; | |
| 105 | + unset($this[$index]); | |
| 123 | 106 | return $head; |
| 124 | 107 | } |
| 125 | 108 | /** |
| 126 | - * @inheritDoc | |
| 109 | + * @return T | |
| 110 | + * | |
| 111 | + * @throws NoSuchElementException if this queue is empty. | |
| 127 | 112 | */ |
| 128 | - public function remove() | |
| 113 | + public function remove() : mixed | |
| 129 | 114 | { |
| 130 | - $head = $this->poll(); | |
| 131 | - if ($head === null) { | |
| 132 | - throw new NoSuchElementException('Can\'t return element from Queue. Queue is empty.'); | |
| 133 | - } | |
| 134 | - return $head; | |
| 115 | + return $this->poll() ?? throw new NoSuchElementException('Can\'t return element from Queue. Queue is empty.'); | |
| 135 | 116 | } |
| 136 | - public function getType(): string | |
| 117 | + public function getType() : string | |
| 137 | 118 | { |
| 138 | 119 | return $this->queueType; |
| 139 | 120 | } |
| 140 | 121 | } |