Completed
Push — master ( bd8043...d919b4 )
by Francesco
05:20
created

HydratableTrait::toArray()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Audiens\AppnexusClient\entity;
4
5
use GeneratedHydrator\Configuration;
6
use Zend\Hydrator\ObjectProperty;
7
8
/**
9
 * Class HydratableTrait
10
 */
11
trait HydratableTrait
12
{
13
    /**
14
     * @param array $objectArray
15
     *
16
     * @return self
17
     */
18
    public static function fromArray(array $objectArray)
19
    {
20
21
        $object = new self();
22
        self::getHydrator()->hydrate($objectArray, $object);
23
24
        return $object;
25
    }
26
27
    /**
28
     * @return array
29
     */
30
    public function toArray()
31
    {
32
        return self::getHydrator()->extract($this);
33
    }
34
35
    /**
36
     * @return ObjectProperty
37
     */
38
    private static function getHydrator()
39
    {
40
41
        $config = new Configuration(self::class);
42
        $hydratorClass = $config->createFactory()->getHydratorClass();
43
        $hydrator = new $hydratorClass();
44
45
        return $hydrator;
46
47
    }
48
}
49