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\sale; |
12
|
|
|
|
13
|
|
|
use DateTimeImmutable; |
14
|
|
|
use hiqdev\php\billing\customer\Customer; |
15
|
|
|
use hiqdev\php\billing\plan\Plan; |
16
|
|
|
use hiqdev\php\billing\plan\PlanInterface; |
17
|
|
|
use hiqdev\php\billing\sale\Sale; |
18
|
|
|
use hiqdev\php\billing\target\Target; |
19
|
|
|
use hiqdev\yii\DataMapper\hydrator\GeneratedHydrator; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Class SaleHydrator. |
23
|
|
|
* |
24
|
|
|
* @author Andrii Vasyliev <[email protected]> |
25
|
|
|
*/ |
26
|
|
|
class SaleHydrator extends GeneratedHydrator |
27
|
|
|
{ |
28
|
|
|
public function hydrate(array $data, $object) |
29
|
|
|
{ |
30
|
|
|
$data['target'] = $this->hydrator->hydrate($data['target'], Target::class); |
|
|
|
|
31
|
|
|
$data['customer'] = $this->hydrator->hydrate($data['customer'], Customer::class); |
32
|
|
|
$data['plan'] = $data['plan'] instanceof PlanInterface |
33
|
|
|
? $data['plan'] |
34
|
|
|
: $this->hydrator->hydrate($data['plan'], Plan::class); |
35
|
|
|
$data['time'] = $this->hydrator->hydrate((array) $data['time'], DateTimeImmutable::class); |
36
|
|
|
|
37
|
|
|
return parent::hydrate($data, $object); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* {@inheritdoc} |
42
|
|
|
* @param object|Sale $object |
43
|
|
|
*/ |
44
|
|
|
public function extract($object) |
45
|
|
|
{ |
46
|
|
|
return array_filter([ |
47
|
|
|
'id' => $object->getId(), |
48
|
|
|
'target' => $this->hydrator->extract($object->getTarget()), |
49
|
|
|
'customer' => $this->hydrator->extract($object->getCustomer()), |
50
|
|
|
'plan' => $this->hydrator->extract($object->getPlan()), |
51
|
|
|
'time' => $object->getTime() ? $this->hydrator->extract($object->getTime()) : null, |
52
|
|
|
], static function ($value): bool { |
53
|
|
|
return $value !== null; |
54
|
|
|
}, ARRAY_FILTER_USE_BOTH); |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|