SplStack.php 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. namespace Zend\Stdlib;
  10. use Serializable;
  11. /**
  12. * Serializable version of SplStack
  13. */
  14. class SplStack extends \SplStack implements Serializable
  15. {
  16. /**
  17. * Serialize to an array representing the stack
  18. *
  19. * @return array
  20. */
  21. public function toArray()
  22. {
  23. $array = [];
  24. foreach ($this as $item) {
  25. $array[] = $item;
  26. }
  27. return $array;
  28. }
  29. /**
  30. * Serialize
  31. *
  32. * @return string
  33. */
  34. public function serialize()
  35. {
  36. return serialize($this->toArray());
  37. }
  38. /**
  39. * Unserialize
  40. *
  41. * @param string $data
  42. * @return void
  43. */
  44. public function unserialize($data)
  45. {
  46. foreach (unserialize($data) as $item) {
  47. $this->unshift($item);
  48. }
  49. }
  50. }