1 | <?php |
||
16 | final class Hydrator |
||
17 | { |
||
18 | /** |
||
19 | * Creates a cursor record set object from a MongoDB cursor. |
||
20 | * |
||
21 | * @param EntityMetadata $metadata |
||
22 | * @param Cursor $cursor |
||
23 | * @param Store $store |
||
24 | * @return CursorRecordSet |
||
25 | */ |
||
26 | public function createCursorRecordSet(EntityMetadata $metadata, Cursor $cursor, Store $store) |
||
27 | { |
||
28 | return new CursorRecordSet($metadata, $cursor, $store, $this); |
||
29 | } |
||
30 | |||
31 | /** |
||
32 | * Extracts the model type from a raw MongoDB record. |
||
33 | * |
||
34 | * @param EntityMetadata $metadata |
||
35 | * @param array $record |
||
36 | * @return string |
||
37 | * @throws PersisterException |
||
38 | */ |
||
39 | public function extractType(EntityMetadata $metadata, array $record) |
||
40 | { |
||
41 | if (false === $metadata->isPolymorphic()) { |
||
42 | return $metadata->type; |
||
43 | } |
||
44 | return $this->extractField(Persister::POLYMORPHIC_KEY, $record); |
||
45 | } |
||
46 | |||
47 | /** |
||
48 | * Processes a raw MongoDB record and normalizes it into a standard array. |
||
49 | * |
||
50 | * @param EntityMetadata $metadata |
||
51 | * @param array $record |
||
52 | * @param Store $store |
||
53 | * @return array |
||
54 | */ |
||
55 | public function normalize(EntityMetadata $metadata, array $record, Store $store) |
||
56 | { |
||
57 | // Get the identifier and model type value from the raw record. |
||
58 | list($identifier, $type) = $this->extractIdAndType($metadata, $record); |
||
59 | |||
60 | // Reload the metadata in case a polymorphic type was found. |
||
61 | $metadata = $store->getMetadataForType($type); |
||
62 | |||
63 | // Convert relationships to the proper format. |
||
64 | $record = $this->convertRelationships($metadata, $record); |
||
65 | |||
66 | return [ |
||
67 | 'identifier' => (String) $identifier, |
||
68 | 'type' => $type, |
||
69 | 'properties' => $record, |
||
70 | ]; |
||
71 | } |
||
72 | |||
73 | /** |
||
74 | * Converts the relationships on a raw result to the proper format. |
||
75 | * |
||
76 | * @param EntityMetadata $metadata |
||
77 | * @param array &$data |
||
78 | * @return array |
||
79 | */ |
||
80 | private function convertRelationships(EntityMetadata $metadata, array $data) |
||
99 | |||
100 | /** |
||
101 | * Extracts a root field from a raw MongoDB result. |
||
102 | * |
||
103 | * @param string $key |
||
104 | * @param array $data |
||
105 | * @return mixed |
||
106 | * @throws PersisterException |
||
107 | */ |
||
108 | private function extractField($key, array $data) |
||
115 | |||
116 | /** |
||
117 | * Extracts the identifier and model type from a raw result and removes them from the source data. |
||
118 | * Returns as a tuple. |
||
119 | * |
||
120 | * @param EntityMetadata $metadata |
||
121 | * @param array &$data |
||
122 | * @return array |
||
123 | * @throws PersisterException |
||
124 | */ |
||
125 | private function extractIdAndType(EntityMetadata $metadata, array &$data) |
||
137 | |||
138 | /** |
||
139 | * Extracts a standard relationship array that the store expects from a raw MongoDB reference value. |
||
140 | * |
||
141 | * @param RelationshipMetadata $relMeta |
||
142 | * @param mixed $reference |
||
143 | * @return array |
||
144 | * @throws RuntimeException If the relationship could not be extracted. |
||
145 | */ |
||
146 | private function extractRelationship(RelationshipMetadata $relMeta, $reference) |
||
174 | } |
||
175 |