Completed
Push — master ( c93e0a...f87b82 )
by Mario
09:30 queued 03:59
created

AggregateHydrator::hydrate()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 10
ccs 5
cts 5
cp 1
rs 9.4285
cc 3
eloc 5
nc 3
nop 2
crap 3
1
<?php
2
3
namespace Marek\Toggable\Hydrator;
4
5
/**
6
 * Class AggregateHydrator
7
 * @package Marek\Toggable\Hydrator
8
 */
9
class AggregateHydrator implements HydratorInterface
10
{
11
    /**
12
     * @var HydratorInterface[]
13
     */
14
    private $hydrators;
15
16
    /**
17
     * @param \Marek\Toggable\Hydrator\HydratorInterface $hydrator
18
     */
19 19
    public function add(HydratorInterface $hydrator)
20
    {
21 19
        $this->hydrators[] = $hydrator;
22 19
    }
23
24
    /**
25
     * {@inheritdoc}
26
     */
27 2
    public function extract($object)
28
    {
29 2
        foreach($this->hydrators as $hydrator) {
30 2
            if ($hydrator->canHydrate($object)) {
31 2
                return $hydrator->extract($object);
32
            }
33
        }
34
35 1
        return $object;
36
    }
37
38
    /**
39
     * {@inheritdoc}
40
     */
41 2
    public function hydrate(array $data, $object)
42
    {
43 2
        foreach($this->hydrators as $hydrator) {
44 2
            if ($hydrator->canHydrate($object)) {
45 2
                return $hydrator->hydrate($data, $object);
46
            }
47
        }
48
49 1
        return $data;
50
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55 1
    public function canHydrate($object)
56
    {
57 1
        return true;
58
    }
59
}
60