TokenizerRegistry::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
use Countable;
37
use Iterator;
38
39
/**
40
 * This class provides a registry for storing multiple Tokenizers
41
 *
42
 * @category   Hyphenation
43
 * @package    Org\Heigl\Hyphenator
44
 * @subpackage Tokenizer
45
 * @author     Andreas Heigl <[email protected]>
46
 * @copyright  2008-2011 Andreas Heigl<[email protected]>
47
 * @license    http://www.opensource.org/licenses/mit-license.php MIT-License
48
 * @version    2.0.1
49
 * @link       http://github.com/heiglandreas/Hyphenator
50
 * @since      04.11.2011
51
 */
52
class TokenizerRegistry implements Iterator, Countable
53
{
54
    /**
55
     * Storage for the Tokenizers.
56
     *
57
     * @var Tokenizer[] $_registry
58
     */
59
    private $registry = array();
60
61
    /**
62
     * Add an item to the registry
63
     *
64
     * @param Tokenizer $tokenizer The tokeniter to be added
65
     *
66
     * @return TokenizerRegistry
67
     */
68
    public function add(Tokenizer $tokenizer)
69
    {
70
        if (! in_array($tokenizer, $this->registry)) {
71
            $this->registry[] = $tokenizer;
72
        }
73
74
        return $this;
75
    }
76
77
    /**
78
     * Get a dictionary entry by its key
79
     *
80
     * @param mixed $key The key to get the tokenizer for.
81
     *
82
     * @return Tokenizer|null
83
     */
84
    public function getTokenizerWithKey($key)
85
    {
86
        if (array_key_exists($key, $this->registry)) {
87
            return $this->registry[$key];
88
        }
89
90
        return null;
91
    }
92
93
    /**
94
     * Cleanup the registry
95
     *
96
     * @return TokenizerRegistry
97
     */
98
    public function cleanup()
99
    {
100
        $this->registry = array();
101
102
        return $this;
103
    }
104
105
    /**
106
     * Pass the given string through the given tokenizers
107
     *
108
     * @param string|TokenRegistry $string The String to be tokenized
109
     *
110
     * @return TokenRegistry
111
     */
112
    public function tokenize($string)
113
    {
114
        if (! $string instanceof TokenRegistry) {
115
            $wt = new WordToken($string);
116
            $string = new TokenRegistry();
117
            $string->add($wt);
118
        }
119
        foreach ($this as $tokenizer) {
120
            $string = $tokenizer->run($string);
121
        }
122
123
        return $string;
124
    }
125
126
    /**
127
     * Implementation of Iterator
128
     *
129
     * @see Iterator::rewind()
130
     *
131
     * @return void
132
     */
133
    public function rewind()
134
    {
135
        reset($this->registry);
136
    }
137
138
    /**
139
     * Get the current object
140
     *
141
     * @see Iterator::current()
142
     *
143
     * @return Tokenizer
144
     */
145
    public function current()
146
    {
147
        return current($this->registry);
148
    }
149
150
    /**
151
     * Get the current key
152
     *
153
     * @see Iterator::key()
154
     *
155
     * @return mixed
156
     */
157
    public function key()
158
    {
159
        return key($this->registry);
160
    }
161
162
    /**
163
     * Get the number of items in the registry
164
     *
165
     * @see Countable::count()
166
     *
167
     * @return int
168
     */
169
    public function count()
170
    {
171
        return count($this->registry);
172
    }
173
174
    /**
175
     * Push the internal pointer forward one step
176
     *
177
     * @see Iterator::next()
178
     *
179
     * @return void
180
     */
181
    public function next()
182
    {
183
        next($this->registry);
184
    }
185
186
    /**
187
     * Check whether the current pointer is in a valid place
188
     *
189
     * @see Iterator::valid()
190
     *
191
     * @return boolean
192
     */
193
    public function valid()
194
    {
195
        if (false === current($this->registry)) {
196
            return false;
197
        }
198
199
        return true;
200
    }
201
}
202