Skip to content
This repository has been archived by the owner on Jan 31, 2020. It is now read-only.

Commit

Permalink
Added serializable SPL data structures
Browse files Browse the repository at this point in the history
- Extended SplQueue, SplStack, and SplPriorityQueue to add the ability to
  serialize each, as well as provide toArray() methods.
  • Loading branch information
weierophinney committed Aug 4, 2010
2 parents 455a3b5 + 4249c17 commit c4c0c95
Show file tree
Hide file tree
Showing 6 changed files with 299 additions and 0 deletions.
77 changes: 77 additions & 0 deletions src/SplPriorityQueue.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php
namespace Zend\Stdlib;

class SplPriorityQueue extends \SplPriorityQueue
{
/**
* @var array Used for serialization
*/
private $_data = array();

/**
* Serialize to an array
*
* Array will be priority => data pairs
*
* @return array
*/
public function toArray()
{
$this->setExtractFlags(self::EXTR_BOTH);
$array = array();
while ($this->valid()) {
$array[] = $this->current();
$this->next();
}
$this->setExtractFlags(self::EXTR_DATA);

// Iterating through a priority queue removes items
foreach ($array as $item) {
$this->insert($item['data'], $item['priority']);
}

// Return only the data
$return = array();
foreach ($array as $item) {
$return[$item['priority']] = $item['data'];
}

return $return;
}

/**
* Serialize
*
* @return array
*/
public function __sleep()
{
$this->_data = array();
$this->setExtractFlags(self::EXTR_BOTH);
while ($this->valid()) {
$this->_data[] = $this->current();
$this->next();
}
$this->setExtractFlags(self::EXTR_DATA);

// Iterating through a priority queue removes items
foreach ($this->_data as $item) {
$this->insert($item['data'], $item['priority']);
}

return array('_data');
}

/**
* Deserialize
*
* @return void
*/
public function __wakeup()
{
foreach ($this->_data as $item) {
$this->insert($item['data'], $item['priority']);
}
$this->_data = array();
}
}
48 changes: 48 additions & 0 deletions src/SplQueue.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php
namespace Zend\Stdlib;

class SplQueue extends \SplQueue
{
/**
* @var array Used for serialization
*/
private $_data = array();

/**
* Return an array representing the queue
*
* @return array
*/
public function toArray()
{
$array = array();
foreach ($this as $item) {
$array[] = $item;
}
return $array;
}

/**
* Serialize
*
* @return array
*/
public function __sleep()
{
$this->_data = $this->toArray();
return array('_data');
}

/**
* Unserialize
*
* @return void
*/
public function __wakeup()
{
foreach ($this->_data as $item) {
$this->push($item);
}
$this->_data = array();
}
}
48 changes: 48 additions & 0 deletions src/SplStack.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php
namespace Zend\Stdlib;

class SplStack extends \SplStack
{
/**
* @var array Used in serialization
*/
private $_data = array();

/**
* Serialize to an array representing the stack
*
* @return void
*/
public function toArray()
{
$array = array();
foreach ($this as $item) {
$array[] = $item;
}
return $array;
}

/**
* Serialize
*
* @return array
*/
public function __sleep()
{
$this->_data = $this->toArray();
return array('_data');
}

/**
* Unserialize
*
* @return void
*/
public function __wakeup()
{
foreach ($this->_data as $item) {
$this->unshift($item);
}
$this->_data = array();
}
}
46 changes: 46 additions & 0 deletions test/SplPriorityQueueTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php
namespace ZendTest\Stdlib;

use Zend\Stdlib\SplPriorityQueue;

class SplPriorityQueueTest extends \PHPUnit_Framework_TestCase
{
public function setUp()
{
$this->queue = new SplPriorityQueue();
$this->queue->insert('foo', 3);
$this->queue->insert('bar', 4);
$this->queue->insert('baz', 2);
$this->queue->insert('bat', 1);
}

public function testSerializationAndDeserializationShouldMaintainState()
{
$s = serialize($this->queue);
$unserialized = unserialize($s);
$count = count($this->queue);
$this->assertSame($count, count($unserialized), 'Expected count ' . $count . '; received ' . count($unserialized));

$expected = array();
foreach ($this->queue as $item) {
$expected[] = $item;
}
$test = array();
foreach ($unserialized as $item) {
$test[] = $item;
}
$this->assertSame($expected, $test, 'Expected: ' . var_export($expected, 1) . "\nReceived:" . var_export($test, 1));
}

public function testCanRetrieveQueueAsArray()
{
$expected = array(
4 => 'bar',
3 => 'foo',
2 => 'baz',
1 => 'bat',
);
$test = $this->queue->toArray();
$this->assertSame($expected, $test, var_export($test, 1));
}
}
39 changes: 39 additions & 0 deletions test/SplQueueTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php
namespace ZendTest\Stdlib;

use Zend\Stdlib\SplQueue;

class SplQueueTest extends \PHPUnit_Framework_TestCase
{
public function setUp()
{
$this->queue = new SplQueue();
$this->queue->push('foo');
$this->queue->push('bar');
$this->queue->push('baz');
}

public function testSerializationAndDeserializationShouldMaintainState()
{
$s = serialize($this->queue);
$unserialized = unserialize($s);
$count = count($this->queue);
$this->assertSame($count, count($unserialized));

$expected = array();
foreach ($this->queue as $item) {
$expected[] = $item;
}
$test = array();
foreach ($unserialized as $item) {
$test[] = $item;
}
$this->assertSame($expected, $test);
}

public function testCanRetrieveQueueAsArray()
{
$expected = array('foo', 'bar', 'baz');
$this->assertSame($expected, $this->queue->toArray());
}
}
41 changes: 41 additions & 0 deletions test/SplStackTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php
namespace ZendTest\Stdlib;

use Zend\Stdlib\SplStack;

class SplStackTest extends \PHPUnit_Framework_TestCase
{
public function setUp()
{
$this->stack = new SplStack();
$this->stack->push('foo');
$this->stack->push('bar');
$this->stack->push('baz');
$this->stack->push('bat');
}

public function testSerializationAndDeserializationShouldMaintainState()
{
$s = serialize($this->stack);
$unserialized = unserialize($s);
$count = count($this->stack);
$this->assertSame($count, count($unserialized));

$expected = array();
foreach ($this->stack as $item) {
$expected[] = $item;
}
$test = array();
foreach ($unserialized as $item) {
$test[] = $item;
}
$this->assertSame($expected, $test);
}

public function testCanRetrieveQueueAsArray()
{
$expected = array('bat', 'baz', 'bar', 'foo');
$test = $this->stack->toArray();
$this->assertSame($expected, $test, var_export($test, 1));
}
}

0 comments on commit c4c0c95

Please sign in to comment.