|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Cerbero\Dto\Manipulators; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* The array converter. |
|
7
|
|
|
* |
|
8
|
|
|
*/ |
|
9
|
|
|
class ArrayConverter |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* The class instance. |
|
13
|
|
|
* |
|
14
|
|
|
* @var self |
|
15
|
|
|
*/ |
|
16
|
|
|
protected static $instance; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* The registered conversions. |
|
20
|
|
|
* |
|
21
|
|
|
* @var array |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $conversions = []; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* The cached value converters. |
|
27
|
|
|
* |
|
28
|
|
|
* @var ValueConverter[] |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $cachedConverters = []; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Instantiate the class |
|
34
|
|
|
* |
|
35
|
|
|
*/ |
|
36
|
15 |
|
protected function __construct() |
|
37
|
|
|
{ |
|
38
|
|
|
// |
|
39
|
15 |
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Retrieve the class instance |
|
43
|
|
|
* |
|
44
|
|
|
* @return self |
|
45
|
|
|
*/ |
|
46
|
84 |
|
public static function instance(): self |
|
47
|
|
|
{ |
|
48
|
84 |
|
return static::$instance = static::$instance ?: new static(); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Set the given value conversions |
|
53
|
|
|
* |
|
54
|
|
|
* @param array $conversions |
|
55
|
|
|
* @return self |
|
56
|
|
|
*/ |
|
57
|
12 |
|
public function setConversions(array $conversions): self |
|
58
|
|
|
{ |
|
59
|
12 |
|
$this->conversions = $conversions; |
|
60
|
|
|
|
|
61
|
12 |
|
return $this; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Retrieve the value conversions |
|
66
|
|
|
* |
|
67
|
|
|
* @return array |
|
68
|
|
|
*/ |
|
69
|
21 |
|
public function getConversions(): array |
|
70
|
|
|
{ |
|
71
|
21 |
|
return $this->conversions; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Convert the given item into an array |
|
76
|
|
|
* |
|
77
|
|
|
* @param mixed $item |
|
78
|
|
|
* @return mixed |
|
79
|
|
|
*/ |
|
80
|
39 |
|
public function convert($item) |
|
81
|
|
|
{ |
|
82
|
39 |
|
if (is_object($item) && $converter = $this->getConverterByInstance($item)) { |
|
83
|
3 |
|
return $converter->fromDto($item); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
39 |
|
if (is_iterable($item)) { |
|
87
|
21 |
|
$result = []; |
|
88
|
|
|
|
|
89
|
21 |
|
foreach ($item as $key => $value) { |
|
90
|
21 |
|
$result[$key] = $this->convert($value); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
21 |
|
return $result; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
39 |
|
return $item; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Retrieve the converter for the given object instance |
|
101
|
|
|
* |
|
102
|
|
|
* @param object $instance |
|
103
|
|
|
* @return ValueConverter|null |
|
104
|
|
|
*/ |
|
105
|
18 |
|
public function getConverterByInstance($instance): ?ValueConverter |
|
106
|
|
|
{ |
|
107
|
18 |
|
$class = get_class($instance); |
|
108
|
|
|
|
|
109
|
18 |
|
if (isset($this->cachedConverters[$class])) { |
|
110
|
3 |
|
return $this->cachedConverters[$class]; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
18 |
|
foreach ($this->getConversions() as $type => $class) { |
|
114
|
18 |
|
if (is_a($instance, $type)) { |
|
115
|
6 |
|
$converter = $this->resolveConverter($class); |
|
116
|
14 |
|
return $this->cachedConverters[$class] = $this->cachedConverters[$type] = $converter; |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
15 |
|
return null; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* Retrieve the instance of the given converter |
|
125
|
|
|
* |
|
126
|
|
|
* @param string $converter |
|
127
|
|
|
* @return ValueConverter |
|
128
|
|
|
*/ |
|
129
|
9 |
|
protected function resolveConverter(string $converter): ValueConverter |
|
130
|
|
|
{ |
|
131
|
9 |
|
return new $converter(); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* Retrieve the converter for the given class |
|
136
|
|
|
* |
|
137
|
|
|
* @param string $class |
|
138
|
|
|
* @return ValueConverter|null |
|
139
|
|
|
*/ |
|
140
|
69 |
|
public function getConverterByClass(string $class): ?ValueConverter |
|
141
|
|
|
{ |
|
142
|
69 |
|
if (isset($this->cachedConverters[$class])) { |
|
143
|
3 |
|
return $this->cachedConverters[$class]; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
69 |
|
if ($converter = $this->conversions[$class] ?? null) { |
|
147
|
3 |
|
return $this->cachedConverters[$class] = $this->resolveConverter($converter); |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
69 |
|
return null; |
|
151
|
|
|
} |
|
152
|
|
|
} |
|
153
|
|
|
|