|
2 | 2 |
|
3 | 3 | namespace EncoreDigitalGroup\StdLib\Objects;
|
4 | 4 |
|
| 5 | +use EncoreDigitalGroup\StdLib\Exceptions\ArgumentNullException; |
5 | 6 | use Illuminate\Support\Arr as ArraySupport;
|
| 7 | +use TypeError; |
6 | 8 |
|
7 | 9 | /**
|
8 | 10 | * @api
|
9 | 11 | *
|
10 | 12 | * @internal
|
11 |
| - * |
12 |
| - * @codeCoverageIgnore |
13 | 13 | */
|
14 | 14 | class Arr extends ArraySupport
|
15 | 15 | {
|
| 16 | + public static function convertToShortSyntax(array|string $code): array|string |
| 17 | + { |
| 18 | + // Regular expression to match long array syntax |
| 19 | + $pattern = '/array\s*\(([^()]*(?:(?R)[^()]*)*)\)/m'; |
| 20 | + |
| 21 | + // Callback function to replace long array syntax with short array syntax |
| 22 | + $callback = function ($matches): string { |
| 23 | + // Recursively convert nested arrays |
| 24 | + $innerArray = $matches[1]; |
| 25 | + $convertedInnerArray = self::convertToShortSyntax($innerArray); |
| 26 | + |
| 27 | + // Handle proper formatting of nested arrays |
| 28 | + $convertedInnerArray = preg_replace('/\n\s*/', ' ', $convertedInnerArray); |
| 29 | + |
| 30 | + if (!is_string($convertedInnerArray)) { |
| 31 | + throw new TypeError(str_concat_space('Expected string, got ', get_type($convertedInnerArray))); |
| 32 | + } |
| 33 | + |
| 34 | + return "[\n " . $convertedInnerArray . "\n]"; |
| 35 | + }; |
| 36 | + |
| 37 | + /** |
| 38 | + * Replace all instances of long array syntax with short array syntax |
| 39 | + */ |
| 40 | + $convertedCode = preg_replace_callback($pattern, $callback, $code); |
| 41 | + |
| 42 | + /** Ensure array brackets are properly placed on the same line as the key. This regex searches for an |
| 43 | + * opening square bracket '[' followed by one or more whitespace characters '\s+' and replaces it with |
| 44 | + * the same opening bracket '[' followed by a new line '\n', ensuring the array's opening bracket appears |
| 45 | + * on its own line. |
| 46 | + */ |
| 47 | + $convertedCode = preg_replace('/\[\s+/', "[\n", $convertedCode ?? ''); |
| 48 | + |
| 49 | + /** |
| 50 | + * Ensure array brackets are properly placed on the same line as the key (closing brackets). This regex searches |
| 51 | + * for a closing square bracket ']' followed by one or more whitespace characters '\s+' and replaces it with just |
| 52 | + * the closing bracket ']', ensuring that the array's closing bracket is placed correctly without extra whitespace. |
| 53 | + */ |
| 54 | + $convertedCode = preg_replace('/\]\s+/', '] ', $convertedCode ?? ''); |
| 55 | + |
| 56 | + /** |
| 57 | + * Ensure array brackets are properly placed on the same line as the key (opening brackets in nested arrays). This regex |
| 58 | + * searches for a comma ',' followed by one or more whitespace characters '\s+' and an opening square bracket '[' and |
| 59 | + * replaces it with just a comma ',' followed by a space and an opening bracket '[', ensuring proper spacing between the |
| 60 | + * comma and the opening bracket in nested arrays. |
| 61 | + */ |
| 62 | + $convertedCode = preg_replace('/,\s+\[/', ', [', $convertedCode ?? ''); |
| 63 | + |
| 64 | + /** |
| 65 | + * Add new lines after commas that are outside of array values. This regex searches for a comma ',' followed by zero or |
| 66 | + * more whitespace characters '\s*' and a closing square bracket ']' and replaces it with just a comma ',' followed by a |
| 67 | + * new line '\n' and a closing bracket ']', ensuring that a new line is triggered before the closing bracket ']'. |
| 68 | + */ |
| 69 | + $convertedCode = preg_replace('/,\s*\]/', ",\n]", $convertedCode ?? ''); |
| 70 | + |
| 71 | + /** |
| 72 | + * Format array keys to ensure brackets are on the same line. This regex matches and captures an opening square bracket '\[' |
| 73 | + * followed by zero or more whitespace characters '\s*', then captures any characters that are not commas, closing or opening |
| 74 | + * square brackets '([^,\]\[]+?)', and finally captures a closing square bracket '\]'. It replaces the matches with the captured |
| 75 | + * groups, ensuring the brackets are not separated by whitespace. |
| 76 | + */ |
| 77 | + $convertedCode = preg_replace('/(\[)\s*([^,\]\[]+?)\s*(\])/', '$1$2$3', $convertedCode ?? ''); |
| 78 | + |
| 79 | + /** |
| 80 | + * Add new lines after closing brackets of arrays. This regex searches for a closing bracket ']' followed by zero or more whitespace |
| 81 | + * characters '\s*' and a comma ',' and replaces it with just the closing bracket ']' followed by a new line '\n', ensuring each array |
| 82 | + * element is on a new line. |
| 83 | + */ |
| 84 | + $convertedCode = preg_replace('/\]\s*,/', "],\n", $convertedCode ?? ''); |
| 85 | + |
| 86 | + /** |
| 87 | + * Replace every occurrence of a comma ',' in the $convertedCode string with a comma followed by a newline ",\n". |
| 88 | + */ |
| 89 | + $convertedCode = preg_replace('/,/', ",\n", $convertedCode ?? ''); |
| 90 | + |
| 91 | + /** |
| 92 | + * Add indentation for nested arrays. This regex searches for a comma ',' followed by zero or more whitespace characters '\s*', a new line |
| 93 | + * '\n', zero or more whitespace characters '\s*', and an opening square bracket '[' and replaces it with a comma ',' followed by a new line |
| 94 | + * '\n' and indentation ' ' equivalent to four spaces and an opening square bracket '['. |
| 95 | + */ |
| 96 | + $convertedCode = preg_replace('/,\s*\n\s*\[/', ",\n [", $convertedCode ?? ''); |
| 97 | + |
| 98 | + /** |
| 99 | + * Add a new line after the closing bracket of the entire array declaration. This regex searches for a closing bracket ']' followed by zero or |
| 100 | + * more whitespace characters '\s*' and a semicolon ';' and replaces it with just the closing bracket ']' followed by a semicolon ';' and a new |
| 101 | + * line '\n'. |
| 102 | + */ |
| 103 | + $convertedCode = preg_replace('/\]\s*;/', '];', $convertedCode ?? ''); |
| 104 | + |
| 105 | + $convertedCode = preg_replace("/(^[\r\n]*|[\r\n]+)[\s\t]*[\r\n]+/", "\n", $convertedCode ?? ''); |
| 106 | + |
| 107 | + if (is_null($convertedCode) || empty($convertedCode)) { |
| 108 | + throw new ArgumentNullException('convertedCode'); |
| 109 | + } |
| 110 | + |
| 111 | + return $convertedCode; |
| 112 | + } |
16 | 113 | }
|
0 commit comments