1
|
|
|
<?php
|
2
|
|
|
|
3
|
|
|
namespace Dgame\Variants;
|
4
|
|
|
|
5
|
|
|
use ICanBoogie\Inflector;
|
6
|
|
|
|
7
|
|
|
/**
|
8
|
|
|
* Class Variants
|
9
|
|
|
* @package Dgame\Variants
|
10
|
|
|
*/
|
11
|
|
|
class Variants
|
12
|
|
|
{
|
13
|
|
|
/**
|
14
|
|
|
* @var string[]
|
15
|
|
|
*/
|
16
|
|
|
private $values = [];
|
17
|
|
|
|
18
|
|
|
/**
|
19
|
|
|
* Variants constructor.
|
20
|
|
|
*
|
21
|
|
|
* @param string[] $values
|
22
|
|
|
*/
|
23
|
5 |
|
public function __construct(array $values)
|
24
|
|
|
{
|
25
|
5 |
|
$this->values = $values;
|
26
|
5 |
|
}
|
27
|
|
|
|
28
|
|
|
/**
|
29
|
|
|
* @return string[]
|
30
|
|
|
*/
|
31
|
|
|
final public function getValues(): array
|
32
|
|
|
{
|
33
|
|
|
return $this->values;
|
34
|
|
|
}
|
35
|
|
|
|
36
|
|
|
/**
|
37
|
|
|
* @param string ...$values
|
38
|
|
|
*
|
39
|
|
|
* @return Variants
|
|
|
|
|
40
|
|
|
*/
|
|
|
|
|
41
|
3 |
|
public static function ofArguments(string ...$values): self
|
42
|
|
|
{
|
43
|
3 |
|
return new self($values);
|
44
|
|
|
}
|
45
|
|
|
|
46
|
|
|
/**
|
47
|
|
|
* @param array $values
|
48
|
|
|
*
|
49
|
|
|
* @return Variants
|
|
|
|
|
50
|
|
|
*/
|
51
|
2 |
|
public static function ofArray(array $values): self
|
52
|
|
|
{
|
53
|
2 |
|
return new self($values);
|
54
|
|
|
}
|
55
|
|
|
|
56
|
|
|
/**
|
57
|
|
|
* @param callable ...$callbacks
|
58
|
|
|
*
|
59
|
|
|
* @return string[]
|
60
|
|
|
*/
|
|
|
|
|
61
|
2 |
|
final public function with(callable ...$callbacks): array
|
62
|
|
|
{
|
63
|
2 |
|
$output = [];
|
64
|
2 |
|
foreach ($this->values as $value) {
|
65
|
2 |
|
foreach ($callbacks as $callback) {
|
66
|
2 |
|
$output[] = $callback($value);
|
67
|
|
|
}
|
68
|
|
|
}
|
69
|
|
|
|
70
|
2 |
|
return $output;
|
71
|
|
|
}
|
72
|
|
|
|
73
|
|
|
/**
|
74
|
|
|
* @param array $patterns
|
75
|
|
|
*
|
76
|
|
|
* @return string[]
|
77
|
|
|
*/
|
78
|
1 |
|
final public function withPattern(array $patterns): array
|
79
|
|
|
{
|
80
|
1 |
|
$output = [];
|
81
|
1 |
|
foreach ($this->values as $value) {
|
82
|
1 |
|
foreach ($patterns as $pattern => $replacement) {
|
83
|
1 |
|
if (is_array($replacement)) {
|
84
|
1 |
|
$output = array_merge(self::replaceArray($pattern, $replacement, $value));
|
85
|
|
|
} else {
|
86
|
1 |
|
$output[] = self::replace($pattern, $replacement, $value);
|
87
|
|
|
}
|
88
|
|
|
}
|
89
|
|
|
}
|
90
|
|
|
|
91
|
1 |
|
return $output;
|
92
|
|
|
}
|
93
|
|
|
|
94
|
|
|
/**
|
95
|
|
|
* @param string $pattern
|
96
|
|
|
* @param array $replacements
|
97
|
|
|
* @param string $value
|
98
|
|
|
*
|
99
|
|
|
* @return array
|
100
|
|
|
*/
|
101
|
1 |
|
private static function replaceArray(string $pattern, array $replacements, string $value): array
|
102
|
|
|
{
|
103
|
1 |
|
$output = [];
|
104
|
1 |
|
foreach ($replacements as $replacement) {
|
105
|
1 |
|
$output[] = self::replace($pattern, $replacement, $value);
|
106
|
|
|
}
|
107
|
|
|
|
108
|
1 |
|
return $output;
|
109
|
|
|
}
|
110
|
|
|
|
111
|
|
|
/**
|
112
|
|
|
* @param string $pattern
|
113
|
|
|
* @param string $replacement
|
114
|
|
|
* @param string $value
|
115
|
|
|
*
|
116
|
|
|
* @return string|null
|
117
|
|
|
*/
|
118
|
1 |
|
private static function replace(string $pattern, string $replacement, string $value): ?string
|
119
|
|
|
{
|
120
|
1 |
|
return preg_replace($pattern, $replacement, $value);
|
121
|
|
|
}
|
122
|
|
|
|
123
|
|
|
/**
|
124
|
|
|
* @return string[]
|
125
|
|
|
*/
|
126
|
1 |
|
final public function withUpperLowerCaseFirst(): array
|
127
|
|
|
{
|
128
|
1 |
|
return $this->with('lcfirst', 'ucfirst');
|
129
|
|
|
}
|
130
|
|
|
|
131
|
|
|
/**
|
132
|
|
|
* @return string[]
|
133
|
|
|
*/
|
134
|
1 |
|
final public function withUpperLowerCase(): array
|
135
|
|
|
{
|
136
|
1 |
|
return $this->with('strtolower', 'strtoupper');
|
137
|
|
|
}
|
138
|
|
|
|
139
|
|
|
/**
|
140
|
|
|
* @return string[]
|
141
|
|
|
*/
|
142
|
2 |
|
final public function withCamelSnakeCase(): array
|
143
|
|
|
{
|
144
|
2 |
|
$output = [];
|
145
|
2 |
|
foreach ($this->values as $value) {
|
146
|
2 |
|
$value = self::replaceWhitespaces($value);
|
147
|
|
|
if (empty($value)) {
|
148
|
2 |
|
continue;
|
149
|
2 |
|
}
|
150
|
2 |
|
|
151
|
|
|
$output[] = Inflector::get()->camelize($value, Inflector::DOWNCASE_FIRST_LETTER);
|
152
|
|
|
$output[] = Inflector::get()->camelize($value, Inflector::UPCASE_FIRST_LETTER);
|
153
|
2 |
|
$output[] = Inflector::get()->underscore($value);
|
154
|
|
|
}
|
155
|
|
|
|
156
|
|
|
return $output;
|
157
|
|
|
}
|
158
|
|
|
|
159
|
1 |
|
/**
|
160
|
|
|
* @return array
|
161
|
1 |
|
*/
|
162
|
1 |
|
final public function assembled(): array
|
163
|
1 |
|
{
|
164
|
|
|
$output = $this->withCamelSnakeCase();
|
165
|
|
|
foreach ($this->values as $value) {
|
166
|
1 |
|
$value = self::replaceWhitespaces($value);
|
167
|
|
|
if (empty($value)) {
|
168
|
|
|
continue;
|
169
|
|
|
}
|
170
|
|
|
|
171
|
|
|
$output[] = Inflector::get()->dasherize($value);
|
172
|
|
|
}
|
173
|
|
|
|
174
|
2 |
|
return $output;
|
175
|
|
|
}
|
176
|
2 |
|
|
177
|
|
|
/**
|
178
|
|
|
* @param string $value
|
179
|
|
|
*
|
180
|
|
|
* @return string|null
|
181
|
|
|
*/
|
182
|
|
|
private static function replaceWhitespaces(string $value): ?string
|
183
|
|
|
{
|
184
|
|
|
return preg_replace('/\s+/', '_', trim($value));
|
185
|
|
|
}
|
186
|
|
|
}
|
187
|
|
|
|
This check compares the return type specified in the
@return
annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.