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 DateTimeImmutable; |
14
|
|
|
use hiqdev\php\billing\action\ActionInterface; |
15
|
|
|
use hiqdev\php\billing\charge\ChargeInterface; |
16
|
|
|
use hiqdev\php\billing\charge\modifiers\addons\Period; |
17
|
|
|
use hiqdev\php\billing\price\SinglePrice; |
18
|
|
|
use hiqdev\php\billing\target\Target; |
19
|
|
|
use hiqdev\php\billing\type\Type; |
20
|
|
|
use hiqdev\php\units\Quantity; |
21
|
|
|
use Money\Money; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Leasing. |
25
|
|
|
* |
26
|
|
|
* @author Andrii Vasyliev <[email protected]> |
27
|
|
|
*/ |
28
|
6 |
|
class Leasing extends Modifier |
29
|
|
|
{ |
30
|
6 |
|
public function getNext() |
31
|
|
|
{ |
32
|
|
|
return $this; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function isAbsolute() |
36
|
|
|
{ |
37
|
|
|
return $this->getAddon(self::VALUE)->isAbsolute(); |
|
|
|
|
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function isRelative() |
41
|
|
|
{ |
42
|
|
|
return !$this->isAbsolute(); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function calculateSum(ChargeInterface $charge = null): Money |
46
|
|
|
{ |
47
|
|
|
return $this->getValue($charge)->calculateSum($charge); |
|
|
|
|
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function buildPrice(Money $sum) |
51
|
|
|
{ |
52
|
|
|
$type = $this->getType(); |
53
|
|
|
$target = $this->getTarget(); |
54
|
|
|
$prepaid = Quantity::items(0); |
55
|
|
|
|
56
|
|
|
return new SinglePrice(null, $type, $target, null, $prepaid, $sum); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function getType() |
60
|
|
|
{ |
61
|
|
|
return new Type(Type::ANY, 'discount'); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function getTarget() |
65
|
|
|
{ |
66
|
|
|
return new Target(Target::ANY, Target::ANY); |
67
|
1 |
|
} |
68
|
|
|
|
69
|
1 |
|
public function till($dummy) |
70
|
|
|
{ |
71
|
|
|
throw new \Exception('till can not be defined for leasing'); |
72
|
1 |
|
} |
73
|
|
|
|
74
|
1 |
|
public function modifyCharge(?ChargeInterface $charge, ActionInterface $action): array |
75
|
|
|
{ |
76
|
|
|
if ($charge === null) { |
77
|
|
|
$charge = $this->calculateCharge($charge, $action); |
|
|
|
|
78
|
1 |
|
} |
79
|
|
|
|
80
|
1 |
|
$this->ensureIsValid(); |
81
|
1 |
|
|
82
|
|
|
$month = $action->getTime()->modify('first day of this month midnight'); |
83
|
|
|
if (!$this->checkPeriod($month)) { |
84
|
|
|
return []; |
85
|
1 |
|
} |
86
|
1 |
|
|
87
|
|
|
$reason = $this->getReason(); |
88
|
|
|
if ($reason) { |
89
|
|
|
$charge->setComment($reason->getValue()); |
90
|
1 |
|
} |
91
|
|
|
|
92
|
|
|
return [$charge]; |
93
|
1 |
|
} |
94
|
|
|
|
95
|
1 |
|
protected function ensureIsValid() |
96
|
1 |
|
{ |
97
|
|
|
$since = $this->getSince(); |
98
|
|
|
if ($since === null) { |
99
|
|
|
throw new \Exception('no since given for leasing'); |
100
|
1 |
|
} |
101
|
1 |
|
|
102
|
|
|
$term = $this->getTerm(); |
103
|
|
|
if ($term === null) { |
104
|
1 |
|
throw new \Exception('no term given for leasing'); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: