PluginProbe
WCPOS – Point of Sale (POS) plugin for WooCommerce / 1.10.13
WCPOS – Point of Sale (POS) plugin for WooCommerce v1.10.13
1.10.19 1.10.18 1.10.17 1.10.16 1.10.15 1.10.13 1.10.14 1.10.12 1.10.11 1.10.10 1.10.9 1.10.8 untagged-3d9b7ccddc54df87c672 1.10.7 1.10.6 1.10.5 1.10.3 1.10.4 1.10.2 1.10.1 1.10.0 1.9.17 1.9.15 1.9.16 1.9.14 All 163 releases
woocommerce-pos / vendor / ramsey / collection / src / Queue.php

Queue.php in WCPOS – Point of Sale (POS) plugin for WooCommerce 1.10.13, at vendor/ramsey/collection/src/Queue.php

170 lines 3.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 private string $queueType;
42
43 /**
44 * The index of the head of the queue.
45 */
46 protected int $index = 0;
47
48 /**
49 * Constructs a queue object of the specified type, optionally with the
50 * specified data.
51 *
52 * @param string $queueType The type (FQCN) associated with this queue.
53 * @param array<array-key, T> $data The initial items to store in the collection.
54 */
55 public function __construct(string $queueType, array $data = [])
56 {
57 $this->queueType = $queueType;
58 parent::__construct($data);
59 }
60
61 /**
62 * {@inheritDoc}
63 *
64 * Since arbitrary offsets may not be manipulated in a queue, this method
65 * serves only to fulfill the `ArrayAccess` interface requirements. It is
66 * invoked by other operations when adding values to the queue.
67 *
68 * @throws InvalidArgumentException if $value is of the wrong type
69 */
70 public function offsetSet($offset, $value): void
71 {
72 if ($this->checkType($this->getType(), $value) === false) {
73 throw new InvalidArgumentException(
74 'Value must be of type ' . $this->getType() . '; value is '
75 . $this->toolValueToString($value),
76 );
77 }
78
79 $this->data[] = $value;
80 }
81
82 /**
83 * @throws InvalidArgumentException if $value is of the wrong type
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