Skip to content
This repository has been archived by the owner. It is now read-only.
Permalink
Browse files

Merge branch 'forage-0.2-compat'

  • Loading branch information...
vierbergenlars committed Oct 5, 2013
2 parents cb9e8cf + c61dde9 commit 40228f4051d2db7cb002435b58ae8da9b4ea5eba
@@ -4,6 +4,7 @@
use vierbergenlars\Forage\ODM\HydrationSettingsInterface;
use Defer\Object as Defer;
use vierbergenlars\Forage\ODM\SearchHit;
/**
* Base class for hydration strategies that use vierbergenlars/defer to hydrate their objects
@@ -13,31 +14,35 @@ abstract class DeferHydration implements HydrationSettingsInterface
/**
* Gets the properties to be injected in the object
* @param string|int $id The id of the document received from the search query
* @param array $document The document received from the search query
* @return array A map of object properties to values
*/
abstract public function getParameters(array $document);
abstract public function getParameters($id, array $document);
/**
* Gets the class name of the object that will be hydrated
* @param string|int $id The id of the document received from the search query
* @param array $document The document received from the search query
* @return string A fully qualified class name. (Should implement {@link Defer\Deferrable})
*/
abstract public function getClass(array $document);
abstract public function getClass($id, array $document);
/**
* Gets the object from a document
* @param array $document
* @return object
*/
public function getObject(array $document)
public function getObject(SearchHit $hit)
{
$document = $hit->getDocument();
array_walk($document, function(&$field) {
if(is_array($field))
$field = new \ArrayObject($field);
});
$data = $this->getParameters($document);
$class = $this->getClass($document);
$id = $hit->getId();
$data = $this->getParameters($id, $document);
$class = $this->getClass($id, $document);
return Defer::defer($data, $class);
}
@@ -30,7 +30,7 @@ public function __construct($className)
* {@inheritDoc}
* @internal
*/
public function getClass(array $document)
public function getClass($id, array $document)
{
return $this->className;
}
@@ -39,21 +39,43 @@ public function getClass(array $document)
* {@inheritDoc}
* @internal
*/
public function getParameters(array $document)
public function getParameters($id, array $document)
{
return $document;
return array_merge($document, array('id'=>$id));
}
/**
* {@InheritDoc}
* Gets the document array from the object
* @param \vierbergenlars\Forage\ODM\Indexable $document
* @internal
*/
public function getDocument($document)
private function getDocArray($document)
{
if(!is_a($document, $this->className))
throw new \LogicException('Document should be of type ' . $this->className . ', got ' . get_class($document));
return $document->toDocument();
}
/**
* {@InheritDoc}
* @param \vierbergenlars\Forage\ODM\Indexable $document
* @internal
*/
public function getDocument($document)
{
$doc = $this->getDocArray($document);
unset($doc['id']);
return $doc;
}
/**
* {@InheritDoc}
* @param \vierbergenlars\Forage\ODM\Indexable $document
* @internal
*/
public function getDocumentId($document)
{
$doc = $this->getDocArray($document);
return $doc['id'];
}
}
@@ -14,10 +14,17 @@ interface HydrationSettingsInterface
*/
public function getDocument($document);
/**
* Gets the id of the document from the object
* @param object $document
* @return string|int The id of the document to be stored in the database
*/
public function getDocumentId($document);
/**
* Gets the object from a document
* @param array $document
* @return object
* @return SearchHit
*/
public function getObject(array $document);
public function getObject(SearchHit $document);
}
@@ -17,6 +17,6 @@ class SearchHit extends Hit
*/
public function hydrate(HydrationSettingsInterface $hydrationSettings)
{
$this->document = $hydrationSettings->getObject($this->document);
$this->document = $hydrationSettings->getObject($this);
}
}
@@ -33,11 +33,15 @@ public function __construct(TransportInterface $transport,
* {@inheritDoc}
*
* @param object $document
* @param null $_ Unused parameter
* @return \vierbergenlars\Forage\ODM\SearchIndex
*/
public function addDocument($document)
public function addDocument($document, $_ = null)
{
parent::addDocument($this->hydrationSettings->getDocument($document));
parent::addDocument(
$this->hydrationSettings->getDocumentId($document),
$this->hydrationSettings->getDocument($document)
);
return $this;
}
@@ -48,13 +52,15 @@ public function addDocument($document)
* When the document to remove is given as a string or an integer,
* it is used as an id. If it is an object, {@link HydrationSettingsInterface::getDocument()}
* gets called, and the document id will be determined from the returned array.
* @param null $_ Unused parameter
* @return \vierbergenlars\Forage\ODM\SearchIndex
*/
public function removeDocument($document)
public function removeDocument($document, $_ = null)
{
if(is_object($document)) {
$doc = $this->hydrationSettings->getDocument($document);
parent::removeDocument($doc['id']);
parent::removeDocument(
$this->hydrationSettings->getDocumentId($document)
);
} else {
parent::removeDocument($document);
}
@@ -41,8 +41,8 @@ class Index
*/
public function __construct(TransportInterface $transport, array $documents = array(), array $facetFields = array())
{
foreach($documents as $document)
$this->addDocument($document);
foreach($documents as $id=>$document)
$this->addDocument($id, $document);
foreach($facetFields as $field)
$this->addFacetField ($field);
$this->transport = $transport;
@@ -72,15 +72,14 @@ public function removeFacetField($field)
/**
* Adds a new document to the index
* @param array $document Should contain a parameter 'id', that will be used as an id
* @param string|int $id The id of the document
* @param array $document The data for the document
* @return \vierbergenlars\Forage\SearchIndex\Index
*/
public function addDocument($document)
public function addDocument($id, $document)
{
if (!is_array($document))
throw new \BadMethodCallException('Parameter 1 of ' . __METHOD__ . ' should be array, ' . (is_object($document) ? get_class($document) : gettype($document)) . ' given.');
$id = $document['id'];
unset($document['id']);
unset($this->removedDocuments[$id]);
$this->uploadedDocuments[$id] = $document;
return $this;
@@ -7,6 +7,13 @@
*/
class Hit
{
/**
* The id of the document that matched
*
* @var string|int
*/
protected $id;
/**
* The terms that matched in the document
*
@@ -35,6 +42,7 @@ class Hit
*/
public function __construct(array $hit_array)
{
$this->id = $hit_array['id'];
$this->matchedTerms = $hit_array['matchedTerms'];
$this->document = $hit_array['document'];
$this->score = $hit_array['score'];
@@ -68,4 +76,14 @@ public function getDocument()
{
return $this->document;
}
/**
* Gets the id of the document
*
* @return string|int
*/
public function getId()
{
return $this->id;
}
}
@@ -22,8 +22,8 @@ function testHydration()
'title'=>'Lol docu!',
'body'=>'asdf osnfoeoq asofns sfqsf',
'categories'=>array('a','0xA', '0xFFF', 'sfefqs'),
'id'=>5
),
'id'=>5,
'score'=>1.2684648946
);
@@ -37,6 +37,6 @@ function testHydration()
$this->assertEqual($doc->getTitle(), $document['document']['title']);
$this->assertEqual($doc->getBody(), $document['document']['body']);
$this->assertEqual($doc->getCategories()->getArrayCopy(), $document['document']['categories']);
$this->assertEqual($doc->getIdReal(), $document['document']['id']);
$this->assertEqual($doc->getIdReal(), $document['id']);
}
}
@@ -49,9 +49,9 @@ function testSearchResult()
"funny",
"dog"
],
"body": "dog dog lol!!",
"id": "1"
"body": "dog dog lol!!"
},
"id": "1",
"score": 3.453877639491069
},
{
@@ -65,9 +65,9 @@ function testSearchResult()
"categories": [
"random"
],
"body": "cat /dev/urandom... lol",
"id": "2"
"body": "cat /dev/urandom... lol"
},
"id": "2",
"score": 2.302585092994046
}
]
@@ -10,13 +10,13 @@ public function testSearchIndexUpload()
{
$transport = new TransportMock;
$index = new SearchIndex($transport, array(
array ('id' => 'doc1', 'title' => 'a', 'body' => 'b', 'categories' => array (
'doc1'=> array ('title' => 'a', 'body' => 'b', 'categories' => array (
'a', 'b')),
array ('id' => 'doc2', 'title' => 'b', 'body' => 'c', 'categories' => array (
'doc2'=>array ('title' => 'b', 'body' => 'c', 'categories' => array (
'b', 'c'))
), array('categories', 'body'));
$index->addDocument(array ('id' => 'doc3', 'title' => 'c', 'body' => 'asdfasdf',
$index->addDocument('doc3', array ('title' => 'c', 'body' => 'asdfasdf',
'categories' => array ('c', 'a', 'd')))
->addFacetField('title')
->removeFacetField('body');
@@ -61,17 +61,17 @@ function testSearchIndexAddDelete()
$transport = new TransportMock;
$index = new SearchIndex($transport);
$index->addDocument(array ('id' => 1, 'title' => 'a'))
->addDocument(array ('id' => 2, 'title' => 'b'))
->addDocument(array ('id' => 3, 'title' => 'c'))
->addDocument(array ('id' => 4, 'title' => 'd'));
$index->addDocument(1, array ('title' => 'a'))
->addDocument(2, array ('title' => 'b'))
->addDocument(3, array ('title' => 'c'))
->addDocument(4, array ('title' => 'd'));
$index->removeDocument(2)
->removeDocument(4)
->removeDocument(5)
->removeDocument(6);
$index->addDocument(array ('id' => 5, 'title' => 'e'));
$index->addDocument(5, array ('title' => 'e'));
$status = $index->flush();
@@ -23,8 +23,9 @@ function testHit()
"dog"
),
"body"=> "dog dog lol!!",
"id"=> "1"
),
"id"=> "1",
"score"=> 51651.64
));
@@ -45,7 +46,8 @@ function testHit()
"dog"
),
"body"=> "dog dog lol!!",
"id"=> "1"
));
$this->assertEqual($hit->getId(), 1);
}
}
@@ -93,9 +93,9 @@ function testSearchResult()
"funny",
"dog"
],
"body": "dog dog lol!!",
"id": "1"
"body": "dog dog lol!!"
},
"id": "1",
"score": 3.453877639491069
},
{
@@ -109,9 +109,9 @@ function testSearchResult()
"categories": [
"random"
],
"body": "cat /dev/urandom... lol",
"id": "2"
"body": "cat /dev/urandom... lol"
},
"id": "2",
"score": 2.302585092994046
}
]

0 comments on commit 40228f4

Please sign in to comment.
You can’t perform that action at this time.