|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Respect\StringFormatter; |
| 6 | + |
| 7 | +use function array_key_exists; |
| 8 | +use function count; |
| 9 | +use function implode; |
| 10 | +use function mb_str_split; |
| 11 | +use function mb_strlen; |
| 12 | +use function mb_strtolower; |
| 13 | +use function mb_strtoupper; |
| 14 | +use function mb_substr; |
| 15 | +use function preg_match; |
| 16 | +use function str_ends_with; |
| 17 | + |
| 18 | +final readonly class PatternFormatter implements Formatter |
| 19 | +{ |
| 20 | + private const array FILTERING_PATTERNS = [ |
| 21 | + '#' => 'any', |
| 22 | + '0' => 'digit', |
| 23 | + 'A' => 'upper_alpha', |
| 24 | + 'a' => 'lower_alpha', |
| 25 | + 'C' => 'alpha', |
| 26 | + 'W' => 'alphanumeric', |
| 27 | + 'X' => 'hex', |
| 28 | + '!' => 'punctuation', |
| 29 | + '@' => 'symbol', |
| 30 | + ]; |
| 31 | + |
| 32 | + private const array TRANSFORMATION_PATTERNS = [ |
| 33 | + 'd' => 'delete', |
| 34 | + 'l' => 'lower_single', |
| 35 | + 'L' => 'lower_until_reset', |
| 36 | + 'u' => 'upper_single', |
| 37 | + 'U' => 'upper_until_reset', |
| 38 | + 'i' => 'invert_single', |
| 39 | + 'I' => 'invert_until_reset', |
| 40 | + 'E' => 'reset_transformations', |
| 41 | + ]; |
| 42 | + |
| 43 | + public function __construct( |
| 44 | + private string $pattern, |
| 45 | + ) { |
| 46 | + if ($this->pattern === '') { |
| 47 | + throw new InvalidFormatterException('Pattern cannot be empty'); |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + public function format(string $input): string |
| 52 | + { |
| 53 | + $inputChars = mb_str_split($input); |
| 54 | + $inputIndex = 0; |
| 55 | + $result = []; |
| 56 | + $transformation = null; |
| 57 | + $patternIndex = 0; |
| 58 | + |
| 59 | + while ($patternIndex < mb_strlen($this->pattern)) { |
| 60 | + $patternChar = mb_substr($this->pattern, $patternIndex, 1); |
| 61 | + |
| 62 | + // Handle escape sequences |
| 63 | + if ($patternChar === '\\') { |
| 64 | + $nextPatternChar = mb_substr($this->pattern, $patternIndex + 1, 1); |
| 65 | + |
| 66 | + if ($nextPatternChar !== '') { |
| 67 | + // Handle escaped transformation patterns |
| 68 | + if (array_key_exists($nextPatternChar, self::TRANSFORMATION_PATTERNS)) { |
| 69 | + match (self::TRANSFORMATION_PATTERNS[$nextPatternChar]) { |
| 70 | + 'delete' => $inputIndex++, |
| 71 | + 'lower_single' => $transformation = 'lower_single', |
| 72 | + 'upper_single' => $transformation = 'upper_single', |
| 73 | + 'invert_single' => $transformation = 'invert_single', |
| 74 | + 'lower_until_reset' => $transformation = 'lower', |
| 75 | + 'upper_until_reset' => $transformation = 'upper', |
| 76 | + 'invert_until_reset' => $transformation = 'invert', |
| 77 | + 'reset_transformations' => $transformation = null, |
| 78 | + }; |
| 79 | + $patternIndex += 2; |
| 80 | + continue; |
| 81 | + } |
| 82 | + |
| 83 | + // For backslash followed by any other character, output that character literally |
| 84 | + $result[] = $nextPatternChar; |
| 85 | + $patternIndex += 2; |
| 86 | + continue; |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + // Handle transformation patterns |
| 91 | + if (array_key_exists($patternChar, self::TRANSFORMATION_PATTERNS)) { |
| 92 | + match (self::TRANSFORMATION_PATTERNS[$patternChar]) { |
| 93 | + 'delete' => $inputIndex++, |
| 94 | + 'lower_single' => $transformation = 'lower_single', |
| 95 | + 'upper_single' => $transformation = 'upper_single', |
| 96 | + 'invert_single' => $transformation = 'invert_single', |
| 97 | + 'lower_until_reset' => $transformation = 'lower', |
| 98 | + 'upper_until_reset' => $transformation = 'upper', |
| 99 | + 'invert_until_reset' => $transformation = 'invert', |
| 100 | + 'reset_transformations' => $transformation = null, |
| 101 | + }; |
| 102 | + $patternIndex++; |
| 103 | + continue; |
| 104 | + } |
| 105 | + |
| 106 | + // Handle filtering patterns |
| 107 | + if (array_key_exists($patternChar, self::FILTERING_PATTERNS)) { |
| 108 | + $inputIndex = $this->consumeNextMatchingChar($inputChars, $inputIndex, $patternChar, $result, $transformation); |
| 109 | + $patternIndex++; |
| 110 | + continue; |
| 111 | + } |
| 112 | + |
| 113 | + // Handle literal characters - they appear in output as-is and don't consume input |
| 114 | + $result[] = $patternChar; |
| 115 | + $patternIndex++; |
| 116 | + } |
| 117 | + |
| 118 | + return implode('', $result); |
| 119 | + } |
| 120 | + |
| 121 | + private function consumeNextMatchingChar(array $inputChars, int $inputIndex, string $filter, array &$result, string|null &$transformation): int |
| 122 | + { |
| 123 | + while ($inputIndex < count($inputChars)) { |
| 124 | + if ($this->matchesFilter($filter, $inputChars[$inputIndex])) { |
| 125 | + if ($transformation !== null) { |
| 126 | + $tempTransformation = $transformation; |
| 127 | + // Clear single-use transformations |
| 128 | + if (str_ends_with($transformation, '_single')) { |
| 129 | + $transformation = null; |
| 130 | + } |
| 131 | + |
| 132 | + $this->appendWithTransformation($result, $inputChars, $inputIndex, $tempTransformation); |
| 133 | + } else { |
| 134 | + $result[] = $inputChars[$inputIndex]; |
| 135 | + $inputIndex++; |
| 136 | + } |
| 137 | + |
| 138 | + break; |
| 139 | + } |
| 140 | + |
| 141 | + $inputIndex++; // Skip non-matching character |
| 142 | + } |
| 143 | + |
| 144 | + return $inputIndex; |
| 145 | + } |
| 146 | + |
| 147 | + private function matchesFilter(string $filter, string $char): bool |
| 148 | + { |
| 149 | + if ($char === '') { |
| 150 | + return false; |
| 151 | + } |
| 152 | + |
| 153 | + // First check if this is a filtering pattern key |
| 154 | + if (array_key_exists($filter, self::FILTERING_PATTERNS)) { |
| 155 | + $filterType = self::FILTERING_PATTERNS[$filter]; |
| 156 | + |
| 157 | + return match ($filterType) { |
| 158 | + 'any' => true, |
| 159 | + 'digit' => preg_match('/^[0-9]$/', $char) === 1, |
| 160 | + 'upper_alpha' => preg_match('/^[A-Z]$/', $char) === 1, |
| 161 | + 'lower_alpha' => preg_match('/^[a-z]$/', $char) === 1, |
| 162 | + 'alpha' => preg_match('/^\p{L}$/u', $char) === 1, |
| 163 | + 'alphanumeric' => preg_match('/^[\p{L}\p{N}]$/u', $char) === 1, |
| 164 | + 'hex' => preg_match('/^[0-9A-Fa-f]$/', $char) === 1, |
| 165 | + 'punctuation' => preg_match('/^[^\w\s]$/', $char) === 1, |
| 166 | + 'symbol' => preg_match('/^[^\w\s]$/', $char) === 1, |
| 167 | + default => false, |
| 168 | + }; |
| 169 | + } |
| 170 | + |
| 171 | + return $char === $filter; |
| 172 | + } |
| 173 | + |
| 174 | + private function appendWithTransformation(array &$result, array $inputChars, int &$inputIndex, string $transformation): void |
| 175 | + { |
| 176 | + if ($inputIndex >= count($inputChars)) { |
| 177 | + return; |
| 178 | + } |
| 179 | + |
| 180 | + $char = $inputChars[$inputIndex]; |
| 181 | + $inputIndex++; |
| 182 | + |
| 183 | + $result[] = match ($transformation) { |
| 184 | + 'lower', 'lower_single' => mb_strtolower($char), |
| 185 | + 'upper', 'upper_single' => mb_strtoupper($char), |
| 186 | + 'invert', 'invert_single' => mb_strtolower($char) === $char ? mb_strtoupper($char) : mb_strtolower($char), |
| 187 | + default => $char, |
| 188 | + }; |
| 189 | + } |
| 190 | +} |
0 commit comments