-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathactions.php
237 lines (211 loc) · 5.73 KB
/
actions.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
<?php
/**
* Created by PhpStorm.
* User: Vincent Sy
* Date: 10/25/2018
* Time: 12:06 AM
*/
$handler = new Handler($_POST['rules'], $_POST['inputText']);
echo json_encode($handler->doAction());
class Handler
{
private $_RULES;
private $_INPUT_TEXT;
private $_OUTPUT_TEXT;
public function __construct($rules = array(), $inputText = "")
{
$this->_RULES = $rules;
$this->_INPUT_TEXT = (string) $inputText;
$this->_OUTPUT_TEXT = (string) $inputText;
} //__construct
public function doAction()
{
$this->doRemoveDuplicateLines();
$this->doTextCaseActions();
$this->doSpaceActions();
$this->doLineBreakActions();
$this->doSortActions();
$this->doShowLines();
$this->doRemoveLines();
return $this->_OUTPUT_TEXT;
} //doAction
/**
* Will do update of string to uppercase or lowercase, will update words to start uppercase
*/
private function doTextCaseActions()
{
switch ($this->_RULES['textCase']) {
default:
break; //do nothing
case 0;
break; //do nothing
case 1:
$tempOutputText = $this->getOutputText();
$this->setOutputText(strtolower($tempOutputText));
break;
case 2:
$tempOutputText = $this->getOutputText();
$this->setOutputText(strtoupper($tempOutputText));
break;
case 3:
$tempOutputText = $this->getOutputText();
$this->setOutputText(ucwords($tempOutputText));
break;
}
} //doTextCaseActions
/**
* Will do removal of space/whitespaces
*/
private function doSpaceActions()
{
switch ($this->_RULES['space']) {
default:
break; //do nothing
case 0;
break; //do nothing
case 1:
$tempOutputText = $this->getOutputText();
//$this->setOutputText(trim(preg_replace('/\s+/', '', $tempOutputText))); //removes all whitespaces including linebreaks
$this->setOutputText(str_replace(' ', '', $tempOutputText));
break;
}
} //doSpaceActions
/**
* Will do removal of linebreaks
*/
private function doLineBreakActions()
{
switch ($this->_RULES['lineBreak']) {
default:
break; //do nothing
case 0;
break; //do nothing
case 1:
$tempOutputText = $this->getOutputText();
$this->setOutputText(trim(preg_replace('/\r|\n/', '', $tempOutputText)));
break;
}
} //doSpaceActions
/**
* Will do sorting of texts
*/
private function doSortActions()
{
switch ($this->_RULES['sort']) {
default:
break; //do nothing
case 0;
break; //do nothing
case 1:
$tempOutputText = $this->getOutputText();
$tempArr = explode("\n", $tempOutputText);
sort($tempArr);
$tempOutputText = implode("\n", $tempArr);
$this->setOutputText($tempOutputText);
break;
case 2:
$tempOutputText = $this->getOutputText();
$tempArr = explode("\n", $tempOutputText);
rsort($tempArr);
$tempOutputText = implode("\n", $tempArr);
$this->setOutputText($tempOutputText);
break;
}
} //doSortActions
/**
* Will do removing of duplicates
*/
private function doRemoveDuplicateLines()
{
if ('true' === $this->_RULES['removeDuplicateLines']) {
$tempOutputText = $this->getOutputText();
$tempArr = explode("\n", $tempOutputText);
$newArr = array();
foreach ($tempArr as $item) {
if (!in_array($item, $newArr)) {
$newArr[] = $item;
}
}
$tempOutputText = implode("\n", $newArr);
$this->setOutputText($tempOutputText);
}
} //doRemoveDuplicateLines
/**
* Will display lines matching criteria for word/character count
*/
private function doShowLines()
{
if (is_numeric($this->_RULES['show']['wordCount'])) {
$tempOutputText = $this->getOutputText();
$tempArr = explode("\n", $tempOutputText);
$newArr = array();
foreach ($tempArr as $item) {
//check word count
if (count(preg_split('/\s+/', $item)) == $this->_RULES['show']['wordCount'] && !in_array($item, $newArr)) {
$newArr[] = $item;
}
}
$tempOutputText = implode("\n", $newArr);
$this->setOutputText($tempOutputText);
}
if (is_numeric($this->_RULES['show']['characterCount'])) {
$tempOutputText = $this->getOutputText();
$tempArr = explode("\n", $tempOutputText);
$newArr = array();
foreach ($tempArr as $item) {
//check character count
if (strlen((string) $item) == $this->_RULES['show']['characterCount'] && !in_array($item, $newArr)) {
$newArr[] = $item;
}
}
$tempOutputText = implode("\n", $newArr);
$this->setOutputText($tempOutputText);
}
} //doShowLines
/**
* Will remove lines matching criteria for word/character count
*/
private function doRemoveLines()
{
if (is_numeric($this->_RULES['remove']['wordCount'])) {
$tempOutputText = $this->getOutputText();
$tempArr = explode("\n", $tempOutputText);
$newArr = array();
foreach ($tempArr as $item) {
//check word count
if (count(preg_split('/\s+/', $item)) == $this->_RULES['remove']['wordCount']) {
continue;
}
if (!in_array($item, $newArr)) {
$newArr[] = $item;
}
}
$tempOutputText = implode("\n", $newArr);
$this->setOutputText($tempOutputText);
}
if (is_numeric($this->_RULES['remove']['characterCount'])) {
$tempOutputText = $this->getOutputText();
$tempArr = explode("\n", $tempOutputText);
$newArr = array();
foreach ($tempArr as $item) {
//check character count
if (strlen((string) $item) == $this->_RULES['remove']['characterCount']) {
continue;
}
if (!in_array($item, $newArr)) {
$newArr[] = $item;
}
}
$tempOutputText = implode("\n", $newArr);
$this->setOutputText($tempOutputText);
}
} //doRemoveLines
private function setOutputText($outputText)
{
$this->_OUTPUT_TEXT = (string) $outputText;
} //setOutputText
private function getOutputText()
{
return $this->_OUTPUT_TEXT;
} //getOutputText
} //Handler