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