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 Options |
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 07.09.2011 |
32
|
|
|
*/ |
33
|
|
|
|
34
|
|
|
namespace Org\Heigl\Hyphenator; |
35
|
|
|
|
36
|
|
|
use \Org\Heigl\Hyphenator\Exception; |
37
|
|
|
use \Org\Heigl\Hyphenator\Tokenizer\Tokenizer; |
38
|
|
|
use \Org\Heigl\Hyphenator\Filter\Filter; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* This class provides Options for the Hyphenator. |
42
|
|
|
* |
43
|
|
|
* @category Hyphenation |
44
|
|
|
* @package Org\Heigl\Hyphenator |
45
|
|
|
* @subpackage Options |
46
|
|
|
* @author Andreas Heigl <[email protected]> |
47
|
|
|
* @copyright 2008-2011 Andreas Heigl<[email protected]> |
48
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php MIT-License |
49
|
|
|
* @version 2.0.1 |
50
|
|
|
* @link http://github.com/heiglandreas/Hyphenator |
51
|
|
|
* @since 07.09.2011 |
52
|
|
|
*/ |
53
|
|
|
class Options |
54
|
|
|
{ |
55
|
|
|
/** |
56
|
|
|
* The String that marks a word not to be hyphenated. |
57
|
|
|
* |
58
|
|
|
* This string has to be prepend to the word in question |
59
|
|
|
* |
60
|
|
|
* @var string _noHyphenateString |
61
|
|
|
*/ |
62
|
|
|
protected $_noHyphenateString = null; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* This property defines the default hyphenation-character. |
66
|
|
|
* |
67
|
|
|
* By default this is the soft-hyphen character U+00AD |
68
|
|
|
* |
69
|
|
|
* @var string $_hyphen |
70
|
|
|
*/ |
71
|
|
|
protected $_hyphen = "\xAD"; |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* How many chars to stay to the left of the first hyphenation of a word. |
75
|
|
|
* |
76
|
|
|
* By default this is 2 characters |
77
|
|
|
* |
78
|
|
|
* @var int $_leftmin |
79
|
|
|
*/ |
80
|
|
|
protected $_leftMin = 2; |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* How many chars to stay to the right of the last hyphenation of a word. |
84
|
|
|
* |
85
|
|
|
* By default this is 2 characters |
86
|
|
|
* |
87
|
|
|
* @var int $_rightmin |
88
|
|
|
*/ |
89
|
|
|
protected $_rightMin = 2; |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Minimum Word length for Hyphenation. |
93
|
|
|
* |
94
|
|
|
* This defaults to 6 Characters. |
95
|
|
|
* |
96
|
|
|
* @var int $_wordMin |
97
|
|
|
*/ |
98
|
|
|
protected $_wordMin = 6; |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* The currently set quality for hyphenation. |
102
|
|
|
* |
103
|
|
|
* The higher the number, the better the hyphenation is |
104
|
|
|
* |
105
|
|
|
* @var int $_quality |
106
|
|
|
*/ |
107
|
|
|
protected $_quality = 9; |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* The String that shall be searched for as a customHyphen. |
111
|
|
|
* |
112
|
|
|
* @var string $_customHyphen |
113
|
|
|
*/ |
114
|
|
|
protected $_customHyphen = '--'; |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* The filters to be used to postprocess the hyphenations. |
118
|
|
|
* |
119
|
|
|
* @var array $_filters |
120
|
|
|
*/ |
121
|
|
|
protected $_filters = array(); |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* The tokenizers to use. |
125
|
|
|
* |
126
|
|
|
* @var array $_tokenizers |
127
|
|
|
*/ |
128
|
|
|
protected $_tokenizers = array(); |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* THe locale to be used. |
132
|
|
|
* |
133
|
|
|
* @var string $_locale |
134
|
|
|
*/ |
135
|
|
|
protected $_defaultLocale = 'en_EN'; |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Set the String that marks a word as not to be hyphenated |
139
|
|
|
* |
140
|
|
|
* @param string $noHyphenateString The string that marks a word not to be |
141
|
|
|
* hyphenated |
142
|
|
|
* |
143
|
|
|
* @return \Org\Heigl\Hyphenator\Options |
144
|
|
|
*/ |
145
|
|
|
public function setNoHyphenateString($noHyphenateString) |
146
|
|
|
{ |
147
|
|
|
$this->_noHyphenateString = $noHyphenateString; |
148
|
|
|
|
149
|
|
|
return $this; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Get the String that marks a word as not to be hyphenated |
154
|
|
|
* |
155
|
|
|
* @return string |
156
|
|
|
*/ |
157
|
|
|
public function getNoHyphenateString() |
158
|
|
|
{ |
159
|
|
|
return $this->_noHyphenateString; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Set the hyphen-string |
164
|
|
|
* |
165
|
|
|
* @param string $hyphen The hyphen to use |
166
|
|
|
* |
167
|
|
|
* @return \Org\Heigl\Hyphenator\Options |
168
|
|
|
*/ |
169
|
|
|
public function setHyphen($hyphen) |
170
|
|
|
{ |
171
|
|
|
$this->_hyphen = $hyphen; |
172
|
|
|
|
173
|
|
|
return $this; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Get the hyphen-string |
178
|
|
|
* |
179
|
|
|
* @return string |
180
|
|
|
*/ |
181
|
|
|
public function getHyphen() |
182
|
|
|
{ |
183
|
|
|
return $this->_hyphen; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Set the Minimum left characters |
188
|
|
|
* |
189
|
|
|
* @param int $leftMin Left minimum Chars |
190
|
|
|
* |
191
|
|
|
* @return \Org\Heigl\Hyphenator\Options |
192
|
|
|
*/ |
193
|
|
|
public function setLeftMin($leftMin) |
194
|
|
|
{ |
195
|
|
|
$this->_leftMin = (int) $leftMin; |
196
|
|
|
|
197
|
|
|
return $this; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* Get the minimum left characters |
202
|
|
|
* |
203
|
|
|
* @return int |
204
|
|
|
*/ |
205
|
|
|
public function getLeftMin() |
206
|
|
|
{ |
207
|
|
|
return (int) $this->_leftMin; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* Set the Minimum right characters |
212
|
|
|
* |
213
|
|
|
* @param int $rightMin Right minimum Characters |
214
|
|
|
* |
215
|
|
|
* @return \Org\Heigl\Hyphenator\Options |
216
|
|
|
*/ |
217
|
|
|
public function setRightMin($rightMin) |
218
|
|
|
{ |
219
|
|
|
$this->_rightMin = (int) $rightMin; |
220
|
|
|
|
221
|
|
|
return $this; |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* Get the minimum right characters |
226
|
|
|
* |
227
|
|
|
* @return int |
228
|
|
|
*/ |
229
|
|
|
public function getRightMin() |
230
|
|
|
{ |
231
|
|
|
return (int) $this->_rightMin; |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* Set the minimum size of a word to be hyphenated |
236
|
|
|
* |
237
|
|
|
* Words with less characters (not byte!) are not to be hyphenated |
238
|
|
|
* |
239
|
|
|
* @param int $minLength Minimum Word-Length |
240
|
|
|
* |
241
|
|
|
* @return \Org\Heigl\Hyphenator\Options |
242
|
|
|
*/ |
243
|
|
|
public function setMinWordLength($minLength) |
244
|
|
|
{ |
245
|
|
|
$this->_wordMin = (int) $minLength; |
246
|
|
|
|
247
|
|
|
return $this; |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
/** |
251
|
|
|
* This is a wrapper for setMinWordLength |
252
|
|
|
* |
253
|
|
|
* @param int $wordLength The minimum word Length |
254
|
|
|
* |
255
|
|
|
* @return \Org\Heigl\Hyphenator\Options |
256
|
|
|
*/ |
257
|
|
|
public function setWordMin($wordLength) |
258
|
|
|
{ |
259
|
|
|
return $this->setMinWordLength($wordLength); |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
/** |
263
|
|
|
* Set the hyphenation quality |
264
|
|
|
* |
265
|
|
|
* @param int $quality The new Hyphenation Quality |
266
|
|
|
* |
267
|
|
|
* @return \Org\Heigl\Hyphenator\Options |
268
|
|
|
*/ |
269
|
|
|
public function setQuality($quality) |
270
|
|
|
{ |
271
|
|
|
$this->_quality = (int) $quality; |
272
|
|
|
|
273
|
|
|
return $this; |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
/** |
277
|
|
|
* Get the hyphenation-quality |
278
|
|
|
* |
279
|
|
|
* @return int |
280
|
|
|
*/ |
281
|
|
|
public function getQuality() |
282
|
|
|
{ |
283
|
|
|
return $this->_quality; |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
/** |
287
|
|
|
* Get the minimum Length of a word to be hyphenated |
288
|
|
|
* |
289
|
|
|
* @return int |
290
|
|
|
*/ |
291
|
|
|
public function getMinWordLength() |
292
|
|
|
{ |
293
|
|
|
return (int) $this->_wordMin; |
294
|
|
|
} |
295
|
|
|
|
296
|
|
|
/** |
297
|
|
|
* Set the string that is treated as a custom Hyphenation |
298
|
|
|
* |
299
|
|
|
* These will be replaced by the set hyphenation character |
300
|
|
|
* |
301
|
|
|
* @param array $customHyphen The custom hyphenation-character |
302
|
|
|
* |
303
|
|
|
* @return \Org\Heigl\Hyphenator\Options |
304
|
|
|
*/ |
305
|
|
|
public function setCustomHyphen($customHyphen) |
306
|
|
|
{ |
307
|
|
|
$this->_customHyphen = (string) $customHyphen; |
308
|
|
|
|
309
|
|
|
return $this; |
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
/** |
313
|
|
|
* Get the custom Hyphen |
314
|
|
|
* |
315
|
|
|
* @return string |
316
|
|
|
*/ |
317
|
|
|
public function getCustomHyphen() |
318
|
|
|
{ |
319
|
|
|
return $this->_customHyphen; |
320
|
|
|
} |
321
|
|
|
|
322
|
|
|
/** |
323
|
|
|
* Set the filters |
324
|
|
|
* |
325
|
|
|
* @param string|array $filters The filters to use as comma separated list |
326
|
|
|
* or array |
327
|
|
|
* |
328
|
|
|
* @return \Org\Heigl\Hyphenator\Options |
329
|
|
|
*/ |
330
|
|
|
public function setFilters($filters) |
331
|
|
|
{ |
332
|
|
|
$this->_filters = array(); |
333
|
|
|
if (! is_array($filters)) { |
334
|
|
|
$filters = explode(',', $filters); |
335
|
|
|
} |
336
|
|
|
foreach ($filters as $filter) { |
337
|
|
|
$this->addFilter($filter); |
338
|
|
|
} |
339
|
|
|
|
340
|
|
|
return $this; |
341
|
|
|
} |
342
|
|
|
|
343
|
|
|
/** |
344
|
|
|
* Add a filter to the options-array |
345
|
|
|
* |
346
|
|
|
* @param string|Filter $filter The filter to be added |
347
|
|
|
* |
348
|
|
|
* @throws \UnexpectedValueException |
349
|
|
|
* @return \Org\Heigl\Hyphenator\Options |
350
|
|
|
*/ |
351
|
|
|
public function addFilter($filter) |
352
|
|
|
{ |
353
|
|
|
if (is_string($filter)) { |
354
|
|
|
$filter = trim($filter); |
355
|
|
|
} elseif (! $filter instanceof Filter) { |
356
|
|
|
throw new \UnexpectedValueException('Expceted instanceof Org\Heigl\Hyphenator\Filter\Filter or string'); |
357
|
|
|
} |
358
|
|
|
if (! $filter) { |
359
|
|
|
return $this; |
360
|
|
|
} |
361
|
|
|
$this->_filters[] = $filter; |
362
|
|
|
|
363
|
|
|
return $this; |
364
|
|
|
} |
365
|
|
|
|
366
|
|
|
/** |
367
|
|
|
* Get all the filters |
368
|
|
|
* |
369
|
|
|
* @return array |
370
|
|
|
*/ |
371
|
|
|
public function getFilters() |
372
|
|
|
{ |
373
|
|
|
return $this->_filters; |
374
|
|
|
} |
375
|
|
|
|
376
|
|
|
/** |
377
|
|
|
* Set the tokenizers to use |
378
|
|
|
* |
379
|
|
|
* @param string|array $tokenizers The Tokenizers to use |
380
|
|
|
* |
381
|
|
|
* @return \Org\Heigl\Hyphenator\Options |
382
|
|
|
*/ |
383
|
|
|
public function setTokenizers($tokenizers) |
384
|
|
|
{ |
385
|
|
|
$this->_tokenizers = array(); |
386
|
|
|
if (! is_array($tokenizers)) { |
387
|
|
|
$tokenizers = explode(',', $tokenizers); |
388
|
|
|
} |
389
|
|
|
foreach ($tokenizers as $tokenizer) { |
390
|
|
|
$this->addTokenizer($tokenizer); |
391
|
|
|
} |
392
|
|
|
|
393
|
|
|
return $this; |
394
|
|
|
} |
395
|
|
|
|
396
|
|
|
/** |
397
|
|
|
* Add a tokenizer to the tomeizer-list |
398
|
|
|
* |
399
|
|
|
* @param string|Tokenizer $tokenizer The tokenizer to add |
400
|
|
|
* |
401
|
|
|
* @return \Org\Heigl\Hyphenator\Options |
402
|
|
|
*/ |
403
|
|
|
public function addTokenizer($tokenizer) |
404
|
|
|
{ |
405
|
|
|
if (is_string($tokenizer)) { |
406
|
|
|
$tokenizer = trim($tokenizer); |
407
|
|
|
} elseif (! $tokenizer instanceof Tokenizer) { |
408
|
|
|
throw new \UnexpectedValueException('Expceted instanceof Org\Heigl\Hyphenator\Tokenizer\Tokenizer or string'); |
409
|
|
|
} |
410
|
|
|
|
411
|
|
|
if (! $tokenizer) { |
412
|
|
|
return $this; |
413
|
|
|
} |
414
|
|
|
$this->_tokenizers[] = $tokenizer; |
415
|
|
|
|
416
|
|
|
return $this; |
417
|
|
|
} |
418
|
|
|
|
419
|
|
|
/** |
420
|
|
|
* Get all the tokenizers |
421
|
|
|
* |
422
|
|
|
* @return array |
423
|
|
|
*/ |
424
|
|
|
public function getTokenizers() |
425
|
|
|
{ |
426
|
|
|
return $this->_tokenizers; |
427
|
|
|
} |
428
|
|
|
|
429
|
|
|
/** |
430
|
|
|
* Create an Option-Object by parsing a given file. |
431
|
|
|
* |
432
|
|
|
* @param string $file The config-file to be parsed |
433
|
|
|
* |
434
|
|
|
* @throws \Org\Heigl\Hyphenator\Exception\PathNotFoundException |
435
|
|
|
* @throws \Org\Heigl\Hyphenator\Exception\InvalidArgumentException |
436
|
|
|
* @return \Org\Heigl\Hyphenator\Options |
437
|
|
|
*/ |
438
|
|
|
public static function factory($file) |
439
|
|
|
{ |
440
|
|
|
if (! file_Exists($file)) { |
441
|
|
|
$file = $file . '.dist'; |
442
|
|
|
if (! file_exists($file)) { |
443
|
|
|
throw new \Org\Heigl\Hyphenator\Exception\PathNotFoundException($file); |
444
|
|
|
} |
445
|
|
|
} |
446
|
|
|
$params = parse_ini_file($file); |
447
|
|
|
if (! is_array($params) || 1 > count($params)) { |
448
|
|
|
throw new \Org\Heigl\Hyphenator\Exception\InvalidArgumentException($file . ' is not a parseable file'); |
449
|
|
|
} |
450
|
|
|
|
451
|
|
|
$option = new Options(); |
452
|
|
|
foreach ($params as $key => $val) { |
453
|
|
|
if (! method_Exists($option, 'set' . $key)) { |
454
|
|
|
continue; |
455
|
|
|
} |
456
|
|
|
call_user_Func(array($option,'set' . $key), $val); |
457
|
|
|
} |
458
|
|
|
|
459
|
|
|
return $option; |
460
|
|
|
} |
461
|
|
|
|
462
|
|
|
/** |
463
|
|
|
* Set the default locale for this instance |
464
|
|
|
* |
465
|
|
|
* @param string $locale The locale to be set |
466
|
|
|
* |
467
|
|
|
* @return \Org\Heigl\Hyphenator\Options |
468
|
|
|
*/ |
469
|
|
|
public function setDefaultLocale($locale) |
470
|
|
|
{ |
471
|
|
|
$this->_defaultLocale = (string) $locale; |
472
|
|
|
|
473
|
|
|
return $this; |
474
|
|
|
} |
475
|
|
|
|
476
|
|
|
/** |
477
|
|
|
* Get the default locale for this instance |
478
|
|
|
* |
479
|
|
|
* @return string |
480
|
|
|
*/ |
481
|
|
|
public function getDefaultLocale() |
482
|
|
|
{ |
483
|
|
|
return $this->_defaultLocale; |
484
|
|
|
} |
485
|
|
|
} |
486
|
|
|
|