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, HiQDev (http://hiqdev.com/) |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace hiqdev\php\billing; |
12
|
|
|
|
13
|
|
|
use hiqdev\php\units\QuantityInterface; |
14
|
|
|
use Money\Money; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Single Price. |
18
|
|
|
* |
19
|
|
|
* - no charge for quantity less then prepaid |
20
|
|
|
* - same price for any quantity above prepaid |
21
|
|
|
* |
22
|
|
|
* @see PriceInterface |
23
|
|
|
* |
24
|
|
|
* @author Andrii Vasyliev <[email protected]> |
25
|
|
|
*/ |
26
|
|
|
class SinglePrice extends AbstractPrice |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* @var Quantity prepaid quantity also implies Unit |
30
|
|
|
*/ |
31
|
|
|
protected $prepaid; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var Money |
35
|
|
|
*/ |
36
|
|
|
protected $price; |
37
|
|
|
|
38
|
3 |
|
public function __construct( |
39
|
|
|
$id, |
40
|
|
|
TargetInterface $target, |
41
|
|
|
TypeInterface $type, |
42
|
|
|
QuantityInterface $prepaid, |
43
|
|
|
Money $price |
44
|
|
|
) { |
45
|
3 |
|
parent::__construct($id, $target, $type); |
46
|
3 |
|
$this->prepaid = $prepaid; |
|
|
|
|
47
|
3 |
|
$this->price = $price; |
48
|
3 |
|
} |
49
|
|
|
|
50
|
|
|
/* |
|
|
|
|
51
|
|
|
protected $properties = [ |
52
|
|
|
'id' => '', |
53
|
|
|
'target' => [AbstractTarget::class, 'create'], |
54
|
|
|
'type' => [Type::class, 'create'], |
55
|
|
|
'prepaid' => [Quantity::class, 'create'], |
56
|
|
|
'price' => [MoneyFactory::class, 'create'], |
57
|
|
|
]; |
58
|
|
|
*/ |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* {@inheritdoc} |
62
|
|
|
*/ |
63
|
3 |
|
public function calculateUsage(QuantityInterface $quantity) |
64
|
|
|
{ |
65
|
3 |
|
$usage = $quantity->convert($this->prepaid->getUnit())->subtract($this->prepaid); |
66
|
|
|
|
67
|
3 |
|
return $usage->isPositive() ? $usage : null; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* {@inheritdoc} |
72
|
|
|
* Same price for any usage. |
73
|
|
|
*/ |
74
|
1 |
|
public function calculatePrice(QuantityInterface $usage) |
75
|
|
|
{ |
76
|
1 |
|
return $this->price; |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
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..