Completed
Push — master ( 15e5fa...9d77d0 )
by Siim
02:05
created

Hydrator   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Test Coverage

Coverage 43.33%

Importance

Changes 0
Metric Value
eloc 30
dl 0
loc 47
ccs 13
cts 30
cp 0.4333
rs 10
c 0
b 0
f 0
wmc 9

1 Method

Rating   Name   Duplication   Size   Complexity  
B hydrate() 0 42 9
1
<?php
2
3
namespace Sf4\Populator;
4
5
use Sf4\Populator\Exception\HydratationException;
6
7
class Hydrator implements HydratorInterface
8
{
9
    /**
10
     * {@inheritDoc}
11
     */
12 3
    public function hydrate($data, $object, HydratorContextInterface $context)
13
    {
14 3
        foreach ($data as $key => $value) {
15 3
            if (null === $property = $context->getProperty($key)) {
16
                continue;
17
            }
18 3
            if ($property->isIgnored()) {
19
                continue;
20
            }
21 3
            $setter = $property->getSetter();
22 3
            $caller = array($object, $setter);
23 3
            if (!method_exists($object, $setter)) {
24 3
                if (!array_key_exists($key, get_object_vars($object))) {
25
                    throw new HydratationException(sprintf(
26
                        "Undefined setter method %s::%s for property '%s'",
27
                        get_class($object),
28
                        $setter,
29
                        $key
30
                    ));
31
                }
32
                $caller = function ($value) use ($object, $key) {
33 3
                    $object->$key = $value;
34 3
                };
35
            }
36 3
            if (null !== $subContext = $property->getContext()) {
37
                if (!is_array($value)) {
38
                    continue;
39
                }
40
                $subClass = $subContext->getClass();
41
                if (!class_exists($subClass)) {
42
                    throw new HydratationException(sprintf(
43
                        "Class %s does not exist for property '%s'",
44
                        $subClass,
45
                        $key
46
                    ));
47
                }
48
                $subObject = new $subClass;
49
                $value = $this->hydrate($value, $subObject, $subContext);
50
            }
51 3
            call_user_func_array($caller, array($value));
52
        }
53 3
        return $object;
54
    }
55
}
56