Email::getType()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Communibase\Entity;
6
7
use Communibase\DataBag;
8
9
/**
10
 * Communibase E-mail
11
 *
12
 * @author Kingsquare ([email protected])
13
 * @copyright Copyright (c) Kingsquare BV (http://www.kingsquare.nl)
14
 */
15
class Email
16
{
17
    /**
18
     * @var DataBag
19
     */
20
    protected $dataBag;
21
22
    /**
23
     * @param array{'_id'?: string, 'type'?: string, 'emailAddress'?: string} $emailAddressData
24
     */
25
    final private function __construct(array $emailAddressData = [])
26
    {
27
        if (empty($emailAddressData['type'])) {
28
            $emailAddressData['type'] = 'private';
29
        }
30
        $this->dataBag = DataBag::create();
31
        $this->dataBag->addEntityData('email', $emailAddressData);
32
    }
33
34
    public static function create(): Email
35
    {
36
        return new static();
37
    }
38
39
    /**
40
     * @param ?array{'_id'?: string, 'type'?: string, 'emailAddress'?: string} $emailAddressData
41
     */
42
    public static function fromEmailAddressData(array $emailAddressData = null): Email
43
    {
44
        if ($emailAddressData === null) {
45
            $emailAddressData = [];
46
        }
47
        return new static($emailAddressData);
48
    }
49
50
    public static function fromEmailAddress(string $emailAddress): Email
51
    {
52
        return static::fromEmailAddressData(
53
            [
54
                'emailAddress' => $emailAddress,
55
            ]
56
        );
57
    }
58
59
    public function getEmailAddress(): string
60
    {
61
        return (string)$this->dataBag->get('email.emailAddress');
62
    }
63
64
    public function setEmailAddress(?string $emailAddress): void
65
    {
66
        $this->dataBag->set('email.emailAddress', $emailAddress);
67
    }
68
69
    public function getType(): string
70
    {
71
        return (string)$this->dataBag->get('email.type');
72
    }
73
74
    public function setType(?string $type): void
75
    {
76
        $this->dataBag->set('email.type', $type);
77
    }
78
79
    public function __clone()
80
    {
81
        $state = $this->getState();
82
        if ($state !== null) {
83
            unset($state['_id']);
84
            $this->dataBag->addEntityData('email', $state);
85
        }
86
    }
87
88
    /**
89
     * @return ?array{'_id'?: string, 'type'?: string, 'emailAddress'?: string}
90
     */
91
    public function getState(): ?array
92
    {
93
        if (empty($this->getEmailAddress())) {
94
            return null;
95
        }
96
        return $this->dataBag->getState('email');
97
    }
98
}
99