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\ChargeModifier; |
17
|
|
|
use hiqdev\php\billing\charge\modifiers\addons\Reason; |
18
|
|
|
use hiqdev\php\billing\charge\modifiers\addons\Since; |
19
|
|
|
use hiqdev\php\billing\charge\modifiers\addons\Till; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Fixed discount. |
23
|
|
|
* |
24
|
|
|
* @author Andrii Vasyliev <[email protected]> |
25
|
|
|
*/ |
26
|
|
|
class Modifier implements ChargeModifier |
27
|
|
|
{ |
28
|
|
|
const REASON = 'reason'; |
29
|
|
|
const SINCE = 'since'; |
30
|
|
|
const TILL = 'till'; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var AddonInterface[] |
34
|
|
|
*/ |
35
|
|
|
protected $addons; |
36
|
|
|
|
37
|
24 |
|
public function __construct(array $addons = []) |
38
|
|
|
{ |
39
|
24 |
|
$this->addons = $addons; |
40
|
24 |
|
} |
41
|
|
|
|
42
|
|
|
public function modifyCharge(?ChargeInterface $charge, ActionInterface $action): array |
43
|
|
|
{ |
44
|
|
|
throw new \Exception('not finished modifier'); |
45
|
|
|
} |
46
|
|
|
|
47
|
3 |
|
public function discount() |
48
|
|
|
{ |
49
|
3 |
|
return new Discount($this->addons); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function leasing() |
53
|
|
|
{ |
54
|
|
|
return new Leasing($this->addons); |
55
|
|
|
} |
56
|
|
|
|
57
|
6 |
|
public function reason($text) |
58
|
|
|
{ |
59
|
6 |
|
return $this->addAddon(self::REASON, new Reason($text)); |
60
|
|
|
} |
61
|
|
|
|
62
|
13 |
|
public function since($time) |
63
|
|
|
{ |
64
|
13 |
|
return $this->addAddon(self::SINCE, new Since($time)); |
65
|
|
|
} |
66
|
|
|
|
67
|
7 |
|
public function till($time) |
68
|
|
|
{ |
69
|
7 |
|
return $this->addAddon(self::TILL, new Till($time)); |
70
|
|
|
} |
71
|
|
|
|
72
|
22 |
|
public function addAddon($name, $addon) |
73
|
|
|
{ |
74
|
22 |
|
if (isset($this->addons[$name])) { |
75
|
|
|
throw new \Exception("'$name' is already set"); |
76
|
|
|
} |
77
|
22 |
|
$this->addons[$name] = $addon; |
78
|
|
|
|
79
|
22 |
|
return $this; |
80
|
|
|
} |
81
|
|
|
|
82
|
18 |
|
public function getAddon($name) |
83
|
|
|
{ |
84
|
18 |
|
return empty($this->addons[$name]) ? null : $this->addons[$name]; |
85
|
|
|
} |
86
|
|
|
|
87
|
10 |
|
public function getReason() |
88
|
|
|
{ |
89
|
10 |
|
return $this->getAddon(self::REASON); |
90
|
|
|
} |
91
|
|
|
|
92
|
11 |
|
public function getSince() |
93
|
|
|
{ |
94
|
11 |
|
return $this->getAddon(self::SINCE); |
95
|
|
|
} |
96
|
|
|
|
97
|
11 |
|
public function getTill() |
98
|
|
|
{ |
99
|
11 |
|
return $this->getAddon(self::TILL); |
100
|
|
|
} |
101
|
|
|
|
102
|
5 |
|
public function checkPeriod(DateTimeImmutable $time) |
103
|
|
|
{ |
104
|
5 |
|
$since = $this->getSince(); |
105
|
5 |
|
if ($since && $since->getValue() > $time) { |
106
|
|
|
return false; |
107
|
|
|
} |
108
|
|
|
|
109
|
5 |
|
$till = $this->getTill(); |
110
|
5 |
|
if ($till && $till->getValue() <= $time) { |
111
|
|
|
return false; |
112
|
|
|
} |
113
|
|
|
|
114
|
5 |
|
return true; |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|