1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* PHP Billing Library |
6
|
|
|
* |
7
|
|
|
* @link https://github.com/hiqdev/php-billing |
8
|
|
|
* @package php-billing |
9
|
|
|
* @license BSD-3-Clause |
10
|
|
|
* @copyright Copyright (c) 2017-2018, HiQDev (http://hiqdev.com/) |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace hiqdev\php\billing\charge\modifiers; |
14
|
|
|
|
15
|
|
|
use DateTimeImmutable; |
16
|
|
|
use hiqdev\php\billing\action\ActionInterface; |
17
|
|
|
use hiqdev\php\billing\charge\ChargeInterface; |
18
|
|
|
use hiqdev\php\billing\charge\ChargeModifier; |
19
|
|
|
use hiqdev\php\billing\charge\modifiers\addons\Reason; |
20
|
|
|
use hiqdev\php\billing\charge\modifiers\addons\Since; |
21
|
|
|
use hiqdev\php\billing\charge\modifiers\addons\Till; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Fixed discount. |
25
|
|
|
* |
26
|
|
|
* @author Andrii Vasyliev <[email protected]> |
27
|
|
|
*/ |
28
|
|
|
class Modifier implements ChargeModifier |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* @var AddonInterface[] |
32
|
|
|
*/ |
33
|
|
|
protected $addons; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var string[]|AddonInterface[] |
37
|
|
|
*/ |
38
|
|
|
protected $supportedAddons = []; |
39
|
|
|
|
40
|
15 |
|
public function __construct(array $addons = []) |
41
|
|
|
{ |
42
|
15 |
|
$this->addons = $addons; |
43
|
|
|
|
44
|
15 |
|
$this->supportedAddons = [ |
|
|
|
|
45
|
|
|
'reason' => Reason::class, |
46
|
|
|
'since' => Since::class, |
47
|
|
|
'till' => Till::class |
48
|
|
|
]; |
49
|
15 |
|
} |
50
|
|
|
|
51
|
9 |
|
public function __call($name, $params) |
52
|
|
|
{ |
53
|
9 |
|
if (strncmp($name, 'get', 3) === 0) { |
54
|
8 |
|
return $this->getAddon(strtolower(substr($name, 3))); |
55
|
|
|
} |
56
|
7 |
|
if (isset($this->supportedAddons[$name])) { |
57
|
7 |
|
return $this->addAddon($name, new $this->supportedAddons[$name](...$params)); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
throw new \BadMethodCallException("Method \"$name\" is not supported"); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function modifyCharge(?ChargeInterface $charge, ActionInterface $action): array |
64
|
|
|
{ |
65
|
|
|
throw new \Exception('not finished modifier'); |
66
|
|
|
} |
67
|
|
|
|
68
|
7 |
|
public function addAddon($name, $addon) |
69
|
|
|
{ |
70
|
7 |
|
if (!isset($this->supportedAddons[$name])) { |
71
|
|
|
throw new \Exception("Addon \"$name\" is not supported by this class"); |
72
|
|
|
} |
73
|
7 |
|
if (isset($this->addons[$name])) { |
74
|
|
|
throw new \Exception("Addon \"$name\" is already set"); |
75
|
|
|
} |
76
|
7 |
|
$this->addons[$name] = $addon; |
77
|
|
|
|
78
|
7 |
|
return $this; |
79
|
|
|
} |
80
|
|
|
|
81
|
8 |
|
public function getAddon(string $name): ?AddonInterface |
82
|
|
|
{ |
83
|
8 |
|
return $this->addons[$name] ?? null; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function hasAddon(string $name): bool |
87
|
|
|
{ |
88
|
|
|
return isset($this->addons[$name]); |
89
|
|
|
} |
90
|
|
|
|
91
|
3 |
|
|
92
|
|
|
public function discount() |
93
|
3 |
|
{ |
94
|
|
|
return new Discount($this->addons); |
95
|
|
|
} |
96
|
2 |
|
|
97
|
|
|
public function checkPeriod(DateTimeImmutable $time) |
98
|
2 |
|
{ |
99
|
2 |
|
$since = $this->getSince(); |
|
|
|
|
100
|
|
|
if ($since && $since->getValue() > $time) { |
101
|
|
|
return false; |
102
|
|
|
} |
103
|
2 |
|
|
104
|
2 |
|
$till = $this->getTill(); |
|
|
|
|
105
|
|
|
if ($till && $till->getValue() <= $time) { |
106
|
|
|
return false; |
107
|
|
|
} |
108
|
2 |
|
|
109
|
|
|
return true; |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..