-
Notifications
You must be signed in to change notification settings - Fork 1
/
Phone.php
90 lines (75 loc) · 1.59 KB
/
Phone.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
<?php
namespace App\Models;
use Grummfy\EloquentExtendedCast\Contracts\StdAbleData;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Contracts\Support\Jsonable;
class Phone implements Arrayable, Jsonable, StdAbleData, \JsonSerializable
{
/**
* @var string
*/
protected $number;
/**
* @var string
*/
protected $prefix;
/**
* @var string
*/
protected $msisdn;
public static function fromStdClass(\stdClass $data)
{
return new static($data->prefix, $data->number, $data->msisdn ?? null);
}
public function __construct(string $prefix, string $number, string $msisdn = null)
{
$this->number = preg_replace('/[^0-9]/', '', $number);
$this->prefix = preg_replace('/[^0-9]/', '', $prefix);
$this->msisdn = $msisdn ?? $this->computeMsisdn();
}
/**
* @return string
*/
public function getNumber(): string
{
return $this->number;
}
/**
* @return string
*/
public function getPrefix(): string
{
return $this->prefix;
}
/**
* @return string
*/
public function getMsisdn(): string
{
return $this->msisdn;
}
public function __toString(): string
{
return '(+' . $this->getPrefix() . ') ' . $this->getNumber();
}
public function jsonSerialize()
{
return $this->toArray();
}
public function toJson($options = 0)
{
return json_encode($this->toArray(), $options);
}
public function toArray(): array
{
return [
'prefix' => $this->getPrefix(),
'number' => $this->getNumber(),
'msisdn' => $this->getMsisdn(),
];
}
protected function computeMsisdn()
{
return ltrim($this->getPrefix(), '0') . ltrim($this->getNumber(), '0');
}
}