1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace MewesK\TwigSpreadsheetBundle\Wrapper; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Class AbstractWrapper |
7
|
|
|
* |
8
|
|
|
* @package MewesK\TwigSpreadsheetBundle\Wrapper |
9
|
|
|
*/ |
10
|
|
|
abstract class AbstractWrapper |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* Calls the matching mapping callable for each property. |
14
|
|
|
* |
15
|
|
|
* @param array $properties |
16
|
|
|
* @param array $mappings |
17
|
|
|
* @throws \RuntimeException |
18
|
|
|
*/ |
19
|
|
|
protected function setProperties(array $properties, array $mappings) |
20
|
|
|
{ |
21
|
|
|
foreach ($properties as $key => $value) { |
22
|
|
|
if (is_array($value) && is_array($mappings[$key])) { |
23
|
|
|
if (isset($mappings[$key]['__multi']) && $mappings[$key]['__multi'] === true) { |
24
|
|
|
/** |
25
|
|
|
* @var array $value |
26
|
|
|
*/ |
27
|
|
|
foreach ($value as $_key => $_value) { |
28
|
|
|
$this->setPropertiesByKey($_key, $_value, $mappings[$key]); |
29
|
|
|
} |
30
|
|
|
} else { |
31
|
|
|
$this->setProperties($value, $mappings[$key]); |
32
|
|
|
} |
33
|
|
|
} elseif (is_callable($mappings[$key])) { |
34
|
|
|
$mappings[$key]($value); |
35
|
|
|
} else { |
36
|
|
|
throw new \RuntimeException(sprintf('Invalid mapping with key "%s"', $key)); |
37
|
|
|
} |
38
|
|
|
} |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param string $key |
43
|
|
|
* @param array $properties |
44
|
|
|
* @param array $mappings |
45
|
|
|
* @throws \RuntimeException |
46
|
|
|
*/ |
47
|
|
|
private function setPropertiesByKey(string $key, array $properties, array $mappings) |
48
|
|
|
{ |
49
|
|
|
foreach ($properties as $_key => $value) { |
50
|
|
|
if (isset($mappings[$_key])) { |
51
|
|
|
if (is_array($value)) { |
52
|
|
|
$this->setPropertiesByKey($key, $value, $mappings[$_key]); |
53
|
|
|
} elseif(is_callable($mappings[$_key])) { |
54
|
|
|
$mappings[$_key]($key, $value); |
55
|
|
|
} else { |
56
|
|
|
throw new \RuntimeException(sprintf('Invalid mapping with key "%s"', $_key)); |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|