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

FormulaHydrator   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 1
dl 0
loc 45
ccs 0
cts 22
cp 0
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A hydrate() 0 5 1
A extract() 0 6 1
A createEmptyInstance() 0 6 1
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