|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/* |
| 6 | + * Copyright MacFJA |
| 7 | + * |
| 8 | + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated |
| 9 | + * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the |
| 10 | + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to |
| 11 | + * permit persons to whom the Software is furnished to do so, subject to the following conditions: |
| 12 | + * |
| 13 | + * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the |
| 14 | + * Software. |
| 15 | + * |
| 16 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE |
| 17 | + * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR |
| 18 | + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR |
| 19 | + * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 20 | + */ |
| 21 | + |
| 22 | +namespace MacFJA\RediSearch\Helper; |
| 23 | + |
| 24 | +use function preg_quote; |
| 25 | +use function preg_replace; |
| 26 | +use function sprintf; |
| 27 | +use function str_split; |
| 28 | + |
| 29 | +class EscapeHelper |
| 30 | +{ |
| 31 | + private const NOT_ESCAPED_CHAR_REGEX_PATTERN = '/(?<!\\\\)((?:\\\\\\\\)*)(%s)/'; |
| 32 | + |
| 33 | + private const CHAR_AT_WORD_BEGIN_REGEX_PATTERN = '/(^|\s+)(%s)/'; |
| 34 | + |
| 35 | + public static function escapeFieldName(string $text): string |
| 36 | + { |
| 37 | + $text = self::escapeCommon($text); |
| 38 | + |
| 39 | + return self::escapeCharAtWordBegin($text, '-'); |
| 40 | + } |
| 41 | + |
| 42 | + public static function escapeWord(string $text): string |
| 43 | + { |
| 44 | + return self::escapeCommon($text); |
| 45 | + } |
| 46 | + |
| 47 | + public static function escapeExactMatch(string $text): string |
| 48 | + { |
| 49 | + return self::escapeCommon($text); |
| 50 | + } |
| 51 | + |
| 52 | + public static function escapeFuzzy(string $text): string |
| 53 | + { |
| 54 | + return self::escapeCommon($text); |
| 55 | + } |
| 56 | + |
| 57 | + public static function escapeNegation(string $text): string |
| 58 | + { |
| 59 | + $text = self::escapeCommon($text); |
| 60 | + $text = self::escapeCharAtWordBegin($text, '-'); |
| 61 | + |
| 62 | + for ($number = 0; $number < 10; $number++) { |
| 63 | + $text = self::escapeCharAtWordBegin($text, (string) $number); |
| 64 | + } |
| 65 | + |
| 66 | + return $text; |
| 67 | + } |
| 68 | + |
| 69 | + public static function escapeOptional(string $text): string |
| 70 | + { |
| 71 | + return self::escapeCommon($text); |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * @codeCoverageIgnore |
| 76 | + */ |
| 77 | + private static function escapeCharAtWordBegin(string $inText, string $char): string |
| 78 | + { |
| 79 | + return preg_replace( |
| 80 | + sprintf(self::CHAR_AT_WORD_BEGIN_REGEX_PATTERN, preg_quote($char, '/')), |
| 81 | + '$1\\\\$2', |
| 82 | + $inText |
| 83 | + ) ?? $inText; |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * @codeCoverageIgnore |
| 88 | + */ |
| 89 | + private static function escapeChar(string $inText, string $char): string |
| 90 | + { |
| 91 | + return preg_replace( |
| 92 | + sprintf(self::NOT_ESCAPED_CHAR_REGEX_PATTERN, preg_quote($char, '/')), |
| 93 | + '$1\\\\$2', |
| 94 | + $inText |
| 95 | + ) ?? $inText; |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * @codeCoverageIgnore |
| 100 | + */ |
| 101 | + private static function escapeCommon(string $inText): string |
| 102 | + { |
| 103 | + $anywhere = str_split(',.<>{}[]"\':;!@#$%^&*()-+=~|'); |
| 104 | + |
| 105 | + foreach ($anywhere as $char) { |
| 106 | + $inText = self::escapeChar($inText, $char); |
| 107 | + } |
| 108 | + |
| 109 | + return self::unescapeNumber($inText); |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * @codeCoverageIgnore |
| 114 | + */ |
| 115 | + private static function unescapeNumber(string $inText): string |
| 116 | + { |
| 117 | + return preg_replace('/(?:\\s|^)\\\\(-\\d)/', '$1', $inText) ?? $inText; |
| 118 | + } |
| 119 | +} |
0 commit comments