1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* PHP Billing Library |
4
|
|
|
* |
5
|
|
|
* @link https://github.com/hiqdev/php-billing |
6
|
|
|
* @package php-billing |
7
|
|
|
* @license BSD-3-Clause |
8
|
|
|
* @copyright Copyright (c) 2017-2020, HiQDev (http://hiqdev.com/) |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace hiqdev\php\billing\type; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* General Type. |
15
|
|
|
* |
16
|
|
|
* @author Andrii Vasyliev <[email protected]> |
17
|
|
|
*/ |
18
|
|
|
class Type implements TypeInterface |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var int|string |
22
|
|
|
*/ |
23
|
|
|
protected $id; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
protected $name; |
29
|
|
|
|
30
|
47 |
|
public function __construct($id, $name = null) |
31
|
|
|
{ |
32
|
47 |
|
$this->id = $id; |
33
|
47 |
|
$this->name = $name; |
34
|
47 |
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* {@inheritdoc} |
38
|
|
|
*/ |
39
|
7 |
|
public function getId() |
40
|
|
|
{ |
41
|
7 |
|
return $this->id; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* {@inheritdoc} |
46
|
|
|
*/ |
47
|
29 |
|
public function getName(): ?string |
48
|
|
|
{ |
49
|
29 |
|
return $this->name; |
50
|
|
|
} |
51
|
|
|
|
52
|
2 |
|
public function hasName(): bool |
53
|
|
|
{ |
54
|
2 |
|
return !empty($this->name) && $this->name !== self::ANY && $this->name !== self::NONE; |
55
|
|
|
} |
56
|
|
|
|
57
|
2 |
|
public function getUniqueId() |
58
|
|
|
{ |
59
|
2 |
|
return $this->hasName() ? $this->name : $this->id; |
60
|
|
|
} |
61
|
|
|
|
62
|
2 |
|
public function equals(TypeInterface $other): bool |
63
|
|
|
{ |
64
|
2 |
|
return $this->id === $other->getId() && |
65
|
2 |
|
$this->name === $other->getName(); |
66
|
|
|
} |
67
|
|
|
|
68
|
22 |
|
public function matches(TypeInterface $other): bool |
69
|
|
|
{ |
70
|
22 |
|
return $this->id === self::ANY || $other->getId() === self::ANY |
71
|
18 |
|
? $this->checkMatches($this->name, $other->getName()) |
72
|
22 |
|
: $this->checkMatches($this->id, $other->getId()); |
73
|
|
|
} |
74
|
|
|
|
75
|
22 |
|
protected function checkMatches($lhs, $rhs) |
76
|
|
|
{ |
77
|
22 |
|
if ($lhs === self::NONE || $rhs === self::NONE) { |
78
|
1 |
|
return false; |
79
|
|
|
} |
80
|
|
|
|
81
|
22 |
|
return (string) $lhs === (string) $rhs; |
82
|
|
|
} |
83
|
|
|
|
84
|
1 |
|
public function jsonSerialize(): array |
85
|
|
|
{ |
86
|
1 |
|
return array_filter(get_object_vars($this)); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function isDefined(): bool |
90
|
|
|
{ |
91
|
|
|
return $this->id !== null || $this->name !== null; |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|