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-2018, HiQDev (http://hiqdev.com/) |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace hiqdev\php\billing\charge\modifiers; |
12
|
|
|
|
13
|
|
|
use DateInterval; |
14
|
|
|
use Money\Money; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Growing discount. |
18
|
|
|
* |
19
|
|
|
* @author Andrii Vasyliev <[email protected]> |
20
|
|
|
*/ |
21
|
|
|
class GrowingDiscount extends FixedDiscount |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var int|Money |
25
|
|
|
*/ |
26
|
|
|
protected $step; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var int|Money |
30
|
|
|
*/ |
31
|
|
|
protected $min; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var int|Money |
35
|
|
|
*/ |
36
|
|
|
protected $max; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var DateInterval |
40
|
|
|
*/ |
41
|
|
|
protected $period; |
42
|
|
|
|
43
|
1 |
|
public function __construct($step, $min = null, array $addons = []) |
44
|
|
|
{ |
45
|
1 |
|
Modifier::__construct($addons); |
46
|
1 |
|
$this->step = static::ensureValidValue($step); |
|
|
|
|
47
|
1 |
|
if ($min) { |
48
|
|
|
$this->min($min); |
49
|
|
|
} |
50
|
1 |
|
} |
51
|
|
|
|
52
|
|
|
public function isAbsolute() |
53
|
|
|
{ |
54
|
|
|
return $this->step instanceof Money; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function isRelative() |
58
|
|
|
{ |
59
|
|
|
return !$this->isAbsolute(); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function min($min) |
63
|
|
|
{ |
64
|
|
|
$this->min = $this->ensureValidLimit($min, 'min'); |
|
|
|
|
65
|
|
|
|
66
|
|
|
return $this; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function max($max) |
70
|
|
|
{ |
71
|
|
|
$this->max = $this->ensureValidLimit($max, 'max'); |
|
|
|
|
72
|
|
|
|
73
|
|
|
return $this; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function ensureValidLimit($limit, $name) |
77
|
|
|
{ |
78
|
|
|
$limit = static::ensureValidValue($limit); |
79
|
|
|
if ($limit instanceof Money) { |
80
|
|
|
if ($this->isRelative()) { |
81
|
|
|
throw new \Exception("$name must be given as percentage because step is percentage"); |
82
|
|
|
} |
83
|
|
|
} elseif ($this->isAbsolute()) { |
84
|
|
|
throw new \Exception("$name must be money because step is money"); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return $limit; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public function everyMonth($num = 1) |
91
|
|
|
{ |
92
|
|
|
if ($this->period !== null) { |
93
|
|
|
throw new \Exception('periodicity is already set'); |
94
|
|
|
} |
95
|
|
|
if (empty($num)) { |
96
|
|
|
$num = 1; |
97
|
|
|
} |
98
|
|
|
if (filter_var($num, FILTER_VALIDATE_INT) === false) { |
99
|
|
|
throw new \Exception('periodicity must be integer number'); |
100
|
|
|
} |
101
|
|
|
$this->period = new DateInterval("P${num}M"); |
102
|
|
|
|
103
|
|
|
return $this; |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
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.