1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace MewesK\TwigSpreadsheetBundle\Wrapper; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Class BaseWrapper. |
7
|
|
|
*/ |
8
|
|
|
abstract class BaseWrapper |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @var array |
12
|
|
|
*/ |
13
|
|
|
protected $context; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var \Twig_Environment |
17
|
|
|
*/ |
18
|
|
|
protected $environment; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var array |
22
|
|
|
*/ |
23
|
|
|
protected $parameters; |
24
|
|
|
/** |
25
|
|
|
* @var array |
26
|
|
|
*/ |
27
|
|
|
protected $mappings; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* BaseWrapper constructor. |
31
|
|
|
* |
32
|
|
|
* @param array $context |
33
|
|
|
* @param \Twig_Environment $environment |
34
|
|
|
*/ |
35
|
112 |
|
public function __construct(array $context, \Twig_Environment $environment) |
36
|
|
|
{ |
37
|
112 |
|
$this->context = $context; |
38
|
112 |
|
$this->environment = $environment; |
39
|
|
|
|
40
|
112 |
|
$this->parameters = []; |
41
|
112 |
|
$this->mappings = $this->configureMappings(); |
42
|
112 |
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @return array |
46
|
|
|
*/ |
47
|
1 |
|
public function getParameters(): array |
48
|
|
|
{ |
49
|
1 |
|
return $this->parameters; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param array $parameters |
54
|
|
|
*/ |
55
|
1 |
|
public function setParameters(array $parameters) |
56
|
|
|
{ |
57
|
1 |
|
$this->parameters = $parameters; |
58
|
1 |
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @return array |
62
|
|
|
*/ |
63
|
|
|
public function getMappings(): array |
64
|
|
|
{ |
65
|
|
|
return $this->mappings; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param array $mappings |
70
|
|
|
*/ |
71
|
|
|
public function setMappings(array $mappings) |
72
|
|
|
{ |
73
|
|
|
$this->mappings = $mappings; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @return array |
78
|
|
|
*/ |
79
|
112 |
|
protected function configureMappings(): array |
80
|
|
|
{ |
81
|
112 |
|
return []; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Calls the matching mapping callable for each property. |
86
|
|
|
* |
87
|
|
|
* @param array $properties |
88
|
|
|
* @param array|null $mappings |
89
|
|
|
* @param string|null $column |
90
|
|
|
* |
91
|
|
|
* @throws \RuntimeException |
92
|
|
|
*/ |
93
|
112 |
|
protected function setProperties(array $properties, array $mappings = null, string $column = null) |
94
|
|
|
{ |
95
|
112 |
|
if ($mappings === null) { |
96
|
112 |
|
$mappings = $this->mappings; |
97
|
|
|
} |
98
|
|
|
|
99
|
112 |
|
foreach ($properties as $key => $value) { |
100
|
39 |
|
if (!isset($mappings[$key])) { |
101
|
|
|
throw new \RuntimeException(sprintf('Missing mapping for key "%s"', $key)); |
102
|
|
|
} |
103
|
|
|
|
104
|
39 |
|
if (\is_array($value) && \is_array($mappings[$key])) { |
105
|
|
|
// recursion |
106
|
19 |
|
if (isset($mappings[$key]['__multi'])) { |
107
|
|
|
// handle multi target structure (with columns) |
108
|
|
|
/** |
109
|
|
|
* @var array $value |
110
|
|
|
*/ |
111
|
6 |
|
foreach ($value as $_column => $_value) { |
112
|
6 |
|
$this->setProperties($_value, $mappings[$key], $_column); |
113
|
|
|
} |
114
|
|
|
} else { |
115
|
|
|
// handle single target structure |
116
|
19 |
|
$this->setProperties($value, $mappings[$key]); |
117
|
|
|
} |
118
|
39 |
|
} elseif (\is_callable($mappings[$key])) { |
119
|
|
|
// call single and multi target mapping |
120
|
|
|
// if column is set it is used to get object from the callback in __multi |
121
|
39 |
|
$mappings[$key]( |
122
|
39 |
|
$value, |
123
|
39 |
|
$column !== null ? $mappings['__multi']($column) : null |
124
|
|
|
); |
125
|
|
|
} else { |
126
|
39 |
|
throw new \RuntimeException(sprintf('Invalid mapping for key "%s"', $key)); |
127
|
|
|
} |
128
|
|
|
} |
129
|
112 |
|
} |
130
|
|
|
} |
131
|
|
|
|