3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
10 namespace Zend\Stdlib;
15 * Serializable version of SplPriorityQueue
17 * Also, provides predictable heap order for datums added with the same priority
18 * (i.e., they will be emitted in the same order they are enqueued).
20 class SplPriorityQueue extends \SplPriorityQueue implements Serializable
23 * @var int Seed used to ensure queue order for items of the same priority
25 protected $serial = PHP_INT_MAX;
28 * Insert a value with a given priority
30 * Utilizes {@var $serial} to ensure that values of equal priority are
31 * emitted in the same order in which they are inserted.
34 * @param mixed $priority
37 public function insert($datum, $priority)
39 if (! is_array($priority)) {
40 $priority = [$priority, $this->serial--];
42 parent::insert($datum, $priority);
46 * Serialize to an array
48 * Array will be priority => data pairs
52 public function toArray()
55 foreach (clone $this as $item) {
66 public function serialize()
69 $clone->setExtractFlags(self::EXTR_BOTH);
72 foreach ($clone as $item) {
76 return serialize($data);
85 public function unserialize($data)
87 foreach (unserialize($data) as $item) {
88 $this->insert($item['data'], $item['priority']);