Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 39 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 1 |
| RegexCommand | |
0.00% |
0 / 39 |
|
0.00% |
0 / 3 |
42 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| buildQuery | |
0.00% |
0 / 15 |
|
0.00% |
0 / 1 |
20 | |||
| escapeString | |
0.00% |
0 / 22 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | /* |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; under version 2 |
| 7 | * of the License (non-upgradable). |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 17 | * |
| 18 | * Copyright (c) 2023 (original work) Open Assessment Technologies SA; |
| 19 | * |
| 20 | */ |
| 21 | |
| 22 | namespace oat\generis\model\kernel\persistence\starsql\search\Command; |
| 23 | |
| 24 | use WikibaseSolutions\CypherDSL\Expressions\Operators\Regex; |
| 25 | use WikibaseSolutions\CypherDSL\Query; |
| 26 | |
| 27 | class RegexCommand implements CommandInterface |
| 28 | { |
| 29 | private bool $hasStartWildcard; |
| 30 | private bool $hasEndWildcard; |
| 31 | |
| 32 | public function __construct(bool $startWildcard = false, bool $endWildcard = false) |
| 33 | { |
| 34 | $this->hasStartWildcard = $startWildcard; |
| 35 | $this->hasEndWildcard = $endWildcard; |
| 36 | } |
| 37 | |
| 38 | public function buildQuery($predicate, $values): Condition |
| 39 | { |
| 40 | // Compatibility with legacy queries |
| 41 | if (str_contains($values, '*')) { |
| 42 | $this->hasStartWildcard = str_starts_with($values, '*'); |
| 43 | $this->hasEndWildcard = str_ends_with($values, '*'); |
| 44 | $values = trim($values, '*'); |
| 45 | } |
| 46 | |
| 47 | $patternToken = $this->escapeString($values); |
| 48 | |
| 49 | if ($this->hasStartWildcard) { |
| 50 | $patternToken = '.*' . $patternToken; |
| 51 | } |
| 52 | |
| 53 | if ($this->hasEndWildcard) { |
| 54 | $patternToken = $patternToken . '.*'; |
| 55 | } |
| 56 | |
| 57 | return new Condition( |
| 58 | new Regex($predicate, $valueParam = Query::parameter()), |
| 59 | [ |
| 60 | $valueParam->getParameter() => "(?i)" . $patternToken, |
| 61 | ] |
| 62 | ); |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * @param $values |
| 67 | * |
| 68 | * @return string |
| 69 | */ |
| 70 | public function escapeString($values): string |
| 71 | { |
| 72 | return strtr( |
| 73 | trim($values, '%'), |
| 74 | [ |
| 75 | '.' => '\\.', |
| 76 | '+' => '\\+', |
| 77 | '?' => '\\?', |
| 78 | '[' => '\\[', |
| 79 | ']' => '\\]', |
| 80 | '(' => '\\(', |
| 81 | ')' => '\\)', |
| 82 | '{' => '\\{', |
| 83 | '}' => '\\}', |
| 84 | '^' => '\\^', |
| 85 | '$' => '\\$', |
| 86 | '|' => '\\|', |
| 87 | '\\_' => '_', |
| 88 | '\\%' => '%', |
| 89 | '*' => '.*', |
| 90 | '_' => '.', |
| 91 | '%' => '.*', |
| 92 | ] |
| 93 | ); |
| 94 | } |
| 95 | } |