-
Notifications
You must be signed in to change notification settings - Fork 0
/
morpher.php
182 lines (159 loc) · 4.21 KB
/
morpher.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
<?php
class morpher_generator
{
const salt = '123456';
function __construct($seed)
{
$this->seed = $seed;
$this->value = md5($seed . self::salt);
}
function get($min, $max)
{
$this->value = md5($this->value . $this->seed . self::salt);
$value = hexdec(substr($this->value, 0, 8));
return $min + ($value % ($max - $min + 1));
}
private $seed;
private $value;
}
class morpher_node_text
{
function __construct($value)
{
$this->value = $value;
}
function get()
{
return $this->value;
}
private $value;
}
class morpher_node_variant
{
function __construct($generator)
{
$this->generator = $generator;
}
function push($node)
{
$this->variants[] = $node;
}
function get()
{
$position = $this->generator->get(0, count($this->variants) - 1);
return $this->variants[$position]->get();
}
private $generator;
private $variants = array();
}
class morpher_node
{
function push($node)
{
$this->children[] = $node;
}
function get()
{
$text = '';
foreach($this->children as $node)
{
$text .= $node->get();
}
return $text;
}
private $children = array();
}
class morpher
{
const variant_begin = '[';
const variant_delimiter = '|';
const variant_end = ']';
function __construct($template, $seed)
{
$this->node = new morpher_node();
self::parse_node($this->node, $template, 0, strlen($template), new morpher_generator($seed));
}
static function get($template, $seed)
{
$morpher = new morpher($template, $seed);
return $morpher->roll();
}
function roll()
{
return $this->node->get();
}
private static function variant_ending($str, $begin, $end)
{
$depth = 0;
for($n = $begin; $n != $end; ++$n)
{
if($str[$n] == self::variant_begin)
{
++$depth;
}
else if($str[$n] == self::variant_end)
{
if($depth)
{
--$depth;
}
else
{
return $n;
}
}
}
return $end;
}
private static function parse_variant($variant, $str, $begin, $end, $generator)
{
$depth = 0;
for($n = $begin, $tn = $begin; $n != $end; ++$n)
{
if($str[$n] == self::variant_begin)
{
++$depth;
}
else if($str[$n] == self::variant_end)
{
--$depth;
}
else if($str[$n] == self::variant_delimiter && !$depth)
{
$node = new morpher_node();
self::parse_node($node, $str, $tn, $n, $generator);
$variant->push($node);
$tn = $n + 1;
}
}
$node = new morpher_node();
self::parse_node($node, $str, $tn, $end, $generator);
$variant->push($node);
}
private static function parse_node($node, $str, $begin, $end, $generator)
{
for($n = $begin, $tn = $begin; $n != $end; ++$n)
{
if($str[$n] == self::variant_begin)
{
if($n != $begin)
{
$node->push(new morpher_node_text(substr($str, $tn, $n - $tn)));
}
$begin_variant = $n + 1;
$end_variant = self::variant_ending($str, $begin_variant, $end);
$variant = new morpher_node_variant($generator);
self::parse_variant($variant, $str, $begin_variant, $end_variant, $generator);
$node->push($variant);
$n = $end_variant;
$tn = $n + 1;
}
}
if($tn != $end)
{
$node->push(new morpher_node_text(substr($str, $tn, $end - $tn)));
}
}
private $node;
}
?>