PhoneValue::getPhone()   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
4
namespace talismanfr\psbbank\shared;
5
6
7
use InvalidArgumentException;
8
9
class PhoneValue
10
{
11
    private $phone;
12
13
    /**
14
     * PhoneValue constructor.
15
     * @param string $phone
16
     * @throws InvalidArgumentException
17
     */
18
    public function __construct(string $phone)
19
    {
20
        $this->setPhone($phone);
21
    }
22
23
    /**
24
     * @return mixed
25
     */
26
    public function getPhone()
27
    {
28
        return $this->phone;
29
    }
30
31
    /**
32
     * @param string $phone
33
     * @throws InvalidArgumentException
34
     */
35
    private function setPhone(string $phone): void
36
    {
37
        $phone = preg_replace('/[^0-9]/', '', $phone);
38
        if (strlen($phone) < 10) {
39
            throw new InvalidArgumentException('Phone number too short. Minimum 10 numbers');
40
        } elseif (strlen($phone) == 10) {
41
            $phone = '+7' . $phone;
42
        } elseif (strlen($phone) > 11) {
43
            throw new InvalidArgumentException('Phone number too long. Maximum 11 numbers without symbol `+`');
44
        } else {
45
            $phone = '+' . $phone;
46
        }
47
48
49
        $this->phone = '+7(' . substr($phone, -10, 3) . ')' . substr($phone, -7, 3)
50
            . '-' . substr($phone, -4, 2) . '-' . substr($phone, -2);
51
    }
52
53
    public function __toString()
54
    {
55
        return $this->getPhone();
56
    }
57
58
}