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