Completed
Push — v2 ( 6101cd...f8a429 )
by Beñat
02:32
created

Serializer::className()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 9
rs 9.6667
cc 2
eloc 5
nc 2
nop 0
1
<?php
2
3
/*
4
 * This file is part of the Stack Exchange Api Client library.
5
 *
6
 * Copyright (c) 2014-2016 Beñat Espiña <[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 BenatEspina\StackExchangeApiClient\Serializer;
13
14
use BenatEspina\StackExchangeApiClient\Model\Model;
15
16
/**
17
 * The serializer abstract class.
18
 *
19
 * @author Beñat Espiña <[email protected]>
20
 */
21
abstract class Serializer
22
{
23
    /**
24
     * Fully qualified model class name.
25
     *
26
     * @var string
27
     */
28
    protected static $class;
29
30
    /**
31
     * Serializes the given data in a correct domain model class.
32
     *
33
     * @param mixed $data The given data
34
     *
35
     * @throws \Exception when the given data is incorrect
36
     *
37
     * @return array|Model
38
     */
39
    public static function serialize($data)
40
    {
41
        if (false === array_key_exists('items', $data)) {
42
            throw new \Exception('Given data is incorrect');
43
        }
44
        $class = static::className();
0 ignored issues
show
Bug introduced by
Since className() is declared private, calling it with static will lead to errors in possible sub-classes. You can either use self, or increase the visibility of className() to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
}

public static function getSomeVariable()
{
    return static::getTemperature();
}

}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass {
      private static function getTemperature() {
        return "-182 °C";
    }
}

print YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
    }

    public static function getSomeVariable()
    {
        return self::getTemperature();
    }
}
Loading history...
45
        if (count($data['items']) > 1) {
46
            $objects = [];
47
            foreach ($data['items'] as $item) {
48
                $objects[] = $class::fromJson($item);
49
            }
50
51
            return $objects;
52
        }
53
54
        return $class::fromJson($data['items'][0]);
55
    }
56
57
    /**
58
     * Gets the fully qualified class name.
59
     *
60
     * @throws \Exception when the class is not a model instance
61
     *
62
     * @return string
63
     */
64
    private static function className()
65
    {
66
        $reflectionClass = new \ReflectionClass(static::$class);
67
        if (false === $reflectionClass->implementsInterface(Model::class)) {
68
            throw new \Exception('Given class name is not a model instance');
69
        }
70
71
        return static::$class;
72
    }
73
}
74