Completed
Push — master ( bfe327...f8a59f )
by Dmitry
05:34
created

PlanHydrator   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 6
c 0
b 0
f 0
lcom 0
cbo 0
dl 0
loc 38
ccs 0
cts 22
cp 0
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A hydrateMultiple() 0 10 2
A hydrate() 0 10 2
A setProperty() 0 13 2
1
<?php
2
3
namespace hiqdev\billing\hiapi\repositories;
4
5
6
class PlanHydrator implements HydratorInterface
7
{
8
    public function hydrateMultiple($prototype, array $data)
9
    {
10
        $results = [];
11
12
        foreach ($data as $key => $item) {
13
            $results[$key] = $this->hydrate(clone $prototype, $item);
14
        }
15
16
        return $results;
17
    }
18
19
    public function hydrate($object, array $data)
20
    {
21
        $reflection = new \ReflectionObject($object);
22
23
        foreach ($data as $property => $value) {
24
            $this->setProperty($reflection, $object, $property, $value);
25
        }
26
27
        return $object;
28
    }
29
30
    protected function setProperty(\ReflectionObject $reflection, $object, $property, $value)
31
    {
32
        $property = $reflection->getProperty($property);
33
34
        if ($property->isPublic()) {
35
            $property->setValue($object, $value);
36
            return;
37
        }
38
39
        $property->setAccessible(true);
40
        $property->setValue($object, $value);
41
        $property->setAccessible(false);
42
    }
43
}
44