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