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
|
|
|
class Leasing extends Modifier |
29
|
|
|
{ |
30
|
|
|
const TERM = 'term'; |
31
|
|
|
|
32
|
5 |
|
public function lasts($term): self |
33
|
|
|
{ |
34
|
5 |
|
return $this->addAddon(self::TERM, Period::fromString($term)); |
35
|
|
|
} |
36
|
|
|
|
37
|
3 |
|
public function getTerm(): ?Period |
38
|
|
|
{ |
39
|
3 |
|
return $this->getAddon(self::TERM); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function isAbsolute() |
43
|
|
|
{ |
44
|
|
|
return $this->getAddon(self::VALUE)->isAbsolute(); |
|
|
|
|
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function isRelative() |
48
|
|
|
{ |
49
|
|
|
return !$this->isAbsolute(); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function calculateSum(ChargeInterface $charge = null): Money |
53
|
|
|
{ |
54
|
|
|
return $this->getValue($charge)->calculateSum($charge); |
|
|
|
|
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function buildPrice(Money $sum) |
58
|
|
|
{ |
59
|
|
|
$type = $this->getType(); |
60
|
|
|
$target = $this->getTarget(); |
61
|
|
|
$prepaid = Quantity::items(0); |
62
|
|
|
|
63
|
|
|
return new SinglePrice(null, $type, $target, null, $prepaid, $sum); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function getType() |
67
|
|
|
{ |
68
|
|
|
return new Type(Type::ANY, 'discount'); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function getTarget() |
72
|
|
|
{ |
73
|
|
|
return new Target(Target::ANY, Target::ANY); |
74
|
|
|
} |
75
|
|
|
|
76
|
1 |
|
public function till($dummy) |
77
|
|
|
{ |
78
|
1 |
|
throw new \Exception('till can not be defined for leasing'); |
79
|
|
|
} |
80
|
|
|
|
81
|
1 |
|
public function modifyCharge(?ChargeInterface $charge, ActionInterface $action): array |
82
|
|
|
{ |
83
|
1 |
|
if ($charge === null) { |
84
|
|
|
$charge = $this->calculateCharge($charge, $action); |
|
|
|
|
85
|
|
|
} |
86
|
|
|
|
87
|
1 |
|
$month = $action->getTime()->modify('first day of this month midnight'); |
88
|
1 |
|
$num = $this->countPeriodsPassed($month); |
89
|
1 |
|
if ($num >= 1 || !$this->checkPeriod($month)) { |
90
|
|
|
return []; |
91
|
|
|
} |
92
|
|
|
|
93
|
1 |
|
$reason = $this->getReason(); |
94
|
1 |
|
if ($reason) { |
95
|
|
|
$charge->setComment($reason->getValue()); |
96
|
|
|
} |
97
|
|
|
|
98
|
1 |
|
return [$charge]; |
99
|
|
|
} |
100
|
|
|
|
101
|
1 |
|
protected function countPeriodsPassed(DateTimeImmutable $time) |
102
|
|
|
{ |
103
|
1 |
|
$since = $this->getSince(); |
104
|
1 |
|
if ($since === null) { |
105
|
|
|
throw new \Exception('no since given for leasing'); |
106
|
|
|
} |
107
|
|
|
|
108
|
1 |
|
$term = $this->getTerm(); |
109
|
1 |
|
if ($term === null) { |
110
|
|
|
throw new \Exception('no term given for leasing'); |
111
|
|
|
} |
112
|
|
|
|
113
|
1 |
|
return $term->countPeriodsPassed($since->getValue(), $time); |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
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: