Token   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 106
Duplicated Lines 0 %

Coupling/Cohesion

Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 16
cbo 0
dl 0
loc 106
ccs 48
cts 48
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A getType() 0 4 1
A getValue() 0 4 1
C __toString() 0 55 13
1
<?php declare(strict_types=1);
2
3
/**
4
 * @copyright   (c) 2017-present brian ridley
5
 * @author      brian ridley <[email protected]>
6
 * @license     http://opensource.org/licenses/MIT MIT
7
 */
8
9
namespace ptlis\SerializedDataEditor\Parser;
10
11
/**
12
 * A token from a serialized PHP value.
13
 */
14
final class Token
15
{
16
    const NULL = 'null';
17
    const BOOL = 'bool';
18
    const INTEGER = 'integer';
19
    const FLOAT = 'float';
20
    const STRING = 'string';
21
    const REFERENCE = 'reference';
22
    const ARRAY_START = 'array_start';
23
    const OBJECT_DEFAULT_NAME = 'object_default_name';
24
    const OBJECT_CUSTOM_NAME = 'object_custom_name';
25
    const OBJECT_CUSTOM_DATA = 'object_custom_data';
26
    const OBJECT_MEMBER_COUNT = 'object_member_count';
27
    const COMPOUND_END = 'compound_end'; // array or object close
28
29
    const PREFIX_NULL = 'N';
30
    const PREFIX_BOOL = 'b';
31
    const PREFIX_INTEGER = 'i';
32
    const PREFIX_FLOAT = 'd';
33
    const PREFIX_STRING = 's';
34
    const PREFIX_REFERENCE = 'R';
35
    const PREFIX_ARRAY_START = 'a';
36
    const PREFIX_OBJECT_DEFAULT_NAME = 'O';
37
    const PREFIX_OBJECT_CUSTOM_NAME = 'C';
38
    const PREFIX_COMPOUND_END = '}';
39
40
    /** @var string */
41
    private $type;
42
43
    /** @var string|null */
44
    private $value;
45
46 46
    public function __construct(
47
        string $type,
48
        ?string $value = null
49
    ) {
50 46
        $this->type = $type;
51 46
        $this->value = $value;
52 46
    }
53
54 37
    public function getType(): string
55
    {
56 37
        return $this->type;
57
    }
58
59 36
    public function getValue(): ?string
60
    {
61 36
        return $this->value;
62
    }
63
64 9
    public function __toString(): string
65
    {
66 9
        $str = '';
67 9
        switch ($this->type) {
68 9
            case self::NULL:
69 1
                $str = self::PREFIX_NULL . ';';
70 1
                break;
71
72 8
            case self::BOOL:
73 2
                $str = self::PREFIX_BOOL . ':' . $this->value . ';';
74 2
                break;
75
76 7
            case self::INTEGER:
77 3
                $str = self::PREFIX_INTEGER . ':' . $this->value . ';';
78 3
                break;
79
80 6
            case self::FLOAT:
81 1
                $str = self::PREFIX_FLOAT . ':' . $this->value . ';';
82 1
                break;
83
84 5
            case self::STRING:
85 4
                $str = self::PREFIX_STRING . ':' . strlen($this->value) . ':"' . $this->value . '";';
86 4
                break;
87
88 4
            case self::REFERENCE:
89 1
                $str = self::PREFIX_REFERENCE . ':' . $this->value . ';';
90 1
                break;
91
92 4
            case self::ARRAY_START:
93 2
                $str = self::PREFIX_ARRAY_START . ':' . $this->value . ':{';
94 2
                break;
95
96 4
            case self::OBJECT_DEFAULT_NAME:
97 1
                $str = self::PREFIX_OBJECT_DEFAULT_NAME . ':' . strlen($this->value) . ':"' . $this->value . '":';
98 1
                break;
99
100 4
            case self::OBJECT_MEMBER_COUNT:
101 1
                $str = $this->value . ':{';
102 1
                break;
103
104 4
            case self::OBJECT_CUSTOM_NAME:
105 1
                $str = self::PREFIX_OBJECT_CUSTOM_NAME . ':' . strlen($this->value) . ':"' . $this->value . '":';
106 1
                break;
107
108 4
            case self::OBJECT_CUSTOM_DATA:
109 1
                $str = strlen($this->value) . ':{' . $this->value;
110 1
                break;
111
112 4
            case self::COMPOUND_END:
113 4
                $str = '}';
114 4
                break;
115
        }
116
117 9
        return $str;
118
    }
119
}