Completed
Push — master ( ce0614...61f862 )
by Andrii
06:01
created

SaleHydrator::hydrate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 0
cts 8
cp 0
rs 9.6666
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 2
crap 2
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\target\Target;
17
use hiqdev\yii\DataMapper\hydrator\GeneratedHydrator;
18
19
/**
20
 * Class SaleHydrator.
21
 *
22
 * @author Andrii Vasyliev <[email protected]>
23
 */
24
class SaleHydrator extends GeneratedHydrator
25
{
26
    public function hydrate(array $data, $object)
27
    {
28
        $data['target']     = $this->hydrator->hydrate($data['target'], Target::class);
29
        $data['customer']   = $this->hydrator->hydrate($data['customer'], Customer::class);
30
        $data['plan']       = $this->hydrator->hydrate($data['plan'], Plan::class);
31
        $data['time']       = new DateTimeImmutable($data['time']);
32
33
        return parent::hydrate($data, $object);
34
    }
35
36
    /**
37
     * {@inheritdoc}
38
     * @param object|Sale $object
39
     */
40
    public function extract($object)
41
    {
42
        $result = array_filter([
43
            'id'            => $object->getId(),
44
            'time'          => $object->time ? $object->time->format(DateTimeImmutable::ATOM) : null,
45
        ]);
46
47
        if ($object->getPlan()) {
48
            $result['plan'] = $this->hydrator->extract($object->getPlan());
49
        }
50
        if ($object->getTarget()) {
51
            $result['target'] = $this->hydrator->extract($object->getTarget());
52
        }
53
        if ($object->getCustomer()) {
54
            $result['customer'] = $this->hydrator->extract($object->getCustomer());
55
        }
56
57
        return $result;
58
    }
59
}
60