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\price; |
12
|
|
|
|
13
|
|
|
use hiqdev\php\billing\plan\PlanInterface; |
14
|
|
|
use hiqdev\php\billing\target\TargetInterface; |
15
|
|
|
use hiqdev\php\billing\type\TypeInterface; |
16
|
|
|
use hiqdev\php\units\Quantity; |
17
|
|
|
use hiqdev\php\units\QuantityInterface; |
18
|
|
|
use Money\Money; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Single Price. |
22
|
|
|
* |
23
|
|
|
* - no charge for quantity less then prepaid |
24
|
|
|
* - same price for any quantity above prepaid |
25
|
|
|
* |
26
|
|
|
* @see PriceInterface |
27
|
|
|
* |
28
|
|
|
* @author Andrii Vasyliev <[email protected]> |
29
|
|
|
*/ |
30
|
|
|
class SinglePrice extends AbstractPrice |
31
|
|
|
{ |
32
|
|
|
/** |
33
|
|
|
* @var Quantity prepaid quantity also implies Unit |
34
|
|
|
*/ |
35
|
|
|
protected $prepaid; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var Money |
39
|
|
|
*/ |
40
|
|
|
protected $price; |
41
|
|
|
|
42
|
3 |
|
public function __construct( |
43
|
|
|
$id, |
44
|
|
|
TypeInterface $type, |
45
|
|
|
TargetInterface $target, |
46
|
|
|
PlanInterface $plan = null, |
47
|
|
|
QuantityInterface $prepaid, |
48
|
|
|
Money $price |
49
|
|
|
) { |
50
|
3 |
|
parent::__construct($id, $type, $target, $plan); |
51
|
3 |
|
$this->prepaid = $prepaid; |
|
|
|
|
52
|
3 |
|
$this->price = $price; |
53
|
3 |
|
} |
54
|
|
|
|
55
|
1 |
|
public function getPrepaid() |
56
|
|
|
{ |
57
|
1 |
|
return $this->prepaid; |
58
|
|
|
} |
59
|
|
|
|
60
|
1 |
|
public function getPrice() |
61
|
|
|
{ |
62
|
1 |
|
return $this->price; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* {@inheritdoc} |
67
|
|
|
*/ |
68
|
1 |
|
public function calculateUsage(QuantityInterface $quantity): ?QuantityInterface |
69
|
|
|
{ |
70
|
1 |
|
$usage = $quantity->convert($this->prepaid->getUnit())->subtract($this->prepaid); |
71
|
|
|
|
72
|
1 |
|
return $usage->isPositive() ? $usage : null; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* {@inheritdoc} |
77
|
|
|
* Same price for any usage. |
78
|
|
|
*/ |
79
|
1 |
|
public function calculatePrice(QuantityInterface $usage): ?Money |
80
|
|
|
{ |
81
|
1 |
|
return $this->price; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function jsonSerialize() |
85
|
|
|
{ |
86
|
|
|
return array_merge(parent::jsonSerialize(), [ |
87
|
|
|
'prepaid' => $this->prepaid, |
88
|
|
|
'price' => $this->price |
89
|
|
|
]); |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.
Either this assignment is in error or an instanceof check should be added for that assignment.