-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSerializerFactory.php
More file actions
65 lines (57 loc) · 1.97 KB
/
Copy pathSerializerFactory.php
File metadata and controls
65 lines (57 loc) · 1.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<?php
/*
* This file is part of the Koded package.
*
* (c) Mihail Binev <mihail@kodeart.com>
*
* Please view the LICENSE distributed with this source code
* for the full copyright and license information.
*/
namespace Koded\Stdlib\Serializer;
use Koded\Exceptions\SerializerException;
use Koded\Stdlib\Serializer;
use function extension_loaded;
use function is_a;
final class SerializerFactory
{
/**
* Factory that creates a new instance of Serializer.
*
* @param string $name The name of the supported serializer.
* Provide a FQCN for custom serializers
* @param $args [optional] Optional arguments for the serializer class
*
* @return Serializer
* @throws SerializerException
*/
public static function new(string $name, ...$args): Serializer
{
switch ($name) {
case Serializer::JSON:
return new JsonSerializer(...$args);
case Serializer::PHP:
return new PhpSerializer;
case Serializer::XML:
empty($args) && $args = [null];
return new XmlSerializer(...$args);
case Serializer::IGBINARY:
// @codeCoverageIgnoreStart
if (false === extension_loaded('igbinary')) {
throw SerializerException::forMissingModule(Serializer::MSGPACK);
}
return new IgbinarySerializer;
// @codeCoverageIgnoreEnd
case Serializer::MSGPACK:
// @codeCoverageIgnoreStart
if (false === extension_loaded('msgpack')) {
throw SerializerException::forMissingModule(Serializer::MSGPACK);
}
return new MsgpackSerializer;
// @codeCoverageIgnoreEnd
}
if (is_a($name, Serializer::class, true)) {
return new $name(...$args);
}
throw SerializerException::forCreateSerializer($name);
}
}