Pattern   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 1
dl 0
loc 88
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setPattern() 0 19 5
A factory() 0 7 1
A getText() 0 8 2
A getPattern() 0 8 2
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      24.11.2011
32
 */
33
34
namespace Org\Heigl\Hyphenator\Dictionary;
35
36
/**
37
 * This class provides a pattern for hyphenation
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      24.11.2011
48
 */
49
class Pattern
50
{
51
    /**
52
     * The internal storage of the text.
53
     *
54
     * @var string $text
55
     */
56
    protected $text = '';
57
58
    /**
59
     * The internal storage for the pattern.
60
     *
61
     * @var string $pattern
62
     */
63
    protected $pattern = '';
64
65
    /**
66
     * Set the pattern.
67
     *
68
     * @param string $pattern The UTF-8 encoded Pattern to store
69
     *
70
     * @return Pattern
71
     */
72
    public function setPattern($pattern)
73
    {
74
        $item = trim($pattern);
75
        $strlen = mb_strlen($item);
76
        for ($i = 0; $i < $strlen; $i++) {
77
            if ((($i) <= $strlen) && preg_match('/[0-9]/u', mb_substr($item, $i, 1))) {
78
                $this->pattern .= mb_substr($item, $i, 1);
79
                $i++;
80
            } else {
81
                $this->pattern .= '0';
82
            }
83
        }
84
        $this->text = preg_replace(array('/[0-9]/u','/\'/u'), array('','\\’'), $item);
85
        if (strlen($this->pattern) == mb_strlen($this->text)) {
86
            $this->pattern .= '0';
87
        }
88
89
        return $this;
90
    }
91
92
    /**
93
     * Creates Pattern-Instance and provide it with the given Pattern
94
     *
95
     * @param string $pattern The pattern to store
96
     *
97
     * @return Pattern
98
     */
99
    public static function factory($pattern)
100
    {
101
        $p = new Pattern();
102
        $p->setPattern($pattern);
103
104
        return $p;
105
    }
106
107
    /**
108
     * Get the text of the pattern
109
     *
110
     * @throws \Org\Heigl\Hyphenator\Exception\NoPatternSetException
111
     * @return string
112
     */
113
    public function getText()
114
    {
115
        if (! $this->text) {
116
            throw new \Org\Heigl\Hyphenator\Exception\NoPatternSetException('No Pattern set');
117
        }
118
119
        return $this->text;
120
    }
121
122
    /**
123
     * Get the pattern of this instance
124
     *
125
     * @throws \Org\Heigl\Hyphenator\Exception\NoPatternSetException
126
     * @return string
127
     */
128
    public function getPattern()
129
    {
130
        if (! $this->pattern) {
131
            throw new \Org\Heigl\Hyphenator\Exception\NoPatternSetException('No Pattern set');
132
        }
133
134
        return $this->pattern;
135
    }
136
}
137