Completed
Push — master ( 53418e...80ac75 )
by Andrii
09:18
created

FormulaHydrator::createEmptyInstance()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 0
cts 5
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
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\formula;
12
13
use hiqdev\php\billing\formula\FormulaEngine;
14
use hiqdev\php\billing\formula\FormulaEngineInterface;
15
use hiqdev\yii\DataMapper\hydrator\GeneratedHydrator;
16
use Zend\Hydrator\HydratorInterface;
17
18
/**
19
 * Formula Hydrator.
20
 *
21
 * @author Andrii Vasyliev <[email protected]>
22
 */
23
class FormulaHydrator extends GeneratedHydrator
24
{
25
    /**
26
     * @var FormulaEngineInterface|FormulaEngine
27
     */
28
    protected $formulaEngine;
29
30
    public function __construct(
31
        HydratorInterface $hydrator,
32
        FormulaEngineInterface $formulaEngine
33
    ) {
34
        parent::__construct($hydrator);
35
        $this->formulaEngine = $formulaEngine;
36
    }
37
38
    public function hydrate(array $data, $object)
39
    {
40
        /// XXX actual data setting in createEmptyInstance
41
        return $object;
42
    }
43
44
    /**
45
     * {@inheritdoc}
46
     * @param object|Money $object
47
     */
48
    public function extract($object)
49
    {
50
        return array_filter([
51
            'text'  => (string)$object,
52
        ]);
53
    }
54
55
    /**
56
     * @param string $className
57
     * @param array $data
58
     * @throws \ReflectionException
59
     * @return object
60
     */
61
    public function createEmptyInstance(string $className, array $data = [])
62
    {
63
        $formula = $data['formula'] ?? reset($data);
64
65
        return $this->formulaEngine->build($formula);
66
    }
67
}
68