Alias::fromArray()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 1
1
<?php declare(strict_types = 1);
2
3
namespace Link0\Bunq\Domain;
4
5
use InvalidArgumentException;
6
7
final class Alias
8
{
9
    const TYPE_PHONE_NUMBER = 'PHONE_NUMBER';
10
    const TYPE_EMAIL = 'EMAIL';
11
    const TYPE_IBAN = 'IBAN';
12
13
    /**
14
     * @var string
15
     */
16
    private $type;
17
18
    /**
19
     * @var string
20
     */
21
    private $value;
22
23
    /**
24
     * @var string
25
     */
26
    private $name;
27
28
    /**
29
     * @param string $type
30
     * @param string $value
31
     * @param string $name
32
     */
33
    public function __construct(string $type, string $value, string $name)
34
    {
35
        $this->guardValidType($type);
36
37
        $this->type = $type;
38
        $this->value = $value;
39
        $this->name = $name;
40
    }
41
42
    public static function fromArray(array $alias)
43
    {
44
        return new Alias(
45
            $alias['type'],
46
            $alias['value'],
47
            $alias['name']
48
        );
49
    }
50
51
    public function toArray()
52
    {
53
        return [
54
            'type' => $this->type(),
55
            'value' => $this->value(),
56
            'name' => $this->name(),
57
        ];
58
    }
59
60
    /**
61
     * @param string $type
62
     * @return void
63
     * @throws InvalidArgumentException
64
     */
65
    private function guardValidType(string $type)
66
    {
67
        switch ($type) {
68
            case self::TYPE_PHONE_NUMBER:
69
            case self::TYPE_EMAIL:
70
            case self::TYPE_IBAN:
71
                break;
72
            default:
73
                throw new InvalidArgumentException("Invalid Alias type: " . $type);
74
        }
75
    }
76
77
    /**
78
     * @return string
79
     */
80
    public function type(): string
81
    {
82
        return $this->type;
83
    }
84
85
    /**
86
     * @return string
87
     */
88
    public function value(): string
89
    {
90
        return $this->value;
91
    }
92
93
    /**
94
     * @return string
95
     */
96
    public function name(): string
97
    {
98
        return $this->name;
99
    }
100
}
101