Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / vendor / zendframework / zend-stdlib / src / SplPriorityQueue.php
1 <?php
2 /**
3  * Zend Framework (http://framework.zend.com/)
4  *
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
8  */
9
10 namespace Zend\Stdlib;
11
12 use Serializable;
13
14 /**
15  * Serializable version of SplPriorityQueue
16  *
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).
19  */
20 class SplPriorityQueue extends \SplPriorityQueue implements Serializable
21 {
22     /**
23      * @var int Seed used to ensure queue order for items of the same priority
24      */
25     protected $serial = PHP_INT_MAX;
26
27     /**
28      * Insert a value with a given priority
29      *
30      * Utilizes {@var $serial} to ensure that values of equal priority are
31      * emitted in the same order in which they are inserted.
32      *
33      * @param  mixed $datum
34      * @param  mixed $priority
35      * @return void
36      */
37     public function insert($datum, $priority)
38     {
39         if (! is_array($priority)) {
40             $priority = [$priority, $this->serial--];
41         }
42         parent::insert($datum, $priority);
43     }
44
45     /**
46      * Serialize to an array
47      *
48      * Array will be priority => data pairs
49      *
50      * @return array
51      */
52     public function toArray()
53     {
54         $array = [];
55         foreach (clone $this as $item) {
56             $array[] = $item;
57         }
58         return $array;
59     }
60
61     /**
62      * Serialize
63      *
64      * @return string
65      */
66     public function serialize()
67     {
68         $clone = clone $this;
69         $clone->setExtractFlags(self::EXTR_BOTH);
70
71         $data = [];
72         foreach ($clone as $item) {
73             $data[] = $item;
74         }
75
76         return serialize($data);
77     }
78
79     /**
80      * Deserialize
81      *
82      * @param  string $data
83      * @return void
84      */
85     public function unserialize($data)
86     {
87         $this->serial = PHP_INT_MAX;
88         foreach (unserialize($data) as $item) {
89             $this->serial--;
90             $this->insert($item['data'], $item['priority']);
91         }
92     }
93 }