This repository has been archived by the owner on Jan 31, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added serializable SPL data structures
- Extended SplQueue, SplStack, and SplPriorityQueue to add the ability to serialize each, as well as provide toArray() methods.
- Loading branch information
Showing
6 changed files
with
299 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |