Skip to content

Commit 0ded03d

Browse files
NorgulStyleCIBot
andauthored
Fix dates (#41)
* Laravel 9 PHP 8 upgrade #major * Fix for dates * Apply fixes from StyleCI Co-authored-by: StyleCI Bot <bot@styleci.io>
1 parent d6390ae commit 0ded03d

File tree

4 files changed

+45
-9
lines changed

4 files changed

+45
-9
lines changed

src/RequestParameters/SearchParameter.php

+1-3
Original file line numberDiff line numberDiff line change
@@ -190,9 +190,7 @@ protected function appendSingle(Builder $builder, OperatorsConfig $operatorsConf
190190
{
191191
$callbackClassName = $operatorsConfig->getCallbackClassFromOperator($searchParser->operator);
192192

193-
/**
194-
* @var AbstractCallback $callback
195-
*/
193+
/** @var AbstractCallback $callback */
196194
new $callbackClassName($builder, $searchParser);
197195
}
198196
}

src/SearchCallbacks/AbstractCallback.php

+11-5
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ abstract class AbstractCallback
1616
protected SearchParser $searchParser;
1717
protected CategorizedValues $categorizedValues;
1818

19+
protected const DATE_FIELDS = [
20+
'date', 'datetime',
21+
];
22+
1923
/**
2024
* AbstractCallback constructor.
2125
*
@@ -105,7 +109,8 @@ protected function lessOrMoreCallback(Builder $builder, string $column, Categori
105109
throw new JsonQueryBuilderException("No valid arguments for '$operator' operator.");
106110
}
107111

108-
$builder->where($column, $operator, $values->and[0]);
112+
$method = $this->isDate($this->searchParser->type) ? 'whereDate' : 'where';
113+
$builder->{$method}($column, $operator, $values->and[0]);
109114
}
110115

111116
/**
@@ -124,10 +129,6 @@ protected function betweenCallback(Builder $builder, string $column, Categorized
124129
throw new JsonQueryBuilderException("Using $operator operator assumes exactly 2 parameters. Wrong number of parameters provided.");
125130
}
126131

127-
if (!count($values->and)) {
128-
throw new JsonQueryBuilderException("No valid arguments for '$operator' operator.");
129-
}
130-
131132
$callback = $operator == '<>' ? 'whereBetween' : 'whereNotBetween';
132133

133134
$builder->{$callback}($column, [$values->and[0], $values->and[1]]);
@@ -147,4 +148,9 @@ protected function checkAllowedValues(CategorizedValues $values, string $operato
147148
throw new JsonQueryBuilderException("Wrong parameter type(s) for '$operator' operator.");
148149
}
149150
}
151+
152+
protected function isDate(string $type): bool
153+
{
154+
return in_array($type, self::DATE_FIELDS);
155+
}
150156
}

src/SearchCallbacks/Equals.php

+20-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Asseco\JsonQueryBuilder\SearchCallbacks;
66

77
use Asseco\JsonQueryBuilder\CategorizedValues;
8+
use Exception;
89
use Illuminate\Database\Eloquent\Builder;
910

1011
class Equals extends AbstractCallback
@@ -14,6 +15,14 @@ public static function operator(): string
1415
return '=';
1516
}
1617

18+
/**
19+
* @param Builder $builder
20+
* @param string $column
21+
* @param CategorizedValues $values
22+
* @return void
23+
*
24+
* @throws Exception
25+
*/
1726
public function execute(Builder $builder, string $column, CategorizedValues $values): void
1827
{
1928
foreach ($values->andLike as $andLike) {
@@ -33,10 +42,20 @@ public function execute(Builder $builder, string $column, CategorizedValues $val
3342
}
3443

3544
if ($values->and) {
36-
$builder->whereIn($column, $values->and);
45+
if ($this->isDate($this->searchParser->type)) {
46+
foreach ($values->and as $andValue) {
47+
$builder->orWhereDate($column, $andValue);
48+
}
49+
} else {
50+
$builder->whereIn($column, $values->and);
51+
}
3752
}
3853

3954
if ($values->not) {
55+
if ($this->isDate($this->searchParser->type)) {
56+
throw new Exception('Not operator is not supported for date(time) fields');
57+
}
58+
4059
$builder->whereNotIn($column, $values->not);
4160
}
4261
}

src/SearchCallbacks/NotEquals.php

+13
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Asseco\JsonQueryBuilder\SearchCallbacks;
66

77
use Asseco\JsonQueryBuilder\CategorizedValues;
8+
use Exception;
89
use Illuminate\Database\Eloquent\Builder;
910

1011
class NotEquals extends AbstractCallback
@@ -14,9 +15,21 @@ public static function operator(): string
1415
return '!=';
1516
}
1617

18+
/**
19+
* @param Builder $builder
20+
* @param string $column
21+
* @param CategorizedValues $values
22+
* @return void
23+
*
24+
* @throws Exception
25+
*/
1726
public function execute(Builder $builder, string $column, CategorizedValues $values): void
1827
{
1928
foreach (array_merge($values->andLike, $values->notLike) as $like) {
29+
if ($this->isDate($this->searchParser->type)) {
30+
throw new Exception('Not operator is not supported for date(time) fields');
31+
}
32+
2033
$builder->where($column, 'NOT LIKE', $like);
2134
}
2235

0 commit comments

Comments
 (0)