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

AbstractSerializerRegistry   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 24
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 2
lcom 1
cbo 0
dl 0
loc 24
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A getFromSerializable() 0 10 2
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