1 | <?php |
||
2 | /** |
||
3 | * This file is part of the O2System Framework package. |
||
4 | * |
||
5 | * For the full copyright and license information, please view the LICENSE |
||
6 | * file that was distributed with this source code. |
||
7 | * |
||
8 | * @author Steeve Andrian Salim |
||
9 | * @copyright Copyright (c) Steeve Andrian Salim |
||
10 | */ |
||
11 | |||
12 | // ------------------------------------------------------------------------ |
||
13 | |||
14 | namespace O2System\Spl\DataStructures; |
||
15 | |||
16 | // ------------------------------------------------------------------------ |
||
17 | |||
18 | use O2System\Spl\Interfaces\SplArrayInterface; |
||
19 | |||
20 | /** |
||
21 | * Class SplArrayQueue |
||
22 | * |
||
23 | * The SplStack class provides the main functionalities of a stack implemented using a doubly linked list and |
||
24 | * the iterator mode is based on FIFO (First In First Out). |
||
25 | * |
||
26 | * @package O2System\Spl\DataStructures |
||
27 | */ |
||
28 | class SplArrayQueue extends \SplQueue implements SplArrayInterface |
||
29 | { |
||
30 | /** |
||
31 | * SplArrayQueue::__construct |
||
32 | * |
||
33 | * @param array $queue |
||
34 | */ |
||
35 | public function __construct(array $queue = []) |
||
36 | { |
||
37 | if (count($queue)) { |
||
38 | foreach ($queue as $item) { |
||
39 | $this->push($item); |
||
40 | } |
||
41 | } |
||
42 | } |
||
43 | |||
44 | // ----------------------------------------------------------------------- |
||
45 | |||
46 | /** |
||
47 | * SplArrayQueue::current |
||
48 | * |
||
49 | * Replacement for \SplQueue current method |
||
50 | * |
||
51 | * @return mixed |
||
52 | */ |
||
53 | public function current() |
||
54 | { |
||
55 | if (null === ($current = parent::current())) { |
||
0 ignored issues
–
show
Unused Code
introduced
by
![]() |
|||
56 | $this->rewind(); |
||
57 | } |
||
58 | |||
59 | return parent::current(); |
||
60 | } |
||
61 | |||
62 | // ----------------------------------------------------------------------- |
||
63 | |||
64 | /** |
||
65 | * SplArrayQueue::isEmpty |
||
66 | * |
||
67 | * Checks if the array storage is empty. |
||
68 | * |
||
69 | * @return bool |
||
70 | */ |
||
71 | public function isEmpty() |
||
72 | { |
||
73 | return ($this->count() == 0 ? true : false); |
||
74 | } |
||
75 | |||
76 | // ----------------------------------------------------------------------- |
||
77 | |||
78 | /** |
||
79 | * SplArrayQueue::has |
||
80 | * |
||
81 | * Checks if a value exists in the storage. |
||
82 | * |
||
83 | * @param mixed $needle The searched value. |
||
84 | * @param bool $strict If the third parameter strict is set to TRUE then the in_array() function will also check |
||
85 | * the types of the needle in the haystack. |
||
86 | * |
||
87 | * @return bool |
||
88 | */ |
||
89 | public function has($needle, $strict = false) |
||
90 | { |
||
91 | return in_array($needle, $this->getArrayCopy(), $strict); |
||
92 | } |
||
93 | |||
94 | // ----------------------------------------------------------------------- |
||
95 | |||
96 | /** |
||
97 | * SplArrayQueue::getArrayCopy |
||
98 | * |
||
99 | * Creates a copy of the storage. |
||
100 | * |
||
101 | * @return array A copy of the storage. |
||
102 | */ |
||
103 | public function getArrayCopy() |
||
104 | { |
||
105 | $arrayCopy = []; |
||
106 | |||
107 | for ($this->rewind(); $this->valid(); $this->next()) { |
||
108 | $arrayCopy[] = $this->current(); |
||
109 | } |
||
110 | |||
111 | return $arrayCopy; |
||
112 | } |
||
113 | } |