Compound::fromNative()   A
last analyzed

Complexity

Conditions 5
Paths 7

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 1
Metric Value
cc 5
eloc 7
c 1
b 1
f 1
nc 7
nop 1
dl 0
loc 13
rs 9.6111
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) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
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