|
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\action; |
|
12
|
|
|
|
|
13
|
|
|
use DateTimeImmutable; |
|
14
|
|
|
use hiqdev\php\billing\action\Action; |
|
15
|
|
|
use hiqdev\php\billing\action\ActionState; |
|
16
|
|
|
use hiqdev\php\billing\customer\Customer; |
|
17
|
|
|
use hiqdev\php\billing\sale\Sale; |
|
18
|
|
|
use hiqdev\php\billing\target\Target; |
|
19
|
|
|
use hiqdev\php\billing\type\Type; |
|
20
|
|
|
use hiqdev\php\units\Quantity; |
|
21
|
|
|
use hiqdev\yii\DataMapper\hydrator\GeneratedHydrator; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Action Hydrator. |
|
25
|
|
|
* |
|
26
|
|
|
* @author Andrii Vasyliev <[email protected]> |
|
27
|
|
|
*/ |
|
28
|
|
|
class ActionHydrator extends GeneratedHydrator |
|
29
|
|
|
{ |
|
30
|
|
|
/** {@inheritdoc} */ |
|
31
|
|
|
public function hydrate(array $data, $object) |
|
32
|
|
|
{ |
|
33
|
|
|
$data['type'] = $this->hydrator->create($data['type'], Type::class); |
|
|
|
|
|
|
34
|
|
|
$data['target'] = $this->hydrator->create($data['target'], Target::class); |
|
35
|
|
|
$data['quantity'] = $this->hydrator->create($data['quantity'], Quantity::class); |
|
36
|
|
|
$data['customer'] = $this->hydrator->create($data['customer'], Customer::class); |
|
37
|
|
|
$data['time'] = $this->hydrator->create($data['time'], DateTimeImmutable::class); |
|
38
|
|
|
if (isset($data['sale'])) { |
|
39
|
|
|
$data['sale'] = $this->hydrator->create($data['sale'], Sale::class); |
|
40
|
|
|
} |
|
41
|
|
|
if (isset($data['parent'])) { |
|
42
|
|
|
$data['parent'] = $this->hydrator->create($data['parent'], Action::class); |
|
43
|
|
|
} |
|
44
|
|
|
if (isset($data['state'])) { |
|
45
|
|
|
$data['state'] = $this->hydrator->create($data['state'], ActionState::class); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
return parent::hydrate($data, $object); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* {@inheritdoc} |
|
53
|
|
|
* @param object|Action $object |
|
54
|
|
|
*/ |
|
55
|
|
|
public function extract($object) |
|
56
|
|
|
{ |
|
57
|
|
|
$result = array_filter([ |
|
58
|
|
|
'id' => $object->getId(), |
|
59
|
|
|
'type' => $this->hydrator->extract($object->getType()), |
|
60
|
|
|
'target' => $this->hydrator->extract($object->getTarget()), |
|
61
|
|
|
'quantity' => $this->hydrator->extract($object->getQuantity()), |
|
62
|
|
|
'customer' => $this->hydrator->extract($object->getCustomer()), |
|
63
|
|
|
'time' => $this->hydrator->extract($object->getTime()), |
|
64
|
|
|
'sale' => $object->getSale() ? $this->hydrator->extract($object->getSale()) : null, |
|
65
|
|
|
'parent' => $object->getParent() ? $this->hydrator->extract($object->getParent()) : null, |
|
66
|
|
|
'state' => $object->getState() ? $this->hydrator->extract($object->getState()) : null, |
|
67
|
|
|
], static function ($value): bool { |
|
68
|
|
|
return $value !== null; |
|
69
|
|
|
}, ARRAY_FILTER_USE_BOTH); |
|
70
|
|
|
|
|
71
|
|
|
return $result; |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|