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

Hydrator::createDefaultContext()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
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
}