Commits

Jiří Knesl committed 0cb93c4

Add serializer class

Comments (0)

Files changed (9)

lib/PHPersist/IndexManager.php

 	 */
 	public function regenerateItemInIndex($bucket, $id, $value)
 	{
+		$this->removeItemFromIndex($bucket, $id);
 		if (isset($this->indexes[$bucket]) && is_array($this->indexes[$bucket]))
 		foreach ($this->indexes[$bucket] as $name => $method) {
-			
 			if (null !== $indexValue = $method($value)) {
 				if (! is_array($this->indexData[$bucket][$name][$indexValue]))
 					$this->indexData[$bucket][$name][$indexValue] = array();

lib/PHPersist/Serializer/Interface.php

+<?php
+
+
+interface PHPersist_Serializer_Interface
+{
+	public function serialize($value);
+	public function unserialize($value);
+}

lib/PHPersist/Serializer/PHP.php

+<?php 
+
+require_once dirname(__FILE__) . "/Interface.php";
+
+class PHPersist_Serializer_PHP implements PHPersist_Serializer_Interface
+{
+
+	public function serialize($value)
+	{
+		if (false === $value)
+			return new PHPersist_Serializer_FalseValue;
+		else if (null === $value)
+			return new PHPersist_Serializer_NullValue;
+		else
+			return serialize($value);
+	}
+
+	public function unserialize($value)
+	{
+		if ($value instanceof PHPersist_Serializer_FalseValue)
+			return false;
+		else if ($value instanceof PHPersist_Serializer_NullValue)
+			return null;
+		else
+			return unserialize($value);
+	}
+
+}

lib/PHPersist/Storage.php

 	 * @var PHPersist_IndexManager_Interface
 	 **/
 	protected $indexManager;
+
+	/**
+	 * Serializer
+	 *
+	 * @var PHPErsist_Serializer_Interface
+	 */
+	protected $serializer;
 	
 	/**
 	 * Storage constructor
 	 * @return void
 	 * @author Jiří Knesl
 	 **/
-	public function __construct(PHPersist_Storage_Interface $storage, PHPersist_IndexManager_Interface $indexManager)
+	public function __construct(PHPersist_Storage_Interface $storage, PHPersist_IndexManager_Interface $indexManager, PHPErsist_Serializer_Interface $serializer)
 	{
 		$this->storage = $storage;
 		$this->indexManager = $indexManager;
 		$this->indexManager->setStorage($this);
+		$this->serializer = $serializer;
 	}
 	
 	/**
 		// @todo refactor to IndexManager
 		$this->getIndexManager()->regenerateItemInIndex($bucket, $id, $value);
 		
-		return $this->storage->save($bucket, $id, serialize($value));
+		return $this->storage->save($bucket, $id, $this->serializer->serialize($value));
 	}
 	
 	/**
 		if (null === $content || false === $content)
 			return null;
 		else
-			return unserialize($content);
+			return $this->serializer->unserialize($content);
 	}
 	
 	/**
 	 * Validate bucket and id name, if not OK, returns false
 	 *
+	 * @param string $bucket bucket name
+	 * @param string|null $id id of record (can be ommitted)
+	 *
 	 * @return bool
 	 * @author Jiří Knesl
 	 **/

tests/IndexManagerTest.php

 	 */
 	protected function setUp()
 	{
-		$this->storage = new PHPersist_Storage(new PHPersist_Storage_File(dirname(__FILE__) . "/ValueIndexTest"), new PHPersist_IndexManager);
+
+		$this->storage = new PHPersist_Storage(new PHPersist_Storage_File(dirname(__FILE__) . "/ValueIndexTest"), new PHPersist_IndexManager, new PHPersist_Serializer_PHP);
 	}
 
 	/**
 		
 		$this->storage->getIndexManager()->addIndex("articles", "seo", $indexFunction);
 		
-		$obja = (object)array("seo"=>"ab", "id"=>1);
+		var_dump($this->storage->getAdapter()->getBucket("articles"));
+		$obja = (object)array("seo"=>"ab", "id"=>333);
 		$this->storage->save("articles", $obja->id, $obja);
 		
 		$objb = (object)array("seo"=>"ab", "id"=>2);
 
 		$this->storage->getIndexManager()->removeIndex("articles", "seo");
 
-		$this->storage->del("articles", 1);
+		var_dump($this->storage->getAdapter()->getBucket("articles"));
+
+		$this->storage->del("articles", 333);
 
 		$this->storage->getIndexManager()->addIndex("articles", "seo", $indexFunction);
 		$this->storage->getIndexManager()->regenerateIndex("articles", "seo");
 		
 		$result = $this->storage->getIndexManager()->loadFromIndex("articles", "seo", "ab");
 		
-		// var_dump($result);
+		var_dump($result);
 		
 		$this->assertEquals(1, sizeof($result));
 		foreach ($result as $item)

tests/StorageTest.php

 			}, glob($item . '/*'));
 		}, glob(dirname(__FILE__) . "/StorageTest/*"));
 		
-		$this->object = new PHPersist_Storage(new PHPersist_Storage_File(dirname(__FILE__) . "/StorageTest"), new PHPersist_IndexManager);
+		$this->object = new PHPersist_Storage(new PHPersist_Storage_File(dirname(__FILE__) . "/StorageTest"), new PHPersist_IndexManager, new PHPersist_Serializer_PHP);
 	}
 
 	public function testSaveAndLoad()

tests/ValidateBucketAndIdTest.php

 	 */
 	protected function setUp()
 	{
-		$this->object = new PHPersist_Storage(new PHPersist_Storage_File(dirname(__FILE__) . "/ValueIndexTest"), new PHPersist_IndexManager);
+		$this->object = new PHPersist_Storage(new PHPersist_Storage_File(dirname(__FILE__) . "/ValueIndexTest"), new PHPersist_IndexManager, new PHPersist_Serializer_PHP);
 	}
 
 	/**

tests/ValueIndexTest/articles/333

+O:8:"stdClass":2:{s:3:"seo";s:2:"ab";s:2:"id";i:333;}

tests/bootstrap.php

 require_once dirname(dirname(__FILE__)) . "/lib/PHPersist/IndexManager/Interface.php";
 require_once dirname(dirname(__FILE__)) . "/lib/PHPersist/IndexManager.php";
 
+require_once dirname(dirname(__FILE__)) . "/lib/PHPersist/Serializer/Interface.php";
+require_once dirname(dirname(__FILE__)) . "/lib/PHPersist/Serializer/NullValue.php";
+require_once dirname(dirname(__FILE__)) . "/lib/PHPersist/Serializer/FalseValue.php";
+
+require_once dirname(dirname(__FILE__)) . "/lib/PHPersist/Serializer/PHP.php";
+
 require_once dirname(dirname(__FILE__)) . "/lib/PHPersist/Storage.php";