|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Cerbero\Dto; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* The processor for DTO property values. |
|
7
|
|
|
* |
|
8
|
|
|
*/ |
|
9
|
|
|
class DtoPropertyValueProcessor |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* The property to process the value of. |
|
13
|
|
|
* |
|
14
|
|
|
* @var DtoProperty |
|
15
|
|
|
*/ |
|
16
|
|
|
protected $property; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Instantiate the class. |
|
20
|
|
|
* |
|
21
|
|
|
* @param DtoProperty $property |
|
22
|
|
|
*/ |
|
23
|
282 |
|
public function __construct(DtoProperty $property) |
|
24
|
|
|
{ |
|
25
|
282 |
|
$this->property = $property; |
|
26
|
282 |
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Retrieve the processed property value |
|
30
|
|
|
* |
|
31
|
|
|
* @return mixed |
|
32
|
|
|
*/ |
|
33
|
255 |
|
public function process() |
|
34
|
|
|
{ |
|
35
|
255 |
|
if (is_null($rawValue = $this->property->getRawValue())) { |
|
36
|
30 |
|
return null; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
225 |
|
if ($this->property->getTypes()->expectCollection) { |
|
40
|
6 |
|
return $this->processCollection($rawValue); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
219 |
|
return $this->processValue($rawValue); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Retrieve the processed value as a collection |
|
48
|
|
|
* |
|
49
|
|
|
* @param iterable $collection |
|
50
|
|
|
* @return array |
|
51
|
|
|
*/ |
|
52
|
6 |
|
protected function processCollection(iterable $collection): array |
|
53
|
|
|
{ |
|
54
|
6 |
|
$processed = []; |
|
55
|
|
|
|
|
56
|
6 |
|
foreach ($collection as $value) { |
|
57
|
6 |
|
$processed[] = $this->processValue($value); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
6 |
|
return $processed; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Retrieve the processed value |
|
65
|
|
|
* |
|
66
|
|
|
* @param mixed $value |
|
67
|
|
|
* @return mixed |
|
68
|
|
|
*/ |
|
69
|
225 |
|
protected function processValue($value) |
|
70
|
|
|
{ |
|
71
|
225 |
|
$types = $this->property->getTypes(); |
|
72
|
|
|
|
|
73
|
225 |
|
if ($converter = $types->expectedConverter) { |
|
74
|
6 |
|
return $converter->toDto($value); |
|
75
|
219 |
|
} elseif ($types->expectedDto) { |
|
76
|
57 |
|
return $this->castValueIntoDto($value); |
|
77
|
207 |
|
} elseif (($this->property->getFlags() & CAST_PRIMITIVES) && $type = $types->expectedPrimitive) { |
|
78
|
3 |
|
settype($value, $type); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
207 |
|
return $value; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Retrieve the given value casted into a DTO |
|
86
|
|
|
* |
|
87
|
|
|
* @param mixed $value |
|
88
|
|
|
* @return Dto |
|
89
|
|
|
*/ |
|
90
|
57 |
|
protected function castValueIntoDto($value) |
|
91
|
|
|
{ |
|
92
|
57 |
|
$dto = $this->property->getTypes()->expectedDto; |
|
93
|
|
|
|
|
94
|
57 |
|
return is_a($value, $dto) ? $value : $dto::make($value, $this->property->getFlags()); |
|
|
|
|
|
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.