Completed
Push — master ( 411d94...8fae9a )
by John
02:59
created

EntityHydrator::hydrate()   A

Complexity

Conditions 3
Paths 1

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3.009

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.4285
c 0
b 0
f 0
ccs 9
cts 10
cp 0.9
cc 3
eloc 9
nc 1
nop 2
crap 3.009
1
<?php
2
/**
3
 * This file is part of graze/unicontroller-client.
4
 *
5
 * Copyright (c) 2016 Nature Delivered Ltd. <https://www.graze.com>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * @license https://github.com/graze/unicontroller-client/blob/master/LICENSE.md
11
 * @link https://github.com/graze/unicontroller-client
12
 */
13
namespace Graze\UnicontrollerClient\Entity;
14
15
use Graze\UnicontrollerClient\Entity\Entity\EntityInterface;
16
17
class EntityHydrator
18
{
19
    /**
20
     * @param EntityInterface $entity
21
     * @param array $data
22
     * @return EntityInterface
23
     */
24
    public function hydrate(EntityInterface $entity, array $data)
25
    {
26 20
        $hydrator = function ($data) {
27 20
            foreach ($data as $property => $value) {
28 20
                if (!property_exists($this, $property)) {
29
                    continue;
30
                }
31 20
                $this->$property = $value;
32 20
            }
33 20
        };
34
35 20
        $hydrator = $hydrator->bindTo($entity, $entity);
36 20
        $hydrator($data);
37
38 20
        return $entity;
39
    }
40
}
41