-
Notifications
You must be signed in to change notification settings - Fork 0
/
CaseSwitcher.php
194 lines (170 loc) · 5 KB
/
CaseSwitcher.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
<?php
namespace Kolyunya\StringProcessor\Format;
use Exception;
use Kolyunya\StringProcessor\BaseProcessor;
use Kolyunya\StringProcessor\Format\CaseSwitcherInterface;
/**
* A base class for all case switchers.
* @author Kolyunya
*/
abstract class CaseSwitcher extends BaseProcessor implements CaseSwitcherInterface
{
/**
* A constant for an undefined case.
*/
const UNDEFINED_CASE = 0x00;
/**
* A constant for a "snake_case".
*/
const SNAKE_CASE = 0x01;
/**
* A constant for a "CamelCase".
*/
const CAMEL_CASE = 0x02;
/**
* A constant for a "kebab-case".
*/
const KEBAB_CASE = 0x03;
/**
* A constant for a "Sentence case".
*/
const SENTENCE_CASE = 0x04;
/**
* Source case of the string.
* @var integer
*/
protected $sourceCase = self::UNDEFINED_CASE;
/**
* Destination case of the string.
* @var integer
*/
protected $destinationCase = self::UNDEFINED_CASE;
/**
* @inheritdoc
*/
public function setSourceCase($sourceCase)
{
$this->sourceCase = $sourceCase;
}
/**
* @inheritdoc
*/
protected function selfProcession($string)
{
// Check whether the destination string case is already reached.
if ($this->destinationCaseIsReached()) {
return $string;
}
// Perform preprocesstion.
$string = $this->preprocess($string);
// Perform actual procession depending on the source case.
switch ($this->sourceCase) {
case self::SNAKE_CASE:
$string = $this->processSnakeCase($string);
break;
case self::CAMEL_CASE:
$string = $this->processCamelCase($string);
break;
case self::KEBAB_CASE:
$string = $this->processKebabCase($string);
break;
case self::SENTENCE_CASE:
$string = $this->processSentenceCase($string);
break;
default:
$string = $this->processUndefinedCase($string);
}
// Perform postprocesstion.
$string = $this->postprocess($string);
return $string;
}
/**
* Performs preprocession on a source string.
* Override this function in actual formatters.
* @param string $string Source sring to perform preprocession on.
*/
protected function preprocess($string)
{
return $string;
}
/**
* Performs postprocession on a processed string.
* Override this function in actual formatters.
* @param string $string Processed sring to perform postprocession on.
*/
protected function postprocess($string)
{
return $string;
}
/**
* Processes a string in an "snake_case".
* Override this function in actual formatters.
* @param string $string String to process.
* @return string Processed string.
*/
protected function processSnakeCase($string)
{
$this->throwProcessionException();
}
/**
* Processes a string in an "CamelCase".
* Override this function in actual formatters.
* @param string $string String to process.
* @return string Processed string.
*/
protected function processCamelCase($string)
{
$this->throwProcessionException();
}
/**
* Processes a string in an "kebab-case".
* Override this function in actual formatters.
* @param string $string String to process.
* @return string Processed string.
*/
protected function processKebabCase($string)
{
$this->throwProcessionException();
}
/**
* Processes a string in an "Sentence case".
* Override this function in actual formatters.
* @param string $string String to process.
* @return string Processed string.
*/
protected function processSentenceCase($string)
{
$this->throwProcessionException();
}
/**
* Processes a string in an undefined case.
* Override this function in actual formatters.
* @param string $string String to process.
* @return string Processed string.
*/
protected function processUndefinedCase($string)
{
$this->throwProcessionException();
}
/**
* Checks whether the destination string case is already reached.
* @return boolean Indicates whether the destination string case is already reached.
*/
private function destinationCaseIsReached()
{
$casesAreEqual = $this->sourceCase === $this->destinationCase;
$casesAreKnown = $this->sourceCase !== self::UNDEFINED_CASE;
$destinationCaseIsReached = $casesAreEqual && $casesAreKnown;
return $destinationCaseIsReached;
}
/**
* Throws a string procession exception.
* @throws Exception
*/
private function throwProcessionException()
{
$message = 'String procession failed.';
$exception = new Exception($message);
throw $exception;
}
}