Passed
Push — master ( 7defe8...edb967 )
by Vincent
25:00
created

Serializer::serialize()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3.0261

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 7
c 1
b 0
f 0
nc 3
nop 3
dl 0
loc 11
ccs 6
cts 7
cp 0.8571
crap 3.0261
rs 10
1
<?php
2
3
namespace Bdf\Serializer;
4
5
use Bdf\Serializer\Context\DenormalizationContext;
6
use Bdf\Serializer\Context\NormalizationContext;
7
use Bdf\Serializer\Normalizer\NormalizerInterface;
8
use Bdf\Serializer\Normalizer\NormalizerLoaderInterface;
9
use Bdf\Serializer\Type\Type;
10
use Bdf\Serializer\Type\TypeFactory;
11
12
/**
13
 * Serializer
14
 * 
15
 * @author  Seb
16
 */
17
class Serializer implements SerializerInterface, NormalizerInterface, BinarySerializerInterface
18
{
19
    /**
20
     * The loader of normalizers
21
     *
22
     * @var NormalizerLoaderInterface
23
     */
24
    private $loader;
25
26
    /**
27
     * Serializer constructor.
28
     *
29
     * @param NormalizerLoaderInterface $loader
30
     */
31 81
    public function __construct(NormalizerLoaderInterface $loader)
32
    {
33 81
        $this->loader = $loader;
34 81
    }
35
36
    /**
37
     * {@inheritdoc}
38
     */
39 13
    public function serialize($data, $format, array $context = [])
40
    {
41 13
        switch ($format) {
42 13
            case 'json':
43 12
                return $this->toJson($data, $context);
44
45 1
            case 'binary':
46
                return $this->toBinary($data, $context);
47
48
            default:
49 1
                return $this->toArray($data, $context);
50
        }
51
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56 15
    public function toJson($data, array $context = [])
57
    {
58 15
        $context = new NormalizationContext($this, $context);
59
60 15
        return json_encode($this->normalize($data, $context), $context->option('json_options', 0));
0 ignored issues
show
Bug introduced by
It seems like $context->option('json_options', 0) can also be of type null; however, parameter $flags of json_encode() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

60
        return json_encode($this->normalize($data, $context), /** @scrutinizer ignore-type */ $context->option('json_options', 0));
Loading history...
61
    }
62
63
    /**
64
     * {@inheritdoc}
65
     */
66
    public function toBinary($data, array $context = [])
67
    {
68
        return igbinary_serialize($this->normalize($data, new NormalizationContext($this, $context)));
69
    }
70
71
    /**
72
     * {@inheritdoc}
73
     */
74 33
    public function toArray($data, array $context = [])
75
    {
76 33
        return $this->normalize($data, new NormalizationContext($this, $context));
77
    }
78
79
    /**
80
     * {@inheritdoc}
81
     */
82 48
    public function normalize($data, NormalizationContext $context)
83
    {
84 48
        if (null === $data || is_scalar($data)) {
85 42
            return $data;
86
        }
87
88 47
        if (is_array($data)) {
89 6
            $normalized = [];
90
91 6
            foreach ($data as $key => $value) {
92 6
                $normalized[$key] = $this->normalize($value, $context);
93
            }
94
95 6
            return $normalized;
96
        }
97
98 46
        $normalized = $this->loader->getNormalizer($data)->normalize($data, $context);
99
100 44
        if ($context->includeMetaType()) {
101
            return [
102 4
                '@type' => get_class($data),
103 4
                'data'  => $normalized,
104
            ];
105
        }
106
107 40
        return $normalized;
108
    }
109
110
    /**
111
     * {@inheritdoc}
112
     */
113 4
    public function deserialize($data, $type, $format, array $context = [])
114
    {
115 4
        switch ($format) {
116 4
            case 'json':
117 3
                return $this->fromJson($data, $type, $context);
118
119 1
            case 'binary':
120
                return $this->fromBinary($data, $type, $context);
121
122
            default:
123 1
                return $this->fromArray($data, $type, $context);
124
        }
125
    }
126
127
    /**
128
     * {@inheritdoc}
129
     */
130 28
    public function fromArray(array $data, $type, array $context = [])
131
    {
132 28
        return $this->denormalize($data, TypeFactory::createType($type), new DenormalizationContext($this, $context));
133
    }
134
135
    /**
136
     * {@inheritdoc}
137
     */
138 6
    public function fromJson(string $json, $type, array $context = [])
139
    {
140 6
        $context = new DenormalizationContext($this, $context);
141
142 6
        return $this->denormalize(json_decode($json, true, 512, $context->option('json_options', 0)), TypeFactory::createType($type), $context);
0 ignored issues
show
Bug introduced by
It seems like $context->option('json_options', 0) can also be of type null; however, parameter $flags of json_decode() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

142
        return $this->denormalize(json_decode($json, true, 512, /** @scrutinizer ignore-type */ $context->option('json_options', 0)), TypeFactory::createType($type), $context);
Loading history...
143
    }
144
145
    /**
146
     * {@inheritdoc}
147
     */
148
    public function fromBinary(string $data, $type, array $context = [])
149
    {
150
        return $this->denormalize(igbinary_unserialize($data), TypeFactory::createType($type), new DenormalizationContext($this, $context));
151
    }
152
153
    /**
154
     * {@inheritdoc}
155
     */
156 34
    public function denormalize($data, Type $type, DenormalizationContext $context)
157
    {
158 34
        if (!is_scalar($data) && !is_array($data)) {
159 2
            return $data;
160
        }
161
162 34
        $type = $type->hint($data);
163
164 34
        if ($type->isArray()) {
165 10
            $denormalized = [];
166
167 10
            foreach ((array)$data as $key => $value) {
168 10
                $denormalized[$key] = $this->denormalize($value, $type->subType(), $context);
169
            }
170
171 10
            return $denormalized;
172
        }
173
174 34
        if ($type->isBuildin()) {
175 26
            return $type->convert($data);
176
        }
177
178 32
        return $this->loader->getNormalizer($type->name())->denormalize($data, $type, $context);
179
    }
180
181
    /**
182
     * {@inheritdoc}
183
     */
184 1
    public function supports(string $className): bool
185
    {
186 1
        return true;
187
    }
188
189
    /**
190
     * Get the normalizer loader
191
     *
192
     * @return NormalizerLoaderInterface
193
     */
194 7
    public function getLoader(): NormalizerLoaderInterface
195
    {
196 7
        return $this->loader;
197
    }
198
}
199