1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Kojimify; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Class Kojimify provides tools to create ingenious texts. |
7
|
|
|
* |
8
|
|
|
* @package Kojimify |
9
|
|
|
*/ |
10
|
|
|
class Kojimify |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var string |
14
|
|
|
*/ |
15
|
|
|
protected $wordsSeparator; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var string |
19
|
|
|
*/ |
20
|
|
|
protected $charactersGlue; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
protected $linesGlue; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
protected $wordsGlue; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var string |
34
|
|
|
*/ |
35
|
|
|
protected $geniusMark; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Kojimify constructor. |
39
|
|
|
* |
40
|
|
|
* @param string $wordSeparator Character that is used as a words separator |
41
|
|
|
* @param string $charactersGlue Character that will be put between characters of a kojimified word |
42
|
|
|
* @param string $linesGlue Character that will be put between lines of a kojimified word |
43
|
|
|
* @param string $wordsGlue Character that will be used to join words of a kojimified text |
44
|
|
|
* @param string $geniusMark Character that is used to detect whether the text has increased genius |
45
|
|
|
*/ |
46
|
23 |
|
public function __construct( |
47
|
|
|
string $wordSeparator = ' ', |
48
|
|
|
string $charactersGlue = ' ', |
49
|
|
|
string $linesGlue = "\n", |
50
|
|
|
string $wordsGlue = "\n\n", |
51
|
|
|
string $geniusMark = '!' |
52
|
|
|
) { |
53
|
|
|
|
54
|
23 |
|
$this->wordsSeparator = $wordSeparator; |
55
|
23 |
|
$this->charactersGlue = $charactersGlue; |
56
|
23 |
|
$this->linesGlue = $linesGlue; |
57
|
23 |
|
$this->wordsGlue = $wordsGlue; |
58
|
23 |
|
$this->geniusMark = $geniusMark; |
59
|
23 |
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Bootstrap method that kojimifies the whole text. |
63
|
|
|
* |
64
|
|
|
* @param string $text |
65
|
|
|
* |
66
|
|
|
* @return string |
67
|
|
|
*/ |
68
|
3 |
|
public function processText(string $text): string |
69
|
|
|
{ |
70
|
3 |
|
$sourceText = $text; |
71
|
3 |
|
$isGenius = $this->isGenius($text); |
72
|
3 |
|
if ($isGenius) { |
73
|
1 |
|
$sourceText = $this->getCleanText($text); |
74
|
|
|
} |
75
|
|
|
|
76
|
3 |
|
$words = $this->splitText($sourceText); |
77
|
3 |
|
foreach ($words as &$word) { |
78
|
3 |
|
$word = $this->processWord($word, $isGenius); |
79
|
|
|
} |
80
|
3 |
|
unset($word); |
81
|
|
|
|
82
|
3 |
|
$processedText = $this->buildText($words); |
83
|
|
|
|
84
|
3 |
|
return $processedText; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Splits source text into separate words. |
89
|
|
|
* |
90
|
|
|
* @param string $text |
91
|
|
|
* |
92
|
|
|
* @return array |
93
|
|
|
*/ |
94
|
7 |
|
public function splitText(string $text): array |
95
|
|
|
{ |
96
|
7 |
|
$words = explode($this->wordsSeparator, $text); |
97
|
|
|
|
98
|
7 |
|
$wordsFiltered = array_filter($words); |
99
|
|
|
|
100
|
7 |
|
return array_values($wordsFiltered); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Kojimifies a single word. |
105
|
|
|
* |
106
|
|
|
* @param string $word |
107
|
|
|
* @param bool $isGenius |
108
|
|
|
* |
109
|
|
|
* @return string |
110
|
|
|
*/ |
111
|
7 |
|
public function processWord(string $word, bool $isGenius): string |
112
|
|
|
{ |
113
|
7 |
|
$characters = []; |
114
|
7 |
|
for ($i = 0; $i < mb_strlen($word); $i++) { |
115
|
7 |
|
$characters[] = mb_substr($word, $i, 1); |
116
|
|
|
} |
117
|
|
|
|
118
|
7 |
|
$processedWord = implode($this->charactersGlue, $characters); |
119
|
7 |
|
$wordLength = count($characters); |
120
|
7 |
|
for ($i = 1; $i < $wordLength; $i++) { |
121
|
7 |
|
$processedWord .= $this->linesGlue . $characters[$i]; |
122
|
7 |
|
if ($isGenius) { |
123
|
3 |
|
$processedWord .= str_repeat($this->charactersGlue, $i) . str_repeat(' ', $i - 1) . $characters[$i]; |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|
127
|
7 |
|
return $processedWord; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Detects if the text has increased genius. |
132
|
|
|
* |
133
|
|
|
* @param string $text |
134
|
|
|
* |
135
|
|
|
* @return bool |
136
|
|
|
*/ |
137
|
12 |
|
public function isGenius(string $text): bool |
138
|
|
|
{ |
139
|
12 |
|
$lastCharacter = mb_substr($text, -1); |
140
|
12 |
|
$isGenius = $lastCharacter === $this->geniusMark; |
141
|
|
|
|
142
|
12 |
|
return $isGenius; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Removes a genius mark from the text. |
147
|
|
|
* |
148
|
|
|
* @param string $text |
149
|
|
|
* |
150
|
|
|
* @return string |
151
|
|
|
*/ |
152
|
6 |
|
public function getCleanText(string $text): string |
153
|
|
|
{ |
154
|
6 |
|
while ($this->isGenius($text)) { |
155
|
4 |
|
$text = mb_substr($text, 0, mb_strlen($text) - 1); |
156
|
|
|
} |
157
|
|
|
|
158
|
6 |
|
return $text; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Builds kojimified text from kojimified words. |
163
|
|
|
* |
164
|
|
|
* @param array $words |
165
|
|
|
* |
166
|
|
|
* @return string |
167
|
|
|
*/ |
168
|
6 |
|
public function buildText(array $words): string |
169
|
|
|
{ |
170
|
6 |
|
$text = implode($this->wordsGlue, $words); |
171
|
|
|
|
172
|
6 |
|
return $text; |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
|