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