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 Dictionary |
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 01.11.2011 |
32
|
|
|
*/ |
33
|
|
|
|
34
|
|
|
namespace Org\Heigl\Hyphenator\Dictionary; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* This class provides a registry for storing multiple dictionaries |
38
|
|
|
* |
39
|
|
|
* @category Hyphenation |
40
|
|
|
* @package Org\Heigl\Hyphenator |
41
|
|
|
* @subpackage Dictionary |
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 01.11.2011 |
48
|
|
|
*/ |
49
|
|
|
class DictionaryRegistry implements \Iterator, \Countable |
50
|
|
|
{ |
51
|
|
|
/** |
52
|
|
|
* Storage for the Dictionaries. |
53
|
|
|
* |
54
|
|
|
* @var Dictionary[] $registry |
55
|
|
|
*/ |
56
|
|
|
protected $registry = array(); |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Add an item to the registry. |
60
|
|
|
* |
61
|
|
|
* @param Dictionary $dict The Dictionary to add to the registry |
62
|
|
|
* |
63
|
|
|
* @return DictionaryRegistry |
64
|
|
|
*/ |
65
|
|
|
public function add(Dictionary $dict) |
66
|
|
|
{ |
67
|
|
|
if (! in_array($dict, $this->registry)) { |
68
|
|
|
$this->registry[] = $dict; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
return $this; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Get a dictionary entry by its key |
76
|
|
|
* |
77
|
|
|
* @param mixed $key The key to retrieve the Dictionary for |
78
|
|
|
* |
79
|
|
|
* @return Dictionary|null |
80
|
|
|
*/ |
81
|
|
|
public function getDictionaryWithKey($key) |
82
|
|
|
{ |
83
|
|
|
if (array_key_exists($key, $this->registry)) { |
84
|
|
|
return $this->registry[$key]; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return null; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Get an array of hyphenation-patterns for a given word. |
92
|
|
|
* |
93
|
|
|
* @deprecated use getHyphenationPatterns() instead |
94
|
|
|
* |
95
|
|
|
* @param string $word The word to get the patterns for. |
96
|
|
|
* |
97
|
|
|
* @return array |
98
|
|
|
*/ |
99
|
|
|
public function getHyphenationPattterns($word) |
100
|
|
|
{ |
101
|
|
|
return $this->getHyphenationPatterns($word); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Get an array of hyphenation-patterns for a given word. |
106
|
|
|
* |
107
|
|
|
* @param string $word The word to get the patterns for. |
108
|
|
|
* |
109
|
|
|
* @return array |
110
|
|
|
*/ |
111
|
|
|
public function getHyphenationPatterns($word) |
112
|
|
|
{ |
113
|
|
|
$pattern = array(array()); |
114
|
|
|
foreach ($this as $dictionary) { |
115
|
|
|
$pattern[] = $dictionary->getPatternsForWord($word); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
return array_merge(...$pattern); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Implementation of \Iterator |
123
|
|
|
* |
124
|
|
|
* @see \Iterator::rewind() |
125
|
|
|
* |
126
|
|
|
* @return void |
127
|
|
|
*/ |
128
|
|
|
public function rewind() |
129
|
|
|
{ |
130
|
|
|
reset($this->registry); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Get the current object |
135
|
|
|
* |
136
|
|
|
* @see \Iterator::current() |
137
|
|
|
* |
138
|
|
|
* @return \Org\Heigl\Hyphenator\Dictionary\Dictionary |
139
|
|
|
*/ |
140
|
|
|
public function current() |
141
|
|
|
{ |
142
|
|
|
return current($this->registry); |
|
|
|
|
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Get the current key |
147
|
|
|
* |
148
|
|
|
* @see \Iterator::key() |
149
|
|
|
* |
150
|
|
|
* @return mixed |
151
|
|
|
*/ |
152
|
|
|
public function key() |
153
|
|
|
{ |
154
|
|
|
return key($this->registry); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Get the number of items in the registry |
159
|
|
|
* |
160
|
|
|
* @see \Countable::count() |
161
|
|
|
* |
162
|
|
|
* @return int |
163
|
|
|
*/ |
164
|
|
|
public function count() |
165
|
|
|
{ |
166
|
|
|
return count($this->registry); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Push the internal pointer forward one step |
171
|
|
|
* |
172
|
|
|
* @see \Iterator::next() |
173
|
|
|
* |
174
|
|
|
* @return void |
175
|
|
|
*/ |
176
|
|
|
public function next() |
177
|
|
|
{ |
178
|
|
|
next($this->registry); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Check whether the current pointer is in a valid place |
183
|
|
|
* |
184
|
|
|
* @see \Iterator::valid() |
185
|
|
|
* |
186
|
|
|
* @return boolean |
187
|
|
|
*/ |
188
|
|
|
public function valid() |
189
|
|
|
{ |
190
|
|
|
if (false === current($this->registry)) { |
191
|
|
|
return false; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
return true; |
195
|
|
|
} |
196
|
|
|
} |
197
|
|
|
|