|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Asseco\JsonQueryBuilder; |
| 6 | + |
| 7 | +use Asseco\JsonQueryBuilder\Config\ModelConfig; |
| 8 | +use Asseco\JsonQueryBuilder\Config\OperatorsConfig; |
| 9 | +use Asseco\JsonQueryBuilder\Exceptions\JsonQueryBuilderException; |
| 10 | +use Asseco\JsonQueryBuilder\Traits\CleansValues; |
| 11 | +use Illuminate\Support\Facades\Config; |
| 12 | + |
| 13 | +class CustomFieldSearchParser implements SearchParserInterface |
| 14 | +{ |
| 15 | + use CleansValues; |
| 16 | + |
| 17 | + /** |
| 18 | + * Constant by which values will be split within a single parameter. E.g. parameter=value1;value2. |
| 19 | + */ |
| 20 | + const VALUE_SEPARATOR = ';'; |
| 21 | + |
| 22 | + public string $column; |
| 23 | + private string $argument; |
| 24 | + public array $values; |
| 25 | + public string $type; |
| 26 | + public string $operator; |
| 27 | + |
| 28 | + public string $cf_field_identificator = 'custom_field_id'; |
| 29 | + public string $cf_field_value = ''; |
| 30 | + |
| 31 | + private ModelConfig $modelConfig; |
| 32 | + |
| 33 | + /** |
| 34 | + * @param ModelConfig $modelConfig |
| 35 | + * @param OperatorsConfig $operatorsConfig |
| 36 | + * @param array $arguments |
| 37 | + * |
| 38 | + * @throws JsonQueryBuilderException |
| 39 | + */ |
| 40 | + public function __construct(ModelConfig $modelConfig, OperatorsConfig $operatorsConfig, array $arguments) |
| 41 | + { |
| 42 | + $this->modelConfig = $modelConfig; |
| 43 | + |
| 44 | + foreach ($arguments as $col => $val) { |
| 45 | + if (str_contains($col, $this->cf_field_identificator)) { |
| 46 | + $this->cf_field_value = $val; |
| 47 | + } else { |
| 48 | + $this->column = $col; |
| 49 | + $this->argument = $val; |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + $this->checkForForbiddenColumns(); |
| 54 | + |
| 55 | + $this->operator = $this->parseOperator($operatorsConfig->getOperators(), $this->argument); |
| 56 | + $arguments = str_replace($this->operator, '', $this->argument); |
| 57 | + $this->values = $this->splitValues($arguments); |
| 58 | + $this->type = $this->getColumnType(); |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * @param $operators |
| 63 | + * @param string $argument |
| 64 | + * @return string |
| 65 | + * |
| 66 | + * @throws JsonQueryBuilderException |
| 67 | + */ |
| 68 | + protected function parseOperator($operators, string $argument): string |
| 69 | + { |
| 70 | + foreach ($operators as $operator) { |
| 71 | + $argumentHasOperator = strpos($argument, $operator) !== false; |
| 72 | + |
| 73 | + if (!$argumentHasOperator) { |
| 74 | + continue; |
| 75 | + } |
| 76 | + |
| 77 | + return $operator; |
| 78 | + } |
| 79 | + |
| 80 | + throw new JsonQueryBuilderException("No valid callback registered for $argument. Are you missing an operator?"); |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * Split values by a given separator. |
| 85 | + * |
| 86 | + * Input: val1;val2 |
| 87 | + * |
| 88 | + * Output: val1 |
| 89 | + * val2 |
| 90 | + * |
| 91 | + * @param string $values |
| 92 | + * @return array |
| 93 | + * |
| 94 | + * @throws JsonQueryBuilderException |
| 95 | + */ |
| 96 | + protected function splitValues(string $values): array |
| 97 | + { |
| 98 | + $valueArray = explode(self::VALUE_SEPARATOR, $values); |
| 99 | + $cleanedUpValues = $this->cleanValues($valueArray); |
| 100 | + |
| 101 | + if (count($cleanedUpValues) < 1) { |
| 102 | + throw new JsonQueryBuilderException("Column '$this->column' is missing a value."); |
| 103 | + } |
| 104 | + |
| 105 | + return $cleanedUpValues; |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * @return string |
| 110 | + * |
| 111 | + * @throws JsonQueryBuilderException |
| 112 | + */ |
| 113 | + protected function getColumnType(): string |
| 114 | + { |
| 115 | + $columns = $this->modelConfig->getModelColumns(); |
| 116 | + |
| 117 | + if (!array_key_exists($this->column, $columns)) { |
| 118 | + // TODO: integrate recursive column check for related models? |
| 119 | + return 'generic'; |
| 120 | + } |
| 121 | + |
| 122 | + return $columns[$this->column]; |
| 123 | + } |
| 124 | + |
| 125 | + /** |
| 126 | + * Check if global forbidden key is used. |
| 127 | + * |
| 128 | + * @throws JsonQueryBuilderException |
| 129 | + */ |
| 130 | + protected function checkForForbiddenColumns() |
| 131 | + { |
| 132 | + $forbiddenKeys = Config::get('asseco-json-query-builder.global_forbidden_columns'); |
| 133 | + $forbiddenKeys = $this->modelConfig->getForbidden($forbiddenKeys); |
| 134 | + |
| 135 | + if (in_array($this->column, $forbiddenKeys)) { |
| 136 | + throw new JsonQueryBuilderException("Searching by '$this->column' field is forbidden. Check the configuration if this is not a desirable behavior."); |
| 137 | + } |
| 138 | + } |
| 139 | +} |
0 commit comments