1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* API for Billing |
4
|
|
|
* |
5
|
|
|
* @link https://github.com/hiqdev/billing-hiapi |
6
|
|
|
* @package billing-hiapi |
7
|
|
|
* @license BSD-3-Clause |
8
|
|
|
* @copyright Copyright (c) 2017-2018, HiQDev (http://hiqdev.com/) |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace hiqdev\billing\hiapi\bill; |
12
|
|
|
|
13
|
|
|
use DateTimeImmutable; |
14
|
|
|
use hiqdev\billing\hiapi\models\Plan; |
15
|
|
|
use hiqdev\php\billing\customer\Customer; |
16
|
|
|
use hiqdev\php\billing\formula\FormulaInterface; |
17
|
|
|
use hiqdev\php\billing\price\PriceFactoryInterface; |
18
|
|
|
use hiqdev\php\billing\target\Target; |
19
|
|
|
use hiqdev\php\billing\type\Type; |
20
|
|
|
use hiqdev\php\units\Quantity; |
21
|
|
|
use hiqdev\php\units\Unit; |
22
|
|
|
use hiqdev\yii\DataMapper\hydrator\GeneratedHydrator; |
23
|
|
|
use Money\Currency; |
24
|
|
|
use Money\Money; |
25
|
|
|
use yii\helpers\Json; |
26
|
|
|
use Zend\Hydrator\HydratorInterface; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Bill Hydrator. |
30
|
|
|
* |
31
|
|
|
* @author Andrii Vasyliev <[email protected]> |
32
|
|
|
*/ |
33
|
|
|
class BillHydrator extends GeneratedHydrator |
34
|
|
|
{ |
35
|
|
|
/** |
36
|
|
|
* {@inheritdoc} |
37
|
|
|
* @param object|Plan $object |
38
|
|
|
*/ |
39
|
|
View Code Duplication |
public function hydrate(array $row, $object) |
|
|
|
|
40
|
|
|
{ |
41
|
|
|
$row['type'] = $this->hydrator->create($row['type'], Type::class); |
|
|
|
|
42
|
|
|
$row['time'] = $this->hydrator->create($row['time'], DateTimeImmutable::class); |
|
|
|
|
43
|
|
|
$row['sum'] = $this->hydrator->create($row['sum'], Money::class); |
|
|
|
|
44
|
|
|
$row['quantity'] = $this->hydrator->create($row['quantity'], Quantity::class); |
|
|
|
|
45
|
|
|
$row['customer'] = $this->hydrator->create($row['customer'], Customer::class); |
|
|
|
|
46
|
|
|
if (isset($row['target'])) { |
47
|
|
|
$row['target'] = $this->hydrator->create($row['target'], Target::class); |
|
|
|
|
48
|
|
|
} |
49
|
|
|
if (isset($row['plan'])) { |
50
|
|
|
$row['plan'] = $this->hydrator->create($row['plan'], Plan::class); |
|
|
|
|
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
return parent::hydrate($row, $object); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* {@inheritdoc} |
58
|
|
|
* @param object|Plan $object |
59
|
|
|
*/ |
60
|
|
|
public function extract($object) |
61
|
|
|
{ |
62
|
|
|
return array_filter([ |
63
|
|
|
'id' => $object->getId(), |
64
|
|
|
'type' => $this->hydrator->extract($object->getType()), |
65
|
|
|
'time' => $this->hydrator->extract($object->getTime()), |
66
|
|
|
'sum' => $this->hydrator->extract($object->getSum()), |
67
|
|
|
'quantity' => $this->hydrator->extract($object->getQuantity()), |
68
|
|
|
'customer' => $this->hydrator->extract($object->getCustomer()), |
69
|
|
|
'target' => $object->getTarget() ? $this->hydrator->extract($object->getTarget()) : null, |
70
|
|
|
'plan' => $object->getPlan() ? $this->hydrator->extract($object->getPlan()) : null, |
71
|
|
|
'charges' => $this->hydrator->extractMultiple($object->getCharges()), |
|
|
|
|
72
|
|
|
]); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
} |
76
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.