SplPriorityQueue.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. declare(strict_types=1);
  3. namespace Laminas\Stdlib;
  4. use ReturnTypeWillChange;
  5. use Serializable;
  6. use UnexpectedValueException;
  7. use function array_key_exists;
  8. use function get_debug_type;
  9. use function is_array;
  10. use function serialize;
  11. use function sprintf;
  12. use function unserialize;
  13. use const PHP_INT_MAX;
  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. * @template TValue
  21. * @template TPriority of int
  22. * @extends \SplPriorityQueue<TPriority, TValue>
  23. */
  24. class SplPriorityQueue extends \SplPriorityQueue implements Serializable
  25. {
  26. /** @var int Seed used to ensure queue order for items of the same priority */
  27. protected $serial = PHP_INT_MAX;
  28. /**
  29. * Insert a value with a given priority
  30. *
  31. * Utilizes {@var $serial} to ensure that values of equal priority are
  32. * emitted in the same order in which they are inserted.
  33. *
  34. * @param TValue $value
  35. * @param TPriority $priority
  36. * @return void
  37. */
  38. #[ReturnTypeWillChange] // Inherited return type should be bool
  39. public function insert($value, $priority)
  40. {
  41. if (! is_array($priority)) {
  42. $priority = [$priority, $this->serial--];
  43. }
  44. parent::insert($value, $priority);
  45. }
  46. /**
  47. * Serialize to an array
  48. *
  49. * Array will be priority => data pairs
  50. *
  51. * @return list<TValue>
  52. */
  53. public function toArray()
  54. {
  55. $array = [];
  56. foreach (clone $this as $item) {
  57. $array[] = $item;
  58. }
  59. return $array;
  60. }
  61. /**
  62. * Serialize
  63. *
  64. * @return string
  65. */
  66. public function serialize()
  67. {
  68. return serialize($this->__serialize());
  69. }
  70. /**
  71. * Magic method used for serializing of an instance.
  72. *
  73. * @return array
  74. */
  75. public function __serialize()
  76. {
  77. $clone = clone $this;
  78. $clone->setExtractFlags(self::EXTR_BOTH);
  79. $data = [];
  80. foreach ($clone as $item) {
  81. $data[] = $item;
  82. }
  83. return $data;
  84. }
  85. /**
  86. * Deserialize
  87. *
  88. * @param string $data
  89. * @return void
  90. */
  91. public function unserialize($data)
  92. {
  93. $toUnserialize = unserialize($data);
  94. if (! is_array($toUnserialize)) {
  95. throw new UnexpectedValueException(sprintf(
  96. 'Cannot deserialize %s instance; corrupt serialization data',
  97. self::class
  98. ));
  99. }
  100. $this->__unserialize($toUnserialize);
  101. }
  102. /**
  103. * Magic method used to rebuild an instance.
  104. *
  105. * @param array<array-key, mixed> $data Data array.
  106. * @return void
  107. */
  108. public function __unserialize($data)
  109. {
  110. $this->serial = PHP_INT_MAX;
  111. foreach ($data as $item) {
  112. if (! is_array($item)) {
  113. throw new UnexpectedValueException(sprintf(
  114. 'Cannot deserialize %s instance: corrupt item; expected array, received %s',
  115. self::class,
  116. get_debug_type($item)
  117. ));
  118. }
  119. if (! array_key_exists('data', $item)) {
  120. throw new UnexpectedValueException(sprintf(
  121. 'Cannot deserialize %s instance: corrupt item; missing "data" element',
  122. self::class
  123. ));
  124. }
  125. $priority = 1;
  126. if (array_key_exists('priority', $item)) {
  127. $priority = (int) $item['priority'];
  128. }
  129. $this->insert($item['data'], $priority);
  130. }
  131. }
  132. }