-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathCategorizedValues.php
126 lines (102 loc) · 3.34 KB
/
CategorizedValues.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
<?php
declare(strict_types=1);
namespace Asseco\JsonQueryBuilder;
use Asseco\JsonQueryBuilder\Config\TypesConfig;
use Asseco\JsonQueryBuilder\Types\AbstractType;
class CategorizedValues
{
/**
* Constants for micro-operator declaration.
*/
const NOT = '!';
const LIKE = '%';
const IS_NULL = 'null';
const IS_NOT_NULL = '!null';
protected SearchParserInterface $searchParser;
public array $and = [];
public array $andLike = [];
public array $not = [];
public array $notLike = [];
public bool $null = false;
public bool $notNull = false;
protected AbstractType $type;
/**
* CategorizedValues constructor.
*
* @param SearchParserInterface $searchParser
*
* @throws Exceptions\JsonQueryBuilderException
*/
public function __construct(SearchParserInterface $searchParser)
{
$this->searchParser = $searchParser;
$this->type = (new TypesConfig())->getTypeClassFromTypeName($this->searchParser->type);
$this->categorize();
$this->format();
}
public function categorize()
{
foreach ($this->searchParser->values as $value) {
if ($value === self::IS_NULL) {
$this->null = true;
continue;
}
if ($value === self::IS_NOT_NULL) {
$this->notNull = true;
continue;
}
if ($this->isNegated($value)) {
$value = $this->replaceNegation($value);
if ($this->hasWildCard($value) || $this->isSingleStringValue()) {
$value = $this->replaceWildCard($value);
$this->notLike[] = $value;
continue;
}
$this->not[] = $value;
continue;
}
if ($this->hasWildCard($value) || $this->isSingleStringValue()) {
$value = $this->replaceWildCard($value);
$this->andLike[] = $value;
continue;
}
$this->and[] = $value;
}
}
/**
* Format categorized values. It must be done after categorizing
* because of micro operators.
*/
public function format()
{
$this->and = $this->type->prepare($this->and);
$this->andLike = $this->type->prepare($this->andLike);
$this->not = $this->type->prepare($this->not);
$this->notLike = $this->type->prepare($this->notLike);
}
protected function isNegated($splitValue): bool
{
return substr($splitValue, 0, 1) === self::NOT;
}
protected function hasWildCard(string $value): bool
{
if (!$value) {
return false;
}
return $value[0] === self::LIKE || $value[strlen($value) - 1] === self::LIKE;
}
protected function replaceWildCard($value)
{
return str_replace(self::LIKE, '%', $value);
}
protected function replaceNegation($value)
{
return preg_replace('~' . self::NOT . '~', '', $value, 1);
}
// Hack so that LIKE operator is used when a single value of string type is passed.
// Not happy with this solution, might need to refactor this later
protected function isSingleStringValue(): bool
{
return count($this->searchParser->values) == 1 && $this->searchParser->type == 'string';
}
}