|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of PHP-Typography. |
|
4
|
|
|
* |
|
5
|
|
|
* Copyright 2017 Peter Putzer. |
|
6
|
|
|
* |
|
7
|
|
|
* This program is free software; you can redistribute it and/or modify |
|
8
|
|
|
* it under the terms of the GNU General Public License as published by |
|
9
|
|
|
* the Free Software Foundation; either version 2 of the License, or |
|
10
|
|
|
* (at your option) any later version. |
|
11
|
|
|
* |
|
12
|
|
|
* This program is distributed in the hope that it will be useful, |
|
13
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
14
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
15
|
|
|
* GNU General Public License for more details. |
|
16
|
|
|
* |
|
17
|
|
|
* You should have received a copy of the GNU General Public License along |
|
18
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc., |
|
19
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|
20
|
|
|
* |
|
21
|
|
|
* *** |
|
22
|
|
|
* |
|
23
|
|
|
* @package mundschenk-at/php-typography |
|
24
|
|
|
* @license http://www.gnu.org/licenses/gpl-2.0.html |
|
25
|
|
|
*/ |
|
26
|
|
|
|
|
27
|
|
|
namespace PHP_Typography\Hyphenator; |
|
28
|
|
|
|
|
29
|
|
|
use PHP_Typography\Strings; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* A hyphenation pattern trie node. |
|
33
|
|
|
* |
|
34
|
|
|
* Portions of this code have been inspired by: |
|
35
|
|
|
* - hyphenator-php (https://nikonyrh.github.io/phphyphenation.html) |
|
36
|
|
|
* |
|
37
|
|
|
* @author Peter Putzer <[email protected]> |
|
38
|
|
|
* |
|
39
|
|
|
* @since 5.0.0 |
|
40
|
|
|
*/ |
|
41
|
|
|
final class Trie_Node { |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* The offsets array. |
|
45
|
|
|
* |
|
46
|
|
|
* @var array |
|
47
|
|
|
*/ |
|
48
|
|
|
private $offsets = []; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Linked trie nodes. |
|
52
|
|
|
* |
|
53
|
|
|
* @var array { |
|
54
|
|
|
* @type Trie_Node $char The next node in the given character path. |
|
55
|
|
|
* } |
|
56
|
|
|
*/ |
|
57
|
|
|
private $links = []; |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Create new Trie_Node. |
|
61
|
|
|
*/ |
|
62
|
1 |
|
private function __construct() { |
|
63
|
1 |
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Retrieves the node for the given letter (or creates it). |
|
67
|
|
|
* |
|
68
|
|
|
* @param string $char A single character. |
|
69
|
|
|
* |
|
70
|
|
|
* @return Trie_Node |
|
71
|
|
|
*/ |
|
72
|
2 |
|
public function get_node( $char ) { |
|
73
|
2 |
|
if ( ! isset( $this->links[ $char ] ) ) { |
|
74
|
1 |
|
$this->links[ $char ] = new Trie_Node(); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
2 |
|
return $this->links[ $char ]; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Checks if there is a node for the given letter. |
|
82
|
|
|
* |
|
83
|
|
|
* @param string $char A single character. |
|
84
|
|
|
* |
|
85
|
|
|
* @return bool |
|
86
|
|
|
*/ |
|
87
|
1 |
|
public function exists( $char ) { |
|
88
|
1 |
|
return ! empty( $this->links[ $char ] ); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Retrieves the offsets array. |
|
93
|
|
|
* |
|
94
|
|
|
* @return array |
|
95
|
|
|
*/ |
|
96
|
1 |
|
public function offsets() { |
|
97
|
1 |
|
return $this->offsets; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Builds pattern search trie from pattern list(s). |
|
102
|
|
|
* |
|
103
|
|
|
* @param array $patterns An array of hyphenation patterns. |
|
104
|
|
|
* |
|
105
|
|
|
* @return Trie_Node The starting node of the trie. |
|
106
|
|
|
*/ |
|
107
|
1 |
|
public static function build_trie( array $patterns ) { |
|
108
|
1 |
|
$trie = new Trie_Node(); |
|
109
|
|
|
|
|
110
|
1 |
|
foreach ( $patterns as $key => $pattern ) { |
|
111
|
1 |
|
$node = $trie; |
|
112
|
|
|
|
|
113
|
1 |
|
foreach ( Strings::mb_str_split( $key ) as $char ) { |
|
114
|
1 |
|
$node = $node->get_node( $char ); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
1 |
|
\preg_match_all( '/([1-9])/S', $pattern, $offsets, PREG_OFFSET_CAPTURE ); |
|
118
|
1 |
|
$node->offsets = $offsets[1]; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
1 |
|
return $trie; |
|
122
|
|
|
} |
|
123
|
|
|
} |
|
124
|
|
|
|