Created
August 16, 2014 22:39
-
-
Save jm42/6820f82276a20382973d to your computer and use it in GitHub Desktop.
Search Indexing
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
<?php | |
namespace Froebel; | |
use Froebel\Hash\Hash; | |
use Froebel\Storage\Storage; | |
class Engine | |
{ | |
protected $hashes; | |
protected $storage; | |
public function __construct($hashes, Storage $storage) | |
{ | |
$this->hashes = $hashes; | |
$this->storage = $storage; | |
} | |
public function index($data) | |
{ | |
foreach ($this->hashes as $hash) { | |
if (!$hash->supports($data)) { | |
continue; | |
} | |
$keys = $hash->tokenize($data); | |
foreach ($keys as $key) { | |
$this->storage->set($hash->name, $key, $data); | |
} | |
} | |
} | |
} | |
namespace Froebel\Hash; | |
interface Hash | |
{ | |
public function supports($data); | |
public function tokenize($data); | |
} | |
class ObjectHash implements Hash | |
{ | |
public $name; | |
public function __construct($name) | |
{ | |
$this->name = $name; | |
} | |
public function supports($data) | |
{ | |
return is_object($data); | |
} | |
public function tokenize($data) | |
{ | |
return array(spl_object_hash($data)); | |
} | |
} | |
namespace Froebel\Storage; | |
interface Storage | |
{ | |
public function set($name, $key, $data); | |
public function get($name, $key); | |
} | |
class MemoryStorage implements Storage | |
{ | |
protected $buckets = array(); | |
public function set($name, $key, $data) | |
{ | |
if (!array_key_exists($name, $this->buckets)) { | |
$this->buckets[$name] = array(); | |
} | |
if (!array_key_exists($key, $this->buckets[$name])) { | |
$this->buckets[$name][$key] = array(); | |
} | |
$this->buckets[$name][$key][] = $data; | |
} | |
public function get($name, $key) | |
{ | |
if (isset($this->buckets[$name][$key])) { | |
return $this->buckets[$name][$key]; | |
} | |
return 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
<?php | |
use Froebel\Engine; | |
use Froebel\Hash\ObjectHash; | |
use Froebel\Storage\MemoryStorage; | |
class EngineTest extends PHPUnit_Framework_TestCase | |
{ | |
public function testIndexNotSupport() | |
{ | |
$storage = $this->getMock('\Froebel\Storage\MemoryStorage', array('set')); | |
$storage->expects($this->never())->method('set'); | |
$engine = new Engine(array(new ObjectHash('a')), $storage); | |
$engine->index(42); | |
} | |
public function testIndex() | |
{ | |
$data = new \stdClass(); | |
$data->answer = 42; | |
$expected = spl_object_hash($data); | |
$storage = $this->getMock('\Froebel\Storage\MemoryStorage', array('set')); | |
$storage->expects($this->once()) | |
->method('set') | |
->with($this->equalTo('a'), $this->equalTo($expected)); | |
$engine = new Engine(array(new ObjectHash('a')), $storage); | |
$engine->index($data); | |
} | |
} | |
class ObjectHashTest extends PHPUnit_Framework_TestCase | |
{ | |
public function testSupports() | |
{ | |
$hash = new ObjectHash('test'); | |
$this->assertFalse($hash->supports(array())); | |
$this->assertFalse($hash->supports('sun')); | |
$this->assertFalse($hash->supports(123)); | |
$this->assertTrue($hash->supports(new stdClass())); | |
} | |
public function testTokenize() | |
{ | |
$hash = new ObjectHash('test'); | |
$obj = new stdClass(); | |
$this->assertSame(array(spl_object_hash($obj)), $hash->tokenize($obj)); | |
} | |
} | |
class MemoryStorageTest extends PHPUnit_Framework_TestCase | |
{ | |
public function testSet() | |
{ | |
$storage = new MemoryStorage(); | |
$storage->set('person', '1', 'Juan M'); | |
$this->assertSame(array('Juan M'), $storage->get('person', '1')); | |
} | |
public function testGetEmpty() | |
{ | |
$storage = new MemoryStorage(); | |
$storage->set('car', 'fiat-strada', array('brand' => 'Fiat', 'model' => 'Strada')); | |
$this->assertSame(array(), $storage->get('person', '1')); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment