|
7 | 7 | use DragonCode\SizeSorter\Enums\GroupEnum; |
8 | 8 | use Illuminate\Support\Str; |
9 | 9 |
|
10 | | -use function array_keys; |
11 | | -use function array_values; |
12 | | -use function preg_replace_callback; |
13 | | -use function str_repeat; |
| 10 | +use function in_array; |
| 11 | +use function preg_match; |
| 12 | +use function strlen; |
14 | 13 |
|
15 | 14 | class LetterClothingGroup extends Group |
16 | 15 | { |
17 | 16 | protected static GroupEnum $group = GroupEnum::LetterClothingSize; |
18 | 17 |
|
19 | 18 | protected static array|string $pattern = '/^(?\'size\'(([2-9]?[x]{1,9}[sml]{1})|([sml])))(_(?1))?$/'; |
20 | 19 |
|
21 | | - public static function normalize(mixed $value, int|string $key): string |
| 20 | + protected static array $multiplier = [ |
| 21 | + 's' => 100, |
| 22 | + 'm' => 1000, |
| 23 | + 'l' => 10000, |
| 24 | + ]; |
| 25 | + |
| 26 | + protected static function prepare(mixed $value): string |
| 27 | + { |
| 28 | + return Str::of($value) |
| 29 | + ->explode('_') |
| 30 | + ->map(static fn (mixed $value) => static::convert($value)) |
| 31 | + ->implode('_'); |
| 32 | + } |
| 33 | + |
| 34 | + protected static function convert(mixed $value): string |
22 | 35 | { |
23 | | - if (! static::containsNumber($value)) { |
24 | | - return parent::normalize($value . '0', $key); |
| 36 | + if (in_array($value, ['s', 'm', 'l'])) { |
| 37 | + return (string) static::multiply($value, 1); |
25 | 38 | } |
26 | 39 |
|
27 | | - $matches = static::match($value); |
| 40 | + if (preg_match('/^(x+)?s$/', $value, $matches)) { |
| 41 | + $count = isset($matches[1]) ? strlen($matches[1]) : 1; |
28 | 42 |
|
29 | | - $value = Str::replace( |
30 | | - array_keys($matches), |
31 | | - array_values($matches), |
32 | | - $value |
33 | | - ); |
| 43 | + return (string) static::multiply('s', $count); |
| 44 | + } |
34 | 45 |
|
35 | | - return parent::normalize($value . '1', $key); |
36 | | - } |
| 46 | + if (preg_match('/^(\d+)xs$/', $value, $matches)) { |
| 47 | + return (string) static::multiply('s', (int) $matches[1], 1); |
| 48 | + } |
37 | 49 |
|
38 | | - protected static function containsNumber(string $value): bool |
39 | | - { |
40 | | - return Str::isMatch('/\d/', $value); |
| 50 | + if (preg_match('/^(x+)l$/', $value, $matches)) { |
| 51 | + $count = strlen($matches[1]); |
| 52 | + |
| 53 | + return (string) static::multiply('l', $count); |
| 54 | + } |
| 55 | + |
| 56 | + if (preg_match('/^(\d+)xl$/', $value, $matches)) { |
| 57 | + return (string) static::multiply('l', (int) $matches[1], 1); |
| 58 | + } |
| 59 | + |
| 60 | + return '0'; |
41 | 61 | } |
42 | 62 |
|
43 | | - protected static function match(string $value): array |
| 63 | + protected static function multiply(string $key, int $count, int $plus = 0): int |
44 | 64 | { |
45 | | - return Str::of($value) |
46 | | - ->matchAll('/\dx/') |
47 | | - ->mapWithKeys(static function (string $item) { |
48 | | - $replace = preg_replace_callback('/(\d)x/', function (array $matches) { |
49 | | - return str_repeat('x', (int) $matches[1]); |
50 | | - }, $item); |
51 | | - |
52 | | - return [$item => $replace]; |
53 | | - }) |
54 | | - ->all(); |
| 65 | + return static::$multiplier[$key] * $count + $plus; |
55 | 66 | } |
56 | 67 | } |
0 commit comments