Completed
Push — main ( fffb13...d8afdd )
by Laurent
139:07 queued 124:29
created

PhoneField::__construct()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
cc 3
nc 2
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Domain\Model\Common\VO;
6
7
use Domain\Model\Common\InvalidPhone;
8
9
final class PhoneField
10
{
11
    /**
12
     * @var string
13
     */
14
    private $phone;
15
16
    /**
17
     * PhoneField constructor.
18
     * @param string $phone
19
     */
20
    public function __construct(string $phone)
21
    {
22
        $phoneSanitized = filter_var($phone, FILTER_SANITIZE_NUMBER_INT);
23
        $phoneToCheck = str_replace("-", "", $phoneSanitized);
24
        if (10 > strlen($phoneToCheck) || strlen($phoneToCheck) > 14) {
25
            throw new InvalidPhone();
26
        }
27
        $this->phone = $phoneSanitized;
28
    }
29
30
    public static function fromString(string $phone): self
31
    {
32
        return new self($phone);
33
    }
34
35
    public function getValue(): string
36
    {
37
        return $this->phone;
38
    }
39
}
40