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