Completed
Push — master ( e6d49b...645be4 )
by Taosikai
11:29
created

Hydrator   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 3
dl 0
loc 41
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A extract() 0 4 1
A hydrate() 0 4 1
A createDefaultContext() 0 4 1
1
<?php
2
3
/*
4
 * This file is part of the slince/shopify-api-php
5
 *
6
 * (c) Slince <[email protected]>
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace Slince\Shopify\Hydrator;
13
14
use JMS\Serializer\SerializationContext;
15
use JMS\Serializer\Serializer;
16
use JMS\Serializer\SerializerBuilder;
17
18
class Hydrator implements HydratorInterface
19
{
20
    /**
21
     * @var Serializer
22
     */
23
    protected $serializer;
24
25
    public function __construct($cacheDir, $metaDirs)
26
    {
27
        $this->serializer = SerializerBuilder::create()
28
            ->setCacheDir($cacheDir)
29
            ->setMetadataDirs($metaDirs)
30
            ->build();
31
    }
32
33
    /**
34
     * {@inheritdoc}
35
     */
36
    public function extract($object)
37
    {
38
        return $this->serializer->toArray($object, $this->createDefaultContext());
39
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44
    public function hydrate($target, array $data)
45
    {
46
        return $this->serializer->fromArray($data, $target);
47
    }
48
49
    /**
50
     * Creates a default serializer context
51
     *
52
     * @return SerializationContext
53
     */
54
    protected function createDefaultContext()
55
    {
56
        return SerializationContext::create()->setSerializeNull(true);
57
    }
58
}