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\plan; |
12
|
|
|
|
13
|
|
|
use hiqdev\php\billing\customer\CustomerInterface; |
14
|
|
|
use hiqdev\php\billing\Exception\CannotReassignException; |
15
|
|
|
use hiqdev\php\billing\price\PriceInterface; |
16
|
|
|
use hiqdev\php\billing\type\TypeInterface; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Tariff Plan. |
20
|
|
|
* |
21
|
|
|
* @author Andrii Vasyliev <[email protected]> |
22
|
|
|
*/ |
23
|
|
|
class Plan implements PlanInterface |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @var int |
27
|
|
|
*/ |
28
|
|
|
protected $id; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var string |
32
|
|
|
*/ |
33
|
|
|
protected $name; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var Plan|null |
37
|
|
|
* XXX not sure to implement |
38
|
|
|
*/ |
39
|
|
|
protected $parent; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var CustomerInterface |
43
|
|
|
*/ |
44
|
|
|
protected $seller; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var PriceInterface[] |
48
|
|
|
*/ |
49
|
|
|
protected $prices = []; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @var ?TypeInterface |
53
|
|
|
*/ |
54
|
|
|
protected $type; |
55
|
14 |
|
|
56
|
|
|
/** |
57
|
|
|
* @param int|string|null $id |
58
|
|
|
* @param string $name |
59
|
|
|
* @param PriceInterface[] $prices |
60
|
|
|
*/ |
61
|
14 |
|
public function __construct( |
62
|
14 |
|
$id, |
63
|
14 |
|
$name, |
64
|
14 |
|
CustomerInterface $seller = null, |
65
|
14 |
|
$prices = [], |
66
|
|
|
TypeInterface $type = null |
67
|
|
|
) { |
68
|
|
|
$this->id = $id; |
|
|
|
|
69
|
|
|
$this->name = $name; |
70
|
|
|
$this->seller = $seller; |
71
|
|
|
$this->prices = $prices; |
72
|
|
|
$this->type = $type; |
73
|
|
|
} |
74
|
|
|
|
75
|
4 |
|
public function getUniqueId() |
76
|
|
|
{ |
77
|
4 |
|
return $this->getId(); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @return int|string |
82
|
|
|
*/ |
83
|
5 |
|
public function getId() |
84
|
|
|
{ |
85
|
5 |
|
return $this->id; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @return string |
90
|
|
|
*/ |
91
|
5 |
|
public function getName(): string |
92
|
|
|
{ |
93
|
5 |
|
return $this->name; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
public function setName(string $name): void |
97
|
|
|
{ |
98
|
|
|
$this->name = $name; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @return CustomerInterface |
103
|
|
|
*/ |
104
|
2 |
|
public function getSeller(): ?CustomerInterface |
105
|
|
|
{ |
106
|
2 |
|
return $this->seller; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
public function getParent(): ?PlanInterface |
110
|
|
|
{ |
111
|
|
|
return $this->parent; |
112
|
4 |
|
} |
113
|
|
|
|
114
|
4 |
|
public function hasPrices(): bool |
115
|
|
|
{ |
116
|
|
|
return $this->prices !== []; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @return PriceInterface[] |
121
|
|
|
*/ |
122
|
|
|
public function getPrices(): array |
123
|
|
|
{ |
124
|
|
|
return $this->prices; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
public function setPrices(array $prices): void |
128
|
|
|
{ |
129
|
|
|
if ($this->hasPrices()) { |
130
|
|
|
throw new CannotReassignException('plan prices'); |
131
|
|
|
} |
132
|
|
|
$this->prices = $prices; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
public function getType(): ?TypeInterface |
136
|
|
|
{ |
137
|
|
|
return $this->type ?? null; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
public function jsonSerialize() |
141
|
|
|
{ |
142
|
|
|
return array_filter(get_object_vars($this)); |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.