Test Failed
Push — master ( e3b5b4...43c430 )
by Kirill
04:55
created

BaseStruct::render()   A

Complexity

Conditions 6
Paths 6

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
nc 6
nop 1
dl 0
loc 15
rs 9.2222
c 0
b 0
f 0
1
<?php
2
/**
3
 * This file is part of Railt package.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
declare(strict_types=1);
9
10
namespace Railt\SDL\Frontend\ValueObject;
11
12
/**
13
 * Class BaseStruct
14
 */
15
abstract class BaseStruct implements \JsonSerializable
16
{
17
    /**
18
     * @var mixed
19
     */
20
    protected $value;
21
22
    /**
23
     * @return mixed
24
     */
25
    public function jsonSerialize()
26
    {
27
        return $this->value;
28
    }
29
30
    /**
31
     * @return mixed
32
     */
33
    public function getValue()
34
    {
35
        return $this->value;
36
    }
37
38
    /**
39
     * @return string
40
     */
41
    public function __toString(): string
42
    {
43
        return $this->render($this->value);
44
    }
45
46
    /**
47
     * @param mixed $value
48
     * @return string
49
     */
50
    private function render($value): string
51
    {
52
        switch (\gettype($value)) {
53
            case 'boolean':
54
                return $value ? 'true' : 'false';
55
            case 'array':
56
                return json_encode($value);
57
            case 'object':
58
                return \get_class($value) . '#' . \spl_object_hash($value);
59
            case 'NULL':
60
                return 'null';
61
            default:
62
                return (string)$value;
63
        }
64
    }
65
66
    /**
67
     * @return bool
68
     */
69
    public function isScalar(): bool
70
    {
71
        return \is_scalar($this->value);
72
    }
73
}
74