Node::getName()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 3
nop 1
dl 0
loc 10
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * This file is part of the koriym/printo package.
4
 *
5
 * @license http://opensource.org/licenses/MIT MIT
6
 */
7
namespace Koriym\Printo;
8
9
final class Node implements NodeInterface
10
{
11
    /**
12
     * @var
13
     */
14
    private $value;
15
16
    /**
17
     * @var array
18
     */
19
    private $meta = [];
20
21
    /**
22
     * @param       $value
23
     * @param array $meta
24
     */
25
    public function __construct($value, array $meta)
26
    {
27
        $this->value = $value;
28
        $this->meta = $meta;
29
    }
30
31
    /**
32
     * @return array
33
     */
34
    public function toArray()
35
    {
36
        $array = $this->meta + ['name' => $this->getName($this->value)];
37
38
        return $array;
39
    }
40
41
    /**
42
     * @param $value
43
     *
44
     * @return string
45
     */
46
    private function getName($value)
47
    {
48
        if (is_object($value)) {
49
            return get_class($value);
50
        }
51
        if (is_array($value)) {
52
            return $this->getArrayName();
53
        }
54
55
        return $this->getScalarName($value);
56
    }
57
58
    /**
59
     * @return string
60
     */
61
    private function getArrayName()
62
    {
63
        $name = 'array';
64
65
        return $name;
66
    }
67
68
    /**
69
     * @param $value
70
     *
71
     * @return string
72
     */
73
    private function getScalarName($value)
74
    {
75
        if (is_bool($value)) {
76
            return $value ? 'true' : 'false';
77
        }
78
        if ($value === null) {
79
            return 'null';
80
        }
81
82
        if (is_string($value)) {
83
            return "'{$value}'";
84
        }
85
        if (is_int($value)) {
86
            return $value;
87
        }
88
        $name = sprintf('(%s) %s', gettype($value), (string) $value);
89
90
        return $name;
91
    }
92
}
93