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 hiqdev\php\billing\action\ActionInterface; |
14
|
|
|
use hiqdev\php\billing\charge\Charge; |
15
|
|
|
use hiqdev\php\billing\charge\ChargeInterface; |
16
|
|
|
use hiqdev\php\billing\price\SinglePrice; |
17
|
|
|
use hiqdev\php\billing\target\Target; |
18
|
|
|
use hiqdev\php\billing\type\Type; |
19
|
|
|
use hiqdev\php\units\Quantity; |
20
|
|
|
use Money\Currency; |
21
|
|
|
use Money\Money; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Fixed discount. |
25
|
|
|
* |
26
|
|
|
* @author Andrii Vasyliev <[email protected]> |
27
|
|
|
*/ |
28
|
|
|
class FixedDiscount extends Modifier |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* @var int|Money |
32
|
|
|
*/ |
33
|
|
|
protected $value; |
34
|
|
|
|
35
|
6 |
|
public function __construct($value, array $addons = []) |
36
|
|
|
{ |
37
|
6 |
|
parent::__construct($addons); |
38
|
6 |
|
$this->value = static::ensureValidValue($value); |
|
|
|
|
39
|
6 |
|
} |
40
|
|
|
|
41
|
3 |
|
public function getValue() |
42
|
|
|
{ |
43
|
3 |
|
return $this->value; |
44
|
|
|
} |
45
|
|
|
|
46
|
5 |
|
public function isAbsolute() |
47
|
|
|
{ |
48
|
5 |
|
return $this->value instanceof Money; |
49
|
|
|
} |
50
|
|
|
|
51
|
3 |
|
public function isRelative() |
52
|
|
|
{ |
53
|
3 |
|
return !$this->isAbsolute(); |
54
|
|
|
} |
55
|
|
|
|
56
|
2 |
|
public function calculateSum(Money $sum) |
57
|
|
|
{ |
58
|
2 |
|
return $this->isAbsolute() ? $this->value : $sum->multiply($this->value*0.01); |
59
|
|
|
} |
60
|
|
|
|
61
|
2 |
|
public function buildPrice(Money $sum) |
62
|
|
|
{ |
63
|
2 |
|
$type = $this->getType(); |
64
|
2 |
|
$target = $this->getTarget(); |
65
|
2 |
|
$prepaid = Quantity::items(0); |
66
|
|
|
|
67
|
2 |
|
return new SinglePrice(null, $type, $target, null, $prepaid, $sum); |
68
|
|
|
} |
69
|
|
|
|
70
|
2 |
|
public function getType() |
71
|
|
|
{ |
72
|
2 |
|
return new Type(Type::ANY, 'discount'); |
73
|
|
|
} |
74
|
|
|
|
75
|
2 |
|
public function getTarget() |
76
|
|
|
{ |
77
|
2 |
|
return new Target(Target::ANY, Target::ANY); |
78
|
|
|
} |
79
|
|
|
|
80
|
2 |
|
public function modifyCharge(?ChargeInterface $charge, ActionInterface $action): array |
81
|
|
|
{ |
82
|
2 |
|
if ($charge === null) { |
83
|
|
|
return []; |
84
|
|
|
} |
85
|
|
|
|
86
|
2 |
|
$month = $action->getTime()->modify('first day of this month midnight'); |
87
|
2 |
|
if (!$this->checkPeriod($month)) { |
88
|
|
|
return [$charge]; |
89
|
|
|
} |
90
|
|
|
|
91
|
2 |
|
$sum = $this->calculateSum($charge->getSum()); |
92
|
2 |
|
$usage = Quantity::items(1); |
93
|
2 |
|
$price = $this->buildPrice($sum); |
|
|
|
|
94
|
2 |
|
$discount = new Charge(null, $action, $price, $usage, $sum); |
95
|
2 |
|
$reason = $this->getReason(); |
96
|
2 |
|
if ($reason) { |
97
|
|
|
$discount->setComment($reason->getValue()); |
98
|
|
|
} |
99
|
|
|
|
100
|
2 |
|
return [$charge, $discount]; |
101
|
|
|
} |
102
|
|
|
|
103
|
7 |
|
public static function ensureValidValue($value) |
104
|
|
|
{ |
105
|
7 |
|
if ($value instanceof Money) { |
106
|
2 |
|
return $value; |
107
|
|
|
} |
108
|
|
|
|
109
|
6 |
|
if (is_numeric($value)) { |
110
|
4 |
|
return (string) $value; |
111
|
|
|
} |
112
|
|
|
|
113
|
3 |
|
if (is_string($value) && preg_match('/^(\d{1,5}(\.\d+)?)%$/', $value, $ms)) { |
114
|
2 |
|
return $ms[1]; |
115
|
|
|
} |
116
|
|
|
|
117
|
1 |
|
if (is_string($value) && preg_match('/^(\d{1,5}(\.\d+)?) ([A-Z]{3})$/', $value, $ms)) { |
118
|
1 |
|
return new Money($ms[1]*100, new Currency($ms[3])); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/// TODO: add special exception |
122
|
|
|
var_dump($value); |
|
|
|
|
123
|
|
|
throw new \Exception("invalid discount value: $value"); |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|
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.