Completed
Push — master ( 6df80b...bb98fa )
by Beñat
02:09
created

Phone::setPhone()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 11
rs 9.4285
cc 2
eloc 6
nc 2
nop 1
1
<?php
2
3
/*
4
 * This file is part of the Shared Kernel library.
5
 *
6
 * Copyright (c) 2016 LIN3S <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace LIN3S\SharedKernel\Domain\Model;
13
14
/**
15
 * @author Beñat Espiña <[email protected]>
16
 */
17
class Phone
18
{
19
    private $phone;
20
21
    public function __construct($phone)
22
    {
23
        $this->phone = $this->setPhone($phone);
24
    }
25
26
    public function phone()
27
    {
28
        return $this->phone;
29
    }
30
31
    public function equals(Phone $phone)
32
    {
33
        return $this->phone === $phone->phone();
34
    }
35
36
    public function __toString()
37
    {
38
        return (string)$this->phone;
39
    }
40
41
    private function setPhone($phone)
42
    {
43
        $phone = str_replace('+34', '', $phone);
44
        $numbers = preg_replace('/\D/', '', $phone);
45
46
        if (!$numbers) {
47
            throw new PhoneFormatInvalidException();
48
        }
49
50
        return $numbers;
51
    }
52
}
53