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

AggregateHydrator::canHydrate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
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