TokenCollection::asString()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 8
rs 9.4286
cc 2
eloc 6
nc 2
nop 0
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 Collection of tokens
14
 *
15
 * @author Jean-François Lépine <https://twitter.com/Halleck45>
16
 */
17
class TokenCollection implements \ArrayAccess, \IteratorAggregate, \Countable {
18
19
    /**
20
     * @var array
21
     */
22
    protected $tokens = array();
23
24
    /**
25
     * Constructor
26
     *
27
     * @param $tokens
28
     */
29
    public function __construct(array $tokens)
30
    {
31
        foreach($tokens as $index => &$token) {
32
            if(!$token instanceof Token) {
33
                $token = new Token($token);
34
            }
35
        }
36
//        $this->tokens = $tokens;return;
37
        $compability = new TokenCollectionCompatibility();
38
        $this->tokens = $compability->decorate($tokens);
39
    }
40
41
    /**
42
     * Push token
43
     *
44
     * @param Token $token
45
     * @return $this
46
     */
47
    public function push(Token $token) {
48
        array_push($this->tokens, $token);
49
        return $this;
50
    }
51
52
    /**
53
     * Extract part of tokens (equivalent of array_slice())
54
     *
55
     * @param $start
56
     * @param $end
57
     * @return TokenCollection
58
     */
59
    public function extract($start, $end) {
60
        $concerned = array_slice($this->asArray(), $start, $end - $start );
61
        return new TokenCollection($concerned);
62
    }
63
64
    /**
65
     * As string representation
66
     *
67
     * @return string
68
     */
69
    public function asString() {
70
        $c = '';
71
        foreach($this->tokens as $token) {
72
            $c .= $token->asString();
73
        }
74
        $c = preg_replace('!(\n\s+)!', "\n", $c);
75
        return trim($c);
76
    }
77
78
    /**
79
     * @inheritdoc
80
     */
81
    public function offsetExists($offset)
82
    {
83
       return isset($this->tokens[$offset]);
84
    }
85
86
    /**
87
     * @inheritdoc
88
     */
89
    public function offsetGet($offset)
90
    {
91
        return $this->tokens[$offset];
92
    }
93
94
    /**
95
     * @inheritdoc
96
     */
97
    public function offsetSet($offset, $value)
98
    {
99
        if(!$value instanceof Token) {
100
            $value = new Token($value);
101
        }
102
        $this->tokens[$offset] = $value;
103
    }
104
105
    /**
106
     * @inheritdoc
107
     */
108
    public function offsetUnset($offset)
109
    {
110
        unset($this->tokens[$offset]);
111
    }
112
113
    /**
114
     * @inheritdoc
115
     */
116
    public function getIterator()
117
    {
118
        return new \ArrayIterator($this->tokens);
119
    }
120
121
    /**
122
     * @inheritdoc
123
     */
124
    public function count() {
125
        return sizeof($this->tokens, COUNT_NORMAL);
126
    }
127
128
    /**
129
     * As array representation
130
     *
131
     * @return array
132
     */
133
    public function asArray() {
134
        return $this->tokens;
135
    }
136
137
    /**
138
     * Replace token with another
139
     *
140
     * @param $index
141
     * @param Token $token
142
     * @return $this
143
     */
144
    public function replace($index, Token $token) {
145
        $tokens = $this->tokens;
146
        $tokens[$index] = $token;
147
        return new TokenCollection($tokens);
148
    }
149
150
    /**
151
     * Remove part of tokens
152
     *
153
     * @param $index
154
     * @param null $end
155
     * @return TokenCollection
156
     */
157
    public function remove($index, $end = null)
158
    {
159
        $tokens = $this->tokens;
160
        if (null === $end) {
161
            $end = $index;
162
        }
163
        for ($i = $index; $i <= $end; $i++) {
164
            unset($tokens[$i]);
165
        }
166
        return new TokenCollection(array_values($tokens));
167
    }
168
169
    /**
170
     * Get token by its index
171
     *
172
     * @param $index
173
     * @return null|Token
174
     */
175
    public function get($index) {
176
        return isset($this->tokens[$index]) ? $this->tokens[$index] : null;
177
    }
178
}
179