Issues (30)

src/Serializer/ChainSerializer.php (2 issues)

Labels
Severity
1
<?php
2
3
namespace MediaMonks\RestApi\Serializer;
4
5
use JMS\Serializer\Serializer;
0 ignored issues
show
The type JMS\Serializer\Serializer was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use JMS\Serializer\SerializationContext;
0 ignored issues
show
The type JMS\Serializer\SerializationContext was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
use MediaMonks\RestApi\Exception\SerializerException;
8
9
class ChainSerializer implements SerializerInterface
10
{
11
    use SerializerTrait;
12
13
    /**
14
     * @var SerializerInterface[]
15
     */
16
    private array $serializers = [];
17
18
    private array $formats = [];
19
20
    /**
21
     * @param SerializerInterface $serializer
22
     */
23
    public function addSerializer(SerializerInterface $serializer)
24
    {
25
        $this->serializers[] = $serializer;
26
        $this->formats = array_merge($this->formats, $serializer->getSupportedFormats());
27 3
    }
28
29 3
    /**
30 3
     * @throws SerializerException
31 3
     */
32
    public function serialize(mixed $data, string $format): string
33
    {
34
        $this->assertHasSerializer();
35
36
        foreach ($this->serializers as $serializer) {
37
            if ($serializer->supportsFormat($format)) {
38
                return $serializer->serialize($data, $format);
39 3
            }
40
        }
41 3
42
        throw new SerializerException(sprintf('No serializer found to support format "%s"', $format));
43 2
    }
44 2
45 1
    /**
46
     * @throws SerializerException
47 2
     */
48
    public function getSupportedFormats(): array
49 1
    {
50
        $this->assertHasSerializer();
51
52
        return $this->formats;
53
    }
54
55 2
    /**
56
     * @throws SerializerException
57 2
     */
58
    public function getDefaultFormat(): string
59 1
    {
60
        $this->assertHasSerializer();
61
62
        return $this->serializers[0]->getDefaultFormat();
63
    }
64
65 2
    /**
66
     * @throws SerializerException
67 2
     */
68
    private function assertHasSerializer()
69 1
    {
70
        if (count($this->serializers) === 0) {
71
            throw new SerializerException('No serializer was configured');
72
        }
73
    }
74
}
75