Completed
Pull Request — master (#143)
by Anton
63:11
created

AbstractSerializerRegistry::getFromSerializable()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
3
/*
4
 * This file is part of JSON-API.
5
 *
6
 * (c) Toby Zerner <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Tobscure\JsonApi;
13
14
use RuntimeException;
15
16
abstract class AbstractSerializerRegistry implements SerializerRegistryInterface
17
{
18
    /**
19
     * @var array
20
     */
21
    protected $serializers = [];
22
23
    /**
24
     * Instantiate serializer from the serializable object.
25
     *
26
     * @param object $serializable
27
     * @return \Tobscure\JsonApi\SerializerInterface
28
     */
29
    public function getFromSerializable($serializable)
30
    {
31
        $class = get_class($serializable);
32
33
        if (!isset($this->serializers[$class])) {
34
            throw new RuntimeException("Serializer with name `{$class}` is not exists");
35
        }
36
37
        return new $this->serializers[$class]();
38
    }
39
}
40