-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSplPriorityQueue.php
163 lines (143 loc) · 4.01 KB
/
SplPriorityQueue.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
<?php
declare(strict_types=1);
/*
* CoreShop
*
* This source file is available under two different licenses:
* - GNU General Public License version 3 (GPLv3)
* - CoreShop Commercial License (CCL)
* Full copyright and license information is available in
* LICENSE.md which is distributed with this source code.
*
* @copyright Copyright (c) CoreShop GmbH (https://www.coreshop.org)
* @license https://www.coreshop.org/license GPLv3 and CCL
*
*/
namespace CoreShop\Component\Pimcore;
use function array_key_exists;
use function get_debug_type;
use function is_array;
use const PHP_INT_MAX;
use ReturnTypeWillChange;
use Serializable;
use function serialize;
use function sprintf;
use UnexpectedValueException;
use function unserialize;
/**
* Serializable version of SplPriorityQueue
*
* Also, provides predictable heap order for datums added with the same priority
* (i.e., they will be emitted in the same order they are enqueued).
*
* @template TValue
* @template TPriority of int
*
* @extends \SplPriorityQueue<TPriority, TValue>
*/
class SplPriorityQueue extends \SplPriorityQueue implements Serializable
{
/** @var int Seed used to ensure queue order for items of the same priority */
protected $serial = PHP_INT_MAX;
/**
* Insert a value with a given priority
*
* Utilizes {@var $serial} to ensure that values of equal priority are
* emitted in the same order in which they are inserted.
*
* @param TValue $value
* @param TPriority $priority
*/
#[ReturnTypeWillChange] // Inherited return type should be bool
public function insert($value, $priority)
{
if (!is_array($priority)) {
$priority = [$priority, $this->serial--];
}
parent::insert($value, $priority);
}
/**
* Serialize to an array
*
* Array will be priority => data pairs
*
* @return list<TValue>
*/
public function toArray()
{
$array = [];
foreach (clone $this as $item) {
$array[] = $item;
}
return $array;
}
/**
* Serialize
*
* @return string
*/
public function serialize()
{
return serialize($this->__serialize());
}
/**
* Magic method used for serializing of an instance.
*
* @return array
*/
public function __serialize()
{
$clone = clone $this;
$clone->setExtractFlags(self::EXTR_BOTH);
$data = [];
foreach ($clone as $item) {
$data[] = $item;
}
return $data;
}
/**
* Deserialize
*
* @param string $data
*/
public function unserialize($data)
{
$toUnserialize = unserialize($data);
if (!is_array($toUnserialize)) {
throw new UnexpectedValueException(sprintf(
'Cannot deserialize %s instance; corrupt serialization data',
self::class,
));
}
$this->__unserialize($toUnserialize);
}
/**
* Magic method used to rebuild an instance.
*
* @param array<array-key, mixed> $data Data array.
*/
public function __unserialize($data)
{
$this->serial = PHP_INT_MAX;
foreach ($data as $item) {
if (!is_array($item)) {
throw new UnexpectedValueException(sprintf(
'Cannot deserialize %s instance: corrupt item; expected array, received %s',
self::class,
get_debug_type($item),
));
}
if (!array_key_exists('data', $item)) {
throw new UnexpectedValueException(sprintf(
'Cannot deserialize %s instance: corrupt item; missing "data" element',
self::class,
));
}
$priority = 1;
if (array_key_exists('priority', $item)) {
$priority = (int) $item['priority'];
}
$this->insert($item['data'], $priority);
}
}
}