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