TokenRegistry::current()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
/**
3
 * Copyright (c) 2008-2011 Andreas Heigl<[email protected]>
4
 *
5
 * Permission is hereby granted, free of charge, to any person obtaining a copy
6
 * of this software and associated documentation files (the "Software"), to deal
7
 * in the Software without restriction, including without limitation the rights
8
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
 * copies of the Software, and to permit persons to whom the Software is
10
 * furnished to do so, subject to the following conditions:
11
 *
12
 * The above copyright notice and this permission notice shall be included in
13
 * all copies or substantial portions of the Software.
14
 *
15
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
 * THE SOFTWARE.
22
 *
23
 * @category   Hyphenation
24
 * @package    Org\Heigl\Hyphenator
25
 * @subpackage Tokenizer
26
 * @author     Andreas Heigl <[email protected]>
27
 * @copyright  2008-2011 Andreas Heigl<[email protected]>
28
 * @license    http://www.opensource.org/licenses/mit-license.php MIT-License
29
 * @version    2.0.1
30
 * @link       http://github.com/heiglandreas/Hyphenator
31
 * @since      04.11.2011
32
 */
33
34
namespace Org\Heigl\Hyphenator\Tokenizer;
35
36
/**
37
 * This class provides a registry for storing Tokens
38
 *
39
 * @category   Hyphenation
40
 * @package    Org\Heigl\Hyphenator
41
 * @subpackage Tokenizer
42
 * @author     Andreas Heigl <[email protected]>
43
 * @copyright  2008-2011 Andreas Heigl<[email protected]>
44
 * @license    http://www.opensource.org/licenses/mit-license.php MIT-License
45
 * @version    2.0.1
46
 * @link       http://github.com/heiglandreas/Hyphenator
47
 * @since      04.11.2011
48
 */
49
class TokenRegistry implements \Iterator, \Countable
50
{
51
    /**
52
     * Storage for the Tokens.
53
     *
54
     * @var \Org\Heigl\Hyphenator\Tokenizer\Token[] $registry
55
     */
56
    private $registry = array();
57
58
    /**
59
     * Add an item to the registry
60
     *
61
     * @param \Org\Heigl\Hyphenator\Tokenizer\Token $token The token to add
62
     *
63
     * @return \Org\Heigl\Hyphenator\Tokenizer\TokenRegistry
64
     */
65
    public function add(Token $token)
66
    {
67
        $this->registry[] = $token;
68
69
        return $this;
70
    }
71
72
    /**
73
     * Replace a given token with an array of new tokens
74
     *
75
     * This will be used when a tokenizer has to replace a token that has been
76
     * divided into multiple tokens.
77
     *
78
     * @param Token   $oldToken  The token to be replaced
79
     * @param Token[] $newTokens The array of tokens replacing the old one
80
     *
81
     * @return TokenRegistry
82
     */
83
    public function replace(Token $oldToken, array $newTokens)
84
    {
85
        // Get the current key of the element.
86
        $key = array_search($oldToken, $this->registry, true);
87
        if (false === $key) {
88
            return $this;
89
        }
90
        $replacement = array();
91
92
        // Check for any non-token-elements and remove them.
93
        foreach ($newTokens as $token) {
94
            if (! $token instanceof Token) {
95
                continue;
96
            }
97
            $replacement[] = $token;
98
        }
99
100
        // Replace the old element with the newly created array
101
        array_splice($this->registry, $key, 1, $replacement);
102
103
        return $this;
104
    }
105
106
    /**
107
     * Get a Token entry by its key
108
     *
109
     * @param mixed $key The key to get the token for
110
     *
111
     * @return Token|null
112
     */
113
    public function getTokenWithKey($key)
114
    {
115
        if (array_key_exists($key, $this->registry)) {
116
            return $this->registry[$key];
117
        }
118
119
        return null;
120
    }
121
122
    /**
123
     * Implementation of \Iterator
124
     *
125
     * @see \Iterator::rewind()
126
     *
127
     * @return void
128
     */
129
    public function rewind()
130
    {
131
        reset($this->registry);
132
    }
133
134
    /**
135
     * Get the current object
136
     *
137
     * @see \Iterator::current()
138
     *
139
     * @return \Org\Heigl\Hyphenator\Tokenizer\Token
140
     */
141
    public function current()
142
    {
143
        return current($this->registry);
144
    }
145
146
    /**
147
     * Get the current key
148
     *
149
     * @see \Iterator::key()
150
     *
151
     * @return mixed
152
     */
153
    public function key()
154
    {
155
        return key($this->registry);
156
    }
157
158
    /**
159
     * Get the number of items in the registry
160
     *
161
     * @see \Countable::count()
162
     *
163
     * @return int
164
     */
165
    public function count()
166
    {
167
        return count($this->registry);
168
    }
169
170
    /**
171
     * Push the internal pointer forward one step
172
     *
173
     * @see \Iterator::next()
174
     *
175
     * @return void
176
     */
177
    public function next()
178
    {
179
        next($this->registry);
180
    }
181
182
    /**
183
     * Check whether the current pointer is in a valid place
184
     *
185
     * @see \Iterator::valid()
186
     *
187
     * @return boolean
188
     */
189
    public function valid()
190
    {
191
        if (false === current($this->registry)) {
192
            return false;
193
        }
194
195
        return true;
196
    }
197
}
198