1: <?php
2:
3: namespace vierbergenlars\Forage\ODM\HydrationSettings;
4:
5: /**
6: * A simple hydration strategy that injects all parameters as-is in the object.
7: */
8: class SingleObjectHydration extends DeferHydration
9: {
10: /**
11: * The class name of the object that will be injected
12: * @var string
13: */
14: protected $className;
15:
16: /**
17: * Creates a new simple hydration strategy
18: * @param string $className The class that will be hydrated (should implement {@link vierbergenlars\Forage\ODM\Indexable})
19: * @throws \LogicException
20: */
21: public function __construct($className)
22: {
23: $interfaces = class_implements($className);
24: if (!isset($interfaces['vierbergenlars\Forage\ODM\Indexable']))
25: throw new \LogicException($className . ' should implement interface vierbergenlars\Forage\ODM\Indexable');
26: $this->className = $className;
27: }
28:
29: /**
30: * {@inheritDoc}
31: * @internal
32: */
33: public function getClass($id, array $document)
34: {
35: return $this->className;
36: }
37:
38: /**
39: * {@inheritDoc}
40: * @internal
41: */
42: public function getParameters($id, array $document)
43: {
44: return array_merge($document, array('id'=>$id));
45: }
46:
47: /**
48: * Gets the document array from the object
49: * @param \vierbergenlars\Forage\ODM\Indexable $document
50: */
51: private function getDocArray($document)
52: {
53: if(!is_a($document, $this->className))
54: throw new \LogicException('Document should be of type ' . $this->className . ', got ' . get_class($document));
55: return $document->toDocument();
56: }
57:
58: /**
59: * {@InheritDoc}
60: * @param \vierbergenlars\Forage\ODM\Indexable $document
61: * @internal
62: */
63: public function getDocument($document)
64: {
65: $doc = $this->getDocArray($document);
66: unset($doc['id']);
67: return $doc;
68: }
69:
70: /**
71: * {@InheritDoc}
72: * @param \vierbergenlars\Forage\ODM\Indexable $document
73: * @internal
74: */
75: public function getDocumentId($document)
76: {
77: $doc = $this->getDocArray($document);
78: return $doc['id'];
79: }
80:
81: }
82: