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\charge; |
12
|
|
|
|
13
|
|
|
use DateTimeImmutable; |
14
|
|
|
use hiqdev\php\billing\bill\Bill; |
15
|
|
|
use hiqdev\php\billing\bill\BillInterface; |
16
|
|
|
use hiqdev\php\units\QuantityInterface; |
17
|
|
|
use Money\Money; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @author Andrii Vasyliev <[email protected]> |
21
|
|
|
* TODO: Split out generalizations to Generalizer class. |
22
|
|
|
*/ |
23
|
|
|
class Aggregator implements AggregatorInterface |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @var BillInterface[] |
27
|
|
|
*/ |
28
|
|
|
protected $bills = []; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var GeneralizerInterface |
32
|
|
|
*/ |
33
|
|
|
protected $generalizer; |
34
|
1 |
|
|
35
|
|
|
public function __construct(GeneralizerInterface $generalizer) |
36
|
1 |
|
{ |
37
|
1 |
|
$this->generalizer = $generalizer; |
38
|
|
|
} |
39
|
1 |
|
|
40
|
|
|
public function aggregateCharges(array $charges) |
41
|
1 |
|
{ |
42
|
1 |
|
$bills = []; |
43
|
1 |
|
foreach ($charges as $charge) { |
44
|
1 |
|
if (is_array($charge)) { |
45
|
1 |
|
$others = $this->aggregateCharges($charge); |
46
|
1 |
|
$bills = $this->aggregateBills($bills, $others); |
47
|
1 |
|
} elseif ($charge instanceof ChargeInterface) { |
48
|
1 |
|
$bill = $this->generalizer->createBill($charge); |
49
|
|
|
$bills = $this->aggregateBills($bills, [$bill]); |
50
|
1 |
|
} else { |
51
|
|
|
throw new \Exception('not a Charge given to Aggregator'); |
52
|
|
|
} |
53
|
|
|
} |
54
|
1 |
|
|
55
|
|
|
return $bills; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Aggregate arrays of bills. |
60
|
|
|
* @param BillInterface[] $bills |
61
|
|
|
* @param BillInterface[] $others |
62
|
|
|
* @return BillInterface[] |
63
|
1 |
|
*/ |
64
|
|
|
public function aggregateBills(array $bills, array $others) |
65
|
1 |
|
{ |
66
|
1 |
|
foreach ($others as $bill) { |
67
|
1 |
|
$uid = $bill->getUniqueString(); |
68
|
1 |
|
if (empty($bills[$uid])) { |
69
|
|
|
$bills[$uid] = $bill; |
70
|
1 |
|
} else { |
71
|
|
|
$bills[$uid] = $this->aggregateBill($bills[$uid], $bill); |
72
|
|
|
} |
73
|
|
|
} |
74
|
1 |
|
|
75
|
|
|
return $bills; |
76
|
|
|
} |
77
|
1 |
|
|
78
|
|
|
public function aggregateBill(BillInterface $first, BillInterface $other) |
79
|
1 |
|
{ |
80
|
1 |
|
return new Bill( |
81
|
1 |
|
null, |
82
|
1 |
|
$first->getType(), |
83
|
1 |
|
$first->getTime(), |
84
|
1 |
|
$this->aggregateSum($first->getSum(), $other->getSum()), |
85
|
1 |
|
$this->aggregateQuantity($first->getQuantity(), $other->getQuantity()), |
86
|
1 |
|
$first->getCustomer(), |
87
|
1 |
|
$first->getTarget(), |
88
|
1 |
|
$first->getPlan(), |
89
|
|
|
array_merge($first->getCharges(), $other->getCharges()) |
90
|
|
|
); |
91
|
|
|
} |
92
|
1 |
|
|
93
|
|
|
public function aggregateSum(Money $first, Money $other) |
94
|
1 |
|
{ |
95
|
|
|
return $first->add($other); |
96
|
|
|
} |
97
|
1 |
|
|
98
|
|
|
public function aggregateQuantity(QuantityInterface $first, QuantityInterface $other) |
99
|
1 |
|
{ |
100
|
|
|
return $first->add($other); |
|
|
|
|
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: