Passed
Pull Request — master (#14)
by Evgeniy
13:01
created

JsonObjectSerializer::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 5
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Serializer;
6
7
use Symfony\Component\Serializer\Encoder\JsonDecode;
8
use Symfony\Component\Serializer\Encoder\JsonEncode;
9
use Symfony\Component\Serializer\Encoder\JsonEncoder;
10
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
11
use Symfony\Component\Serializer\Serializer;
12
13
use const JSON_UNESCAPED_SLASHES;
14
use const JSON_UNESCAPED_UNICODE;
15
16
/**
17
 * JsonObjectSerializer serializes objects in JSON format and vice versa.
18
 */
19
final class JsonObjectSerializer implements ObjectSerializerInterface
20
{
21
    use ObjectSerializerTrait;
22
23
    /**
24
     * @param int $options The encoding options.
25
     * @see http://www.php.net/manual/en/function.json-encode.php
26
     */
27
    public function __construct(int $options = JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE)
28
    {
29
        $this->serializer = new Serializer([new ObjectNormalizer()], [new JsonEncoder(
30
            new JsonEncode([JsonEncode::OPTIONS => $options]),
31
            new JsonDecode([JsonDecode::ASSOCIATIVE => true]),
32
        )]);
33
    }
34
35
    public function serialize(object $object): string
36
    {
37
        return $this->serializer->serialize($object, JsonEncoder::FORMAT);
38
    }
39
40
    public function serializeMultiple(array $objects): string
41
    {
42
        $this->checkArrayObjects($objects);
43
        return $this->serializer->serialize($objects, JsonEncoder::FORMAT);
44
    }
45
46
    public function unserialize(string $value, string $class): object
47
    {
48
        $value = $this->checkAndDecodeData($value, $class, JsonEncoder::FORMAT);
49
        return $this->denormalizeObject($value, $class);
50
    }
51
52
    public function unserializeMultiple(string $value, string $class): array
53
    {
54
        $value = $this->checkAndDecodeData($value, $class, JsonEncoder::FORMAT);
55
        return $this->denormalizeObjects($value, $class);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->denormalizeObjects($value, $class) returns the type array<mixed,array> which is incompatible with the return type mandated by Yiisoft\Serializer\Objec...::unserializeMultiple() of array<mixed,object>.

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
56
    }
57
}
58