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
|
91 |
|
public function __construct(DtoProperty $property) |
24
|
|
|
{ |
25
|
91 |
|
$this->property = $property; |
26
|
91 |
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Retrieve the processed property value |
30
|
|
|
* |
31
|
|
|
* @return mixed |
32
|
|
|
*/ |
33
|
85 |
|
public function process() |
34
|
|
|
{ |
35
|
85 |
|
if (is_null($rawValue = $this->property->getRawValue())) { |
36
|
11 |
|
return null; |
37
|
|
|
} |
38
|
|
|
|
39
|
76 |
|
if ($this->property->getTypes()->expectCollection) { |
40
|
2 |
|
return $this->processCollection($rawValue); |
41
|
|
|
} |
42
|
|
|
|
43
|
74 |
|
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
|
2 |
|
protected function processCollection(iterable $collection): array |
53
|
|
|
{ |
54
|
2 |
|
$processed = []; |
55
|
|
|
|
56
|
2 |
|
foreach ($collection as $value) { |
57
|
2 |
|
$processed[] = $this->processValue($value); |
58
|
|
|
} |
59
|
|
|
|
60
|
2 |
|
return $processed; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Retrieve the processed value |
65
|
|
|
* |
66
|
|
|
* @param mixed $value |
67
|
|
|
* @return mixed |
68
|
|
|
*/ |
69
|
76 |
|
protected function processValue($value) |
70
|
|
|
{ |
71
|
76 |
|
$types = $this->property->getTypes(); |
72
|
|
|
|
73
|
76 |
|
if ($converter = $types->expectedConverter) { |
74
|
2 |
|
return $converter->toDto($value); |
75
|
74 |
|
} elseif ($types->expectedDto) { |
76
|
19 |
|
return $this->castValueIntoDto($value); |
77
|
70 |
|
} elseif (($this->property->getFlags() & CAST_PRIMITIVES) && $type = $types->expectedPrimitive) { |
78
|
4 |
|
settype($value, $type); |
79
|
|
|
} |
80
|
|
|
|
81
|
70 |
|
return $value; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Retrieve the given value casted into a DTO |
86
|
|
|
* |
87
|
|
|
* @param mixed $value |
88
|
|
|
* @return Dto |
89
|
|
|
*/ |
90
|
19 |
|
protected function castValueIntoDto($value): Dto |
91
|
|
|
{ |
92
|
19 |
|
$dto = $this->property->getTypes()->expectedDto; |
93
|
|
|
|
94
|
19 |
|
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.