Completed
Pull Request — master (#80)
by John
03:04
created

ArraySerializer   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
wmc 3
c 0
b 0
f 0
lcom 0
cbo 0
dl 0
loc 29
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A serialize() 0 4 1
A deserialize() 0 10 2
1
<?php declare(strict_types = 1);
2
/*
3
 * This file is part of the KleijnWeb\SwaggerBundle package.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
9
namespace KleijnWeb\SwaggerBundle\Serialize\Serializer;
10
11
use KleijnWeb\SwaggerBundle\Serialize\Serializer;
12
13
/**
14
 * Simply utilizes json_encode/json_decode
15
 *
16
 * @author John Kleijn <[email protected]>
17
 */
18
class ArraySerializer implements Serializer
19
{
20
    /**
21
     * @param mixed $data
22
     *
23
     * @return string
24
     */
25
    public function serialize($data): string
26
    {
27
        return json_encode($data);
28
    }
29
30
    /**
31
     * @param mixed       $data
32
     * @param string|null $type
33
     *
34
     * @return mixed
35
     */
36
    public function deserialize($data, string $type = null)
37
    {
38
        $array = json_decode($data, true);
39
40
        if (!is_array($array)) {
41
            throw new \UnexpectedValueException("Expected result to be an array");
42
        }
43
44
        return $array;
45
    }
46
}
47