@@ -14,23 +14,24 @@ class Mapper
1414 *
1515 * @param array $record Record.
1616 * @param Strategy|Mapping|array|mixed $expression Expression.
17- * @param mixed $context Context.
17+ * @param mixed $context Optional. Contextual data.
18+ * @param string|int|null $key Internal. Record key.
1819 *
1920 * @return mixed
2021 *
2122 * @throws InvalidExpressionException An invalid strategy or mapping object was specified.
2223 */
23- public function map (array $ record , $ expression , $ context = null )
24+ public function map (array $ record , $ expression , $ context = null , $ key = null )
2425 {
2526 /* Strategy. */
2627 if ($ expression instanceof Strategy) {
27- return $ this ->mapStrategy ($ record , $ expression , $ context );
28+ return $ this ->mapStrategy ($ record , $ expression , $ context, $ key );
2829 } /* Mapping. */
2930 elseif ($ expression instanceof Mapping) {
30- return $ this ->mapMapping ($ record , $ expression , $ context );
31+ return $ this ->mapMapping ($ record , $ expression , $ context, $ key );
3132 } /* Mapping fragment. */
3233 elseif (is_array ($ expression )) {
33- return $ this ->mapFragment ($ record , $ expression , $ context );
34+ return $ this ->mapFragment ($ record , $ expression , $ context, $ key );
3435 } /* Null or scalar values. */
3536 elseif (null === $ expression || is_scalar ($ expression )) {
3637 return $ expression ;
@@ -42,15 +43,16 @@ public function map(array $record, $expression, $context = null)
4243 /**
4344 * @param array $record Record.
4445 * @param Mapping $mapping Mapping.
45- * @param mixed $context Contextual data.
46+ * @param mixed $context Optional. Contextual data.
47+ * @param string|int|null $key Internal. Record key.
4648 *
4749 * @return array Mapped record.
4850 *
4951 * @throws \Exception
5052 */
51- protected function mapMapping (array $ record , Mapping $ mapping , $ context = null )
53+ protected function mapMapping (array $ record , Mapping $ mapping , $ context = null , $ key = null )
5254 {
53- $ mapped = $ this ->mapFragment ($ record , $ mapping ->toArray (), $ context );
55+ $ mapped = $ this ->mapFragment ($ record , $ mapping ->toArray (), $ context, $ key );
5456
5557 if ($ mapping ->isWrapped ()) {
5658 // Unwrap.
@@ -60,12 +62,23 @@ protected function mapMapping(array $record, Mapping $mapping, $context = null)
6062 return $ mapped ;
6163 }
6264
63- protected function mapFragment (array $ record , array $ fragment , $ context = null )
65+ /**
66+ * @param array $record Record.
67+ * @param array $fragment Mapping.
68+ * @param null $context Optional. Contextual data.
69+ * @param string|int|null $key Internal. Record key.
70+ *
71+ * @return array Mapped record.
72+ *
73+ * @throws \Exception Mapping failed for an unknown reason.
74+ */
75+ protected function mapFragment (array $ record , array $ fragment , $ context = null , $ key = null )
6476 {
6577 if (array_walk (
6678 $ fragment ,
67- function (&$ strategy , $ key , array $ record ) use ($ context ) {
68- $ strategy = $ this ->map ($ record , $ strategy , $ context );
79+ // Mapping fragment keys are not useful because they are hard-coded.
80+ function (&$ expression , $ _ , array $ record ) use ($ context , $ key ) {
81+ $ expression = $ this ->map ($ record , $ expression , $ context , $ key );
6982 },
7083 $ record
7184 )) {
@@ -76,14 +89,17 @@ function (&$strategy, $key, array $record) use ($context) {
7689 }
7790
7891 /**
79- * @param array $record
80- * @param Strategy $strategy
81- * @param mixed $context
92+ * @param array $record Record.
93+ * @param Strategy $strategy Strategy.
94+ * @param mixed $context Optional. Contextual data.
95+ * @param string|int|null $key Internal. Record key.
8296 *
8397 * @return mixed
8498 */
85- protected function mapStrategy (array $ record , Strategy $ strategy , $ context = null )
99+ protected function mapStrategy (array $ record , Strategy $ strategy , $ context = null , $ key = null )
86100 {
101+ $ strategy instanceof KeyAware && $ strategy ->setKey ($ key );
102+
87103 $ this ->injectDependencies ($ strategy );
88104
89105 return $ strategy ($ record , $ context );
0 commit comments