1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace Apie\TypeJuggling; |
5
|
|
|
|
6
|
|
|
use Apie\TypeJuggling\Exceptions\InvalidInputException; |
7
|
|
|
use Apie\TypeJuggling\Exceptions\MissingValueException; |
8
|
|
|
|
9
|
|
|
class Compound implements TypeUtilInterface |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @var TypeUtilInterface[] |
13
|
|
|
*/ |
14
|
|
|
private $subTypes; |
15
|
|
|
/** |
16
|
|
|
* @var string |
17
|
|
|
*/ |
18
|
|
|
private $fieldName; |
19
|
|
|
|
20
|
|
|
public function __construct(string $fieldName, TypeUtilInterface... $subTypes) |
21
|
|
|
{ |
22
|
|
|
$this->fieldName = $fieldName; |
23
|
|
|
$this->subTypes = $subTypes; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
public function fromNative($input) |
27
|
|
|
{ |
28
|
|
|
foreach ($this->subTypes as $subType) { |
29
|
|
|
if ($subType->supportsFromNative($input)) { |
30
|
|
|
return $subType->fromNative($input); |
31
|
|
|
} |
32
|
|
|
} |
33
|
|
|
foreach ($this->subTypes as $subType) { |
34
|
|
|
if ($subType->supports($input)) { |
35
|
|
|
return $subType->fromNative($input); |
36
|
|
|
} |
37
|
|
|
} |
38
|
|
|
throw new InvalidInputException($this->fieldName, $this, $input); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function toNative($input) |
42
|
|
|
{ |
43
|
|
|
foreach ($this->subTypes as $subType) { |
44
|
|
|
if ($subType->supportsToNative($input)) { |
45
|
|
|
return $subType->toNative($input); |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
foreach ($this->subTypes as $subType) { |
49
|
|
|
if ($subType->supports($input)) { |
50
|
|
|
return $subType->toNative($input); |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
throw new InvalidInputException($this->fieldName, $this, $input); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function fromMissingValue() |
57
|
|
|
{ |
58
|
|
|
foreach ($this->subTypes as $subType) { |
59
|
|
|
try { |
60
|
|
|
return $subType->fromMissingValue(); |
61
|
|
|
} catch (\Exception $e) { |
|
|
|
|
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
throw new MissingValueException($this->fieldName); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function supports($input): bool |
68
|
|
|
{ |
69
|
|
|
foreach ($this->subTypes as $subType) { |
70
|
|
|
if ($subType->supports($input)) { |
71
|
|
|
return true; |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
return false; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function supportsFromNative($input): bool |
78
|
|
|
{ |
79
|
|
|
foreach ($this->subTypes as $subType) { |
80
|
|
|
if ($subType->supportsFromNative($input)) { |
81
|
|
|
return true; |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
return false; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
public function supportsToNative($input): bool |
88
|
|
|
{ |
89
|
|
|
foreach ($this->subTypes as $subType) { |
90
|
|
|
if ($subType->supportsToNative($input)) { |
91
|
|
|
return true; |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
return false; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public function __toString() |
98
|
|
|
{ |
99
|
|
|
return '(' . join('|', $this->subTypes) . ')'; |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|