-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathMapper.php
More file actions
125 lines (110 loc) · 3.77 KB
/
Mapper.php
File metadata and controls
125 lines (110 loc) · 3.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<?php
namespace ScriptFUSION\Mapper;
use ScriptFUSION\Mapper\Strategy\Strategy;
/**
* Maps records according to expression value types.
*/
class Mapper
{
/**
* Maps the specified record according to the specified expression type. Optionally, used-defined contextual data
* may be passed to the expression. The record key is for internal use only and represents the current array key.
*
* May be called recursively if the expression embeds more expressions.
*
* @param array $record Record.
* @param Strategy|Mapping|array|mixed $expression Expression.
* @param mixed $context Optional. Contextual data.
* @param string|int|null $key Internal. Record key.
*
* @return mixed
*
* @throws InvalidExpressionException An invalid strategy or mapping object was specified.
*/
public function map(array $record, $expression, $context = null, $key = null)
{
// Null or scalar values.
if (null === $expression || is_scalar($expression)) {
return $expression;
}
// Strategy.
if ($expression instanceof Strategy) {
return $this->mapStrategy($record, $expression, $context, $key);
}
// Mapping.
if ($expression instanceof Mapping) {
return $this->mapMapping($record, $expression, $context, $key);
}
// Mapping fragment.
if (is_array($expression)) {
return $this->mapFragment($record, $expression, $context, $key);
}
throw new InvalidExpressionException('Invalid strategy or mapping: "' . get_class($expression) . '".');
}
/**
* @param array $record Record.
* @param Mapping $mapping Mapping.
* @param mixed $context Optional. Contextual data.
* @param string|int|null $key Internal. Record key.
*
* @return array Mapped record.
*
* @throws \Exception
*/
protected function mapMapping(array $record, Mapping $mapping, $context = null, $key = null)
{
$mapped = $this->mapFragment($record, $mapping->toArray(), $context, $key);
if ($mapping->isWrapped()) {
// Unwrap.
return $mapped[0];
}
return $mapped;
}
/**
* @param array $record Record.
* @param array $fragment Mapping.
* @param null $context Optional. Contextual data.
* @param string|int|null $key Internal. Record key.
*
* @return array Mapped record.
*
* @throws \Exception Mapping failed for an unknown reason.
*/
protected function mapFragment(array $record, array $fragment, $context = null, $key = null)
{
if (array_walk(
$fragment,
// Mapping fragment keys are not useful because they are hard-coded.
function (&$expression, $_, array $record) use ($context, $key) {
$expression = $this->map($record, $expression, $context, $key);
},
$record
)) {
return $fragment;
}
throw new \Exception; // TODO: Determine whether this statement is reachable.
}
/**
* @param array $record Record.
* @param Strategy $strategy Strategy.
* @param mixed $context Optional. Contextual data.
* @param string|int|null $key Internal. Record key.
*
* @return mixed
*/
protected function mapStrategy(array $record, Strategy $strategy, $context = null, $key = null)
{
$strategy instanceof KeyAware && $strategy->setKey($key);
$this->injectDependencies($strategy);
return $strategy($record, $context);
}
/**
* @param object $object
*/
protected function injectDependencies($object)
{
if ($object instanceof MapperAware) {
$object->setMapper($this);
}
}
}