forked from rvanlaak/SettingsBundle
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSerializerFactory.php
More file actions
45 lines (38 loc) · 1.27 KB
/
Copy pathSerializerFactory.php
File metadata and controls
45 lines (38 loc) · 1.27 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
<?php
/**
* This file is part of the DmishhSettingsBundle package.
*
* (c) 2013 Dmitriy Scherbina <http://dmishh.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Dmishh\SettingsBundle\Serializer;
use Dmishh\SettingsBundle\Exception\UnknownSerializerException;
use Symfony\Component\DependencyInjection\Container;
class SerializerFactory
{
/**
* @param string $name short name of serializer (ex.: php) or full class name
*
* @throws \Dmishh\SettingsBundle\Exception\UnknownSerializerException
*
* @return SerializerInterface
*/
public static function create($name)
{
$serializerClass = 'Dmishh\\SettingsBundle\\Serializer\\' . Container::camelize($name) . 'Serializer';
if (class_exists($serializerClass)) {
return new $serializerClass();
} else {
$serializerClass = $name;
if (class_exists($serializerClass)) {
$serializer = new $serializerClass();
if ($serializer instanceof SerializerInterface) {
return $serializer;
}
}
}
throw new UnknownSerializerException($serializerClass);
}
}