1: <?php
2:
3: namespace vierbergenlars\Forage\ODM\HydrationSettings;
4:
5: use vierbergenlars\Forage\ODM\HydrationSettingsInterface;
6: use Defer\Object as Defer;
7: use vierbergenlars\Forage\ODM\SearchHit;
8:
9: /**
10: * Base class for hydration strategies that use vierbergenlars/defer to hydrate their objects
11: */
12: abstract class DeferHydration implements HydrationSettingsInterface
13: {
14:
15: /**
16: * Gets the properties to be injected in the object
17: * @param string|int $id The id of the document received from the search query
18: * @param array $document The document received from the search query
19: * @return array A map of object properties to values
20: */
21: abstract public function getParameters($id, array $document);
22:
23: /**
24: * Gets the class name of the object that will be hydrated
25: * @param string|int $id The id of the document received from the search query
26: * @param array $document The document received from the search query
27: * @return string A fully qualified class name. (Should implement {@link Defer\Deferrable})
28: */
29: abstract public function getClass($id, array $document);
30:
31: /**
32: * Gets the object from a document
33: * @param array $document
34: * @return object
35: */
36: public function getObject(SearchHit $hit)
37: {
38: $document = $hit->getDocument();
39: array_walk($document, function(&$field) {
40: if(is_array($field))
41: $field = new \ArrayObject($field);
42: });
43: $id = $hit->getId();
44: $data = $this->getParameters($id, $document);
45: $class = $this->getClass($id, $document);
46: return Defer::defer($data, $class);
47: }
48:
49: }
50: