PluginProbe
Media Cloud Sync / 1.2.4
Media Cloud Sync v1.2.4
1.4.0 1.3.12 1.3.11 1.3.10 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.2.0 1.2.10 1.2.11 1.2.12 1.2.13 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 1.3.0 All 34 releases
media-cloud-sync / includes / sdk / google / ramsey / collection / src / Queue.php

Queue.php in Media Cloud Sync 1.2.4, at includes/sdk/google/ramsey/collection/src/Queue.php

141 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 declare (strict_types=1);
13 namespace Dudlewebs\WPMCS\Ramsey\Collection;
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;
19 /**
20 * This class provides a basic implementation of `QueueInterface`, to minimize
21 * the effort required to implement this interface.
22 *
23 * @template T
24 * @extends AbstractArray<T>
25 * @implements QueueInterface<T>
26 */
27 class Queue extends AbstractArray implements QueueInterface
28 {
29 use TypeTrait;
30 use ValueToStringTrait;
31 /**
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 * Constructs a queue object of the specified type, optionally with the
44 * specified data.
45 *
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.
48 */
49 public function __construct(string $queueType, array $data = [])
50 {
51 $this->queueType = $queueType;
52 parent::__construct($data);
53 }
54 /**
55 * {@inheritDoc}
56 *
57 * Since arbitrary offsets may not be manipulated in a queue, this method
58 * serves only to fulfill the `ArrayAccess` interface requirements. It is
59 * invoked by other operations when adding values to the queue.
60 *
61 * @throws InvalidArgumentException if $value is of the wrong type
62 */
63 public function offsetSet($offset, $value): void
64 {
65 if ($this->checkType($this->getType(), $value) === \false) {
66 throw new InvalidArgumentException('Value must be of type ' . $this->getType() . '; value is ' . $this->toolValueToString($value));
67 }
68 $this->data[] = $value;
69 }
70 /**
71 * @throws InvalidArgumentException if $value is of the wrong type
72 *
73 * @inheritDoc
74 */
75 public function add($element): bool
76 {
77 $this[] = $element;
78 return \true;
79 }
80 /**
81 * @inheritDoc
82 */
83 public function element()
84 {
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;
90 }
91 /**
92 * @inheritDoc
93 */
94 public function offer($element): bool
95 {
96 try {
97 return $this->add($element);
98 } catch (InvalidArgumentException $e) {
99 return \false;
100 }
101 }
102 /**
103 * @inheritDoc
104 */
105 public function peek()
106 {
107 if ($this->count() === 0) {
108 return null;
109 }
110 return $this[$this->index];
111 }
112 /**
113 * @inheritDoc
114 */
115 public function poll()
116 {
117 if ($this->count() === 0) {
118 return null;
119 }
120 $head = $this[$this->index];
121 unset($this[$this->index]);
122 $this->index++;
123 return $head;
124 }
125 /**
126 * @inheritDoc
127 */
128 public function remove()
129 {
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;
135 }
136 public function getType(): string
137 {
138 return $this->queueType;
139 }
140 }
141