Completed
Push — master ( 104946...6683dc )
by Dmitry
02:26
created

src/charge/ChargeHydrator.php (2 issues)

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\charge;
12
13
use hiqdev\php\billing\action\Action;
14
use hiqdev\php\billing\bill\Bill;
15
use hiqdev\php\billing\charge\Charge;
16
use hiqdev\php\billing\charge\ChargeInterface;
17
use hiqdev\php\billing\charge\ChargeState;
18
use hiqdev\php\billing\price\PriceInterface;
19
use hiqdev\php\billing\target\Target;
20
use hiqdev\php\billing\type\Type;
21
use hiqdev\php\units\Quantity;
22
use hiqdev\yii\DataMapper\hydrator\GeneratedHydrator;
23
use Money\Money;
24
25
/**
26
 * Charge Hydrator.
27
 *
28
 * @author Andrii Vasyliev <[email protected]>
29
 */
30
class ChargeHydrator extends GeneratedHydrator
31
{
32
    /** {@inheritdoc} */
33
    public function hydrate(array $data, $object)
34
    {
35
        $data['type']   = $this->hydrator->create($data['type'], Type::class);
0 ignored issues
show
The method create() does not exist on Zend\Hydrator\HydratorInterface. It seems like you code against a sub-type of Zend\Hydrator\HydratorInterface such as hiqdev\yii\DataMapper\hy...urableAggregateHydrator. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

35
        /** @scrutinizer ignore-call */ 
36
        $data['type']   = $this->hydrator->create($data['type'], Type::class);
Loading history...
36
        $data['target'] = $this->hydrator->create($data['target'], Target::class);
37
        $data['action'] = $this->hydrator->create($data['action'], Action::class);
38
        $data['usage']  = $this->hydrator->create($data['usage'], Quantity::class);
39
        $data['sum']    = $this->hydrator->create($data['sum'], Money::class);
40
        if (isset($data['price'])) {
41
            $data['price'] = $this->hydrator->create($data['price'], PriceInterface::class);
42
        }
43
        if (isset($data['bill'])) {
44
            if (\count($data['bill']) > 1) { // If relation is actually populated
45
                $data['bill'] = $this->hydrator->create($data['bill'], Bill::class);
46
            } else {
47
                unset($data['bill']);
48
            }
49
        }
50
        if (isset($data['state'])) {
51
            $data['state'] = $this->hydrator->create($data['state'], ChargeState::class);
52
        }
53
        if (isset($data['parent'])) {
54
            if (\count($data['parent']) > 1) { // If relation is actually populated
55
                $data['parent'] = $this->hydrate($data['parent'], $this->createEmptyInstance(ChargeInterface::class));
56
            } else {
57
                unset($data['parent']);
58
            }
59
        }
60
61
        return parent::hydrate($data, $object);
62
    }
63
64
    /**
65
     * {@inheritdoc}
66
     * @param object|Charge $object
67
     */
68
    public function extract($object)
69
    {
70
        $result = array_filter([
71
            'id'            => $object->getId(),
72
            'type'          => $this->hydrator->extract($object->getType()),
73
            'target'        => $this->hydrator->extract($object->getTarget()),
74
            'action'        => $this->hydrator->extract($object->getAction()),
75
            'price'         => $this->hydrator->extract($object->getPrice()),
76
            'usage'         => $this->hydrator->extract($object->getUsage()),
77
            'sum'           => $this->hydrator->extract($object->getSum()),
78
            'bill'          => $object->getBill() ? $this->hydrator->extract($object->getBill()) : null,
79
            'state'         => $object->getState() ? $this->hydrator->extract($object->getState()) : null,
80
            'comment'       => $object->getComment(),
81
        ]);
82
83
        return $result;
84
    }
85
86
    /**
87
     * @param string $className
88
     * @param array $data
89
     * @throws \ReflectionException
90
     * @return object
91
     */
92
    public function createEmptyInstance(string $className, array $data = [])
0 ignored issues
show
The parameter $className is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

92
    public function createEmptyInstance(/** @scrutinizer ignore-unused */ string $className, array $data = [])

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
93
    {
94
        return parent::createEmptyInstance(Charge::class, $data);
95
    }
96
}
97