Completed
Push — master ( 5c5634...4e7205 )
by personal
05:16
created

Token::__construct()   B

Complexity

Conditions 5
Paths 6

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 15
rs 8.8571
cc 5
eloc 9
nc 6
nop 1
1
<?php
2
3
/*
4
 * (c) Jean-François Lépine <https://twitter.com/Halleck45>
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
namespace Hal\Component\Token;
11
12
/**
13
 * Representation of Token
14
 *
15
 * @author Jean-François Lépine <https://twitter.com/Halleck45>
16
 */
17
class Token {
18
    /**
19
     * Type of token
20
     *
21
     * @var integer
22
     */
23
    private $type;
24
25
    /**
26
     * Value of token
27
     *
28
     * @var string
29
     */
30
    private $value;
31
32
    /**
33
     * Constructor
34
     * @param string|array $data
35
     */
36
    public function __construct($data)
37
    {
38
        if(!is_array($data)) {
39
            $this->type = T_STRING;
40
            $this->value = $data;
41
        } else {
42
            $this->type = $data[0];
43
            $this->value = isset($data[1]) ? $data[1] : null;
44
        }
45
46
        // reduce multiple spaces to one
47
        if(T_WHITESPACE  === $this->type && preg_match('/^\s*$/', $this->value)) {
48
            $this->value = ' ';
49
        }
50
    }
51
52
    /**
53
     * Get the type of token
54
     *
55
     * @return integer
56
     */
57
    public function getType()
58
    {
59
        return $this->type;
60
    }
61
62
    /**
63
     * Get value of token
64
     *
65
     * @return string
66
     */
67
    public function getValue()
68
    {
69
        return $this->value;
70
    }
71
72
    /**
73
     * String representation
74
     *
75
     * @return integer
76
     */
77
    public function asString() {
78
        return $this->getValue();
79
    }
80
81
}