Exception
3 years ago
Map
3 years ago
Tool
3 years ago
AbstractArray.php
3 years ago
AbstractCollection.php
3 years ago
AbstractSet.php
3 years ago
ArrayInterface.php
3 years ago
Collection.php
3 years ago
CollectionInterface.php
3 years ago
DoubleEndedQueue.php
3 years ago
DoubleEndedQueueInterface.php
3 years ago
GenericArray.php
3 years ago
Queue.php
3 years ago
QueueInterface.php
3 years ago
Set.php
3 years ago
Queue.php
170 lines
| 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 | |
| 13 | declare(strict_types=1); |
| 14 | |
| 15 | namespace Ramsey\Collection; |
| 16 | |
| 17 | use Ramsey\Collection\Exception\InvalidArgumentException; |
| 18 | use Ramsey\Collection\Exception\NoSuchElementException; |
| 19 | use Ramsey\Collection\Tool\TypeTrait; |
| 20 | use Ramsey\Collection\Tool\ValueToStringTrait; |
| 21 | |
| 22 | /** |
| 23 | * This class provides a basic implementation of `QueueInterface`, to minimize |
| 24 | * the effort required to implement this interface. |
| 25 | * |
| 26 | * @template T |
| 27 | * @extends AbstractArray<T> |
| 28 | * @implements QueueInterface<T> |
| 29 | */ |
| 30 | class Queue extends AbstractArray implements QueueInterface |
| 31 | { |
| 32 | use TypeTrait; |
| 33 | use ValueToStringTrait; |
| 34 | |
| 35 | /** |
| 36 | * The type of elements stored in this queue. |
| 37 | * |
| 38 | * A queue's type is immutable once it is set. For this reason, this |
| 39 | * property is set private. |
| 40 | * |
| 41 | * @var string |
| 42 | */ |
| 43 | private $queueType; |
| 44 | |
| 45 | /** |
| 46 | * The index of the head of the queue. |
| 47 | * |
| 48 | * @var int |
| 49 | */ |
| 50 | protected $index = 0; |
| 51 | |
| 52 | /** |
| 53 | * Constructs a queue object of the specified type, optionally with the |
| 54 | * specified data. |
| 55 | * |
| 56 | * @param string $queueType The type (FQCN) associated with this queue. |
| 57 | * @param array<array-key, T> $data The initial items to store in the collection. |
| 58 | */ |
| 59 | public function __construct(string $queueType, array $data = []) |
| 60 | { |
| 61 | $this->queueType = $queueType; |
| 62 | parent::__construct($data); |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * {@inheritDoc} |
| 67 | * |
| 68 | * Since arbitrary offsets may not be manipulated in a queue, this method |
| 69 | * serves only to fulfill the `ArrayAccess` interface requirements. It is |
| 70 | * invoked by other operations when adding values to the queue. |
| 71 | */ |
| 72 | public function offsetSet($offset, $value): void |
| 73 | { |
| 74 | if ($this->checkType($this->getType(), $value) === false) { |
| 75 | throw new InvalidArgumentException( |
| 76 | 'Value must be of type ' . $this->getType() . '; value is ' |
| 77 | . $this->toolValueToString($value) |
| 78 | ); |
| 79 | } |
| 80 | |
| 81 | $this->data[] = $value; |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * @inheritDoc |
| 86 | */ |
| 87 | public function add($element): bool |
| 88 | { |
| 89 | $this[] = $element; |
| 90 | |
| 91 | return true; |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * @inheritDoc |
| 96 | */ |
| 97 | public function element() |
| 98 | { |
| 99 | $element = $this->peek(); |
| 100 | |
| 101 | if ($element === null) { |
| 102 | throw new NoSuchElementException( |
| 103 | 'Can\'t return element from Queue. Queue is empty.' |
| 104 | ); |
| 105 | } |
| 106 | |
| 107 | return $element; |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * @inheritDoc |
| 112 | */ |
| 113 | public function offer($element): bool |
| 114 | { |
| 115 | try { |
| 116 | return $this->add($element); |
| 117 | } catch (InvalidArgumentException $e) { |
| 118 | return false; |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * @inheritDoc |
| 124 | */ |
| 125 | public function peek() |
| 126 | { |
| 127 | if ($this->count() === 0) { |
| 128 | return null; |
| 129 | } |
| 130 | |
| 131 | return $this[$this->index]; |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * @inheritDoc |
| 136 | */ |
| 137 | public function poll() |
| 138 | { |
| 139 | if ($this->count() === 0) { |
| 140 | return null; |
| 141 | } |
| 142 | |
| 143 | $head = $this[$this->index]; |
| 144 | |
| 145 | unset($this[$this->index]); |
| 146 | $this->index++; |
| 147 | |
| 148 | return $head; |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * @inheritDoc |
| 153 | */ |
| 154 | public function remove() |
| 155 | { |
| 156 | $head = $this->poll(); |
| 157 | |
| 158 | if ($head === null) { |
| 159 | throw new NoSuchElementException('Can\'t return element from Queue. Queue is empty.'); |
| 160 | } |
| 161 | |
| 162 | return $head; |
| 163 | } |
| 164 | |
| 165 | public function getType(): string |
| 166 | { |
| 167 | return $this->queueType; |
| 168 | } |
| 169 | } |
| 170 |