From 753be98161a2ae04773db70d77e8193d561e302a Mon Sep 17 00:00:00 2001 From: James Harris Date: Fri, 8 Feb 2013 09:37:18 +1000 Subject: [PATCH] Map and Set now serialize their hash function (fixes #23) --- .../Collections/AssociativeKeyGenerator.php | 2 +- lib/Icecave/Collections/LinkedList.php | 2 +- lib/Icecave/Collections/Map.php | 13 +++++++++---- lib/Icecave/Collections/PriorityQueue.php | 2 +- lib/Icecave/Collections/Queue.php | 2 +- lib/Icecave/Collections/Set.php | 13 +++++++++---- lib/Icecave/Collections/Stack.php | 2 +- lib/Icecave/Collections/Vector.php | 2 +- test/suite/Icecave/Collections/MapTest.php | 15 +++++++++++++++ test/suite/Icecave/Collections/SetTest.php | 15 +++++++++++++++ 10 files changed, 54 insertions(+), 14 deletions(-) diff --git a/lib/Icecave/Collections/AssociativeKeyGenerator.php b/lib/Icecave/Collections/AssociativeKeyGenerator.php index afef790..1c4ed93 100644 --- a/lib/Icecave/Collections/AssociativeKeyGenerator.php +++ b/lib/Icecave/Collections/AssociativeKeyGenerator.php @@ -29,7 +29,7 @@ public function __construct($arrayHashFunction = 'md5', $objectHashFunction = 's */ public function __invoke($value) { - $this->typeCheck->__invoke(func_get_args()); + $this->typeCheck->validateInvoke(func_get_args()); return $this->generate($value); } diff --git a/lib/Icecave/Collections/LinkedList.php b/lib/Icecave/Collections/LinkedList.php index bd6ae6c..0758d7a 100644 --- a/lib/Icecave/Collections/LinkedList.php +++ b/lib/Icecave/Collections/LinkedList.php @@ -97,7 +97,7 @@ public function isEmpty() */ public function __toString() { - $this->typeCheck->__toString(func_get_args()); + $this->typeCheck->validateToString(func_get_args()); if ($this->isEmpty()) { return ''; diff --git a/lib/Icecave/Collections/Map.php b/lib/Icecave/Collections/Map.php index c603fca..dec8a0e 100644 --- a/lib/Icecave/Collections/Map.php +++ b/lib/Icecave/Collections/Map.php @@ -72,7 +72,7 @@ public function isEmpty() */ public function __toString() { - $this->typeCheck->__toString(func_get_args()); + $this->typeCheck->validateToString(func_get_args()); if ($this->isEmpty()) { return ''; @@ -1031,7 +1031,12 @@ public function serialize() { $this->typeCheck->serialize(func_get_args()); - return serialize($this->elements()); + return serialize( + array( + $this->elements(), + $this->hashFunction + ) + ); } /** @@ -1041,9 +1046,9 @@ public function unserialize($packet) { TypeCheck::get(__CLASS__)->unserialize(func_get_args()); - $elements = unserialize($packet); + list($elements, $hashFunction) = unserialize($packet); - $this->__construct(); + $this->__construct(null, $hashFunction); foreach ($elements as $element) { list($key, $value) = $element; diff --git a/lib/Icecave/Collections/PriorityQueue.php b/lib/Icecave/Collections/PriorityQueue.php index 8db1489..b3a2612 100644 --- a/lib/Icecave/Collections/PriorityQueue.php +++ b/lib/Icecave/Collections/PriorityQueue.php @@ -42,7 +42,7 @@ public function __construct($prioritizer, $collection = null) */ public function __toString() { - $this->typeCheck->__toString(func_get_args()); + $this->typeCheck->validateToString(func_get_args()); if ($this->isEmpty()) { return ''; diff --git a/lib/Icecave/Collections/Queue.php b/lib/Icecave/Collections/Queue.php index 6e76ebd..9cd3afa 100644 --- a/lib/Icecave/Collections/Queue.php +++ b/lib/Icecave/Collections/Queue.php @@ -70,7 +70,7 @@ public function isEmpty() */ public function __toString() { - $this->typeCheck->__toString(func_get_args()); + $this->typeCheck->validateToString(func_get_args()); if ($this->isEmpty()) { return ''; diff --git a/lib/Icecave/Collections/Set.php b/lib/Icecave/Collections/Set.php index 6d0b2a3..36c33e7 100644 --- a/lib/Icecave/Collections/Set.php +++ b/lib/Icecave/Collections/Set.php @@ -71,7 +71,7 @@ public function isEmpty() */ public function __toString() { - $this->typeCheck->__toString(func_get_args()); + $this->typeCheck->validateToString(func_get_args()); if ($this->isEmpty()) { return ''; @@ -311,7 +311,12 @@ public function serialize() { $this->typeCheck->serialize(func_get_args()); - return serialize($this->elements()); + return serialize( + array( + $this->elements(), + $this->hashFunction + ) + ); } /** @@ -321,8 +326,8 @@ public function unserialize($packet) { TypeCheck::get(__CLASS__)->unserialize(func_get_args()); - $elements = unserialize($packet); - $this->__construct($elements); + list($elements, $hashFunction) = unserialize($packet); + $this->__construct($elements, $hashFunction); } //////////////////////////// diff --git a/lib/Icecave/Collections/Stack.php b/lib/Icecave/Collections/Stack.php index a53467a..ccde3d9 100644 --- a/lib/Icecave/Collections/Stack.php +++ b/lib/Icecave/Collections/Stack.php @@ -70,7 +70,7 @@ public function isEmpty() */ public function __toString() { - $this->typeCheck->__toString(func_get_args()); + $this->typeCheck->validateToString(func_get_args()); if ($this->isEmpty()) { return ''; diff --git a/lib/Icecave/Collections/Vector.php b/lib/Icecave/Collections/Vector.php index f4149ef..22b592b 100644 --- a/lib/Icecave/Collections/Vector.php +++ b/lib/Icecave/Collections/Vector.php @@ -73,7 +73,7 @@ public function isEmpty() */ public function __toString() { - $this->typeCheck->__toString(func_get_args()); + $this->typeCheck->validateToString(func_get_args()); if ($this->isEmpty()) { return ''; diff --git a/test/suite/Icecave/Collections/MapTest.php b/test/suite/Icecave/Collections/MapTest.php index 4c9e31b..46703f4 100644 --- a/test/suite/Icecave/Collections/MapTest.php +++ b/test/suite/Icecave/Collections/MapTest.php @@ -1,6 +1,7 @@ assertSame($this->_collection->elements(), $collection->elements()); } + /** + * @group regression + * @link https://github.com/IcecaveStudios/collections/issues/23 + */ + public function testSerializationOfHashFunction() + { + $collection = new Map(null, 'sha1'); + + $packet = serialize($collection); + $collection = unserialize($packet); + + $this->assertSame('sha1', Liberator::liberate($collection)->hashFunction); + } + /////////////////////////////////////////// // Implementation of CollectionInterface // /////////////////////////////////////////// diff --git a/test/suite/Icecave/Collections/SetTest.php b/test/suite/Icecave/Collections/SetTest.php index 0242fb2..0d74d54 100644 --- a/test/suite/Icecave/Collections/SetTest.php +++ b/test/suite/Icecave/Collections/SetTest.php @@ -1,6 +1,7 @@ assertSame($this->_collection->elements(), $collection->elements()); } + /** + * @group regression + * @link https://github.com/IcecaveStudios/collections/issues/23 + */ + public function testSerializationOfHashFunction() + { + $collection = new Set(null, 'sha1'); + + $packet = serialize($collection); + $collection = unserialize($packet); + + $this->assertSame('sha1', Liberator::liberate($collection)->hashFunction); + } + /////////////////////////////////////////// // Implementation of CollectionInterface // ///////////////////////////////////////////