Skip to content

Commit

Permalink
Date Filters
Browse files Browse the repository at this point in the history
GT_DATE
GTE_DATE
LT_DATE
LTE_DATE
EQUALS_DATE
  • Loading branch information
Mauro Cassani committed Oct 17, 2017
1 parent ed219d1 commit 1774329
Show file tree
Hide file tree
Showing 21 changed files with 293 additions and 16 deletions.
28 changes: 26 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,11 @@ foreach ($qb->getResults() as $element){
* `<=`
* `>=`
* `!=`
* `GT_DATE`
* `GTE_DATE`
* `LT_DATE`
* `LTE_DATE`
* `EQUALS_DATE`
* `IN_ARRAY`
* `IN_ARRAY_INVERSED`
* `ARRAY_MATCH`
Expand All @@ -106,9 +111,9 @@ foreach ($qb->getResults() as $element){
* `ASC` (default operator, can be omitted)
* `DESC`

## Limit and Offset
## Performing Queries

You can specify limit and offset for your query results:
You can add criteria and specify limit and offset for your query results:

```php
use ArrayQuery\QueryBuilder;
Expand All @@ -125,6 +130,25 @@ foreach ($qb->getResults() as $element){
}
```

## Working with dates

You can perform queries based on datetime fields. You must specify **date format**:

```php
use ArrayQuery\QueryBuilder;

$qb = QueryBuilder::create($array);
$qb
->addCriterion('registration_date', '01/05/2017', 'GT_DATE', 'd/m/Y')
->addCriterion('rate', '3', '>')
->sortedBy('title')
->limit(0, 10);

foreach ($qb->getResults() as $element){
// ...
}
```

## Support

If you found an issue or had an idea please refer [to this section](https://github.com/mauretto78/array-query/issues).
Expand Down
2 changes: 1 addition & 1 deletion src/Filters/Criterion/ArrayMatchFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class ArrayMatchFilter implements FilterInterface
* @param $valueToCompare
* @return bool
*/
public function match($value, $valueToCompare)
public function match($value, $valueToCompare, $dateFormat = null)
{
return count(array_intersect($value, $valueToCompare)) ? true : false;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Filters/Criterion/ContainsFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class ContainsFilter implements FilterInterface
* @param $valueToCompare
* @return bool
*/
public function match($value, $valueToCompare)
public function match($value, $valueToCompare, $dateFormat = null)
{
return stripos($value, $valueToCompare) !== false;
}
Expand Down
27 changes: 27 additions & 0 deletions src/Filters/Criterion/EqualsDateFilter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php
/**
* This file is part of the ArrayQuery package.
*
* (c) Mauro Cassani<https://github.com/mauretto78>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace ArrayQuery\Filters\Criterion;

class EqualsDateFilter implements FilterInterface
{
/**
* @param $value
* @param $valueToCompare
* @return bool
*/
public function match($value, $valueToCompare, $dateFormat = null)
{
$valueDate = \DateTimeImmutable::createFromFormat(($dateFormat) ?: 'Y-m-d', $value);
$valueToComapareDate = \DateTimeImmutable::createFromFormat(($dateFormat) ?: 'Y-m-d', $valueToCompare);

return $valueDate == $valueToComapareDate;
}
}
2 changes: 1 addition & 1 deletion src/Filters/Criterion/EqualsFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class EqualsFilter implements FilterInterface
* @param $valueToCompare
* @return bool
*/
public function match($value, $valueToCompare)
public function match($value, $valueToCompare, $dateFormat = null)
{
return $value === $valueToCompare;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Filters/Criterion/FilterInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@ interface FilterInterface
* @param $valueToCompare
* @return mixed
*/
public function match($value, $valueToCompare);
public function match($value, $valueToCompare, $dateFormat = null);
}
27 changes: 27 additions & 0 deletions src/Filters/Criterion/GreaterThanDateFilter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php
/**
* This file is part of the ArrayQuery package.
*
* (c) Mauro Cassani<https://github.com/mauretto78>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace ArrayQuery\Filters\Criterion;

class GreaterThanDateFilter implements FilterInterface
{
/**
* @param $value
* @param $valueToCompare
* @return bool
*/
public function match($value, $valueToCompare, $dateFormat = null)
{
$valueDate = \DateTimeImmutable::createFromFormat(($dateFormat) ?: 'Y-m-d', $value);
$valueToComapareDate = \DateTimeImmutable::createFromFormat(($dateFormat) ?: 'Y-m-d', $valueToCompare);

return $valueDate > $valueToComapareDate;
}
}
27 changes: 27 additions & 0 deletions src/Filters/Criterion/GreaterThanEqualsDateFilter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php
/**
* This file is part of the ArrayQuery package.
*
* (c) Mauro Cassani<https://github.com/mauretto78>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace ArrayQuery\Filters\Criterion;

class GreaterThanEqualsDateFilter implements FilterInterface
{
/**
* @param $value
* @param $valueToCompare
* @return bool
*/
public function match($value, $valueToCompare, $dateFormat = null)
{
$valueDate = \DateTimeImmutable::createFromFormat(($dateFormat) ?: 'Y-m-d', $value);
$valueToComapareDate = \DateTimeImmutable::createFromFormat(($dateFormat) ?: 'Y-m-d', $valueToCompare);

return $valueDate >= $valueToComapareDate;
}
}
2 changes: 1 addition & 1 deletion src/Filters/Criterion/GreaterThanEqualsFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class GreaterThanEqualsFilter implements FilterInterface
* @param $valueToCompare
* @return bool
*/
public function match($value, $valueToCompare)
public function match($value, $valueToCompare, $dateFormat = null)
{
return $value >= $valueToCompare;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Filters/Criterion/GreaterThanFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class GreaterThanFilter implements FilterInterface
* @param $valueToCompare
* @return bool
*/
public function match($value, $valueToCompare)
public function match($value, $valueToCompare, $dateFormat = null)
{
return $value > $valueToCompare;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Filters/Criterion/InArrayFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class InArrayFilter implements FilterInterface
* @param $valueToCompare
* @return bool
*/
public function match($value, $valueToCompare)
public function match($value, $valueToCompare, $dateFormat = null)
{
return in_array($value, (array) $valueToCompare);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Filters/Criterion/InArrayInversedFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class InArrayInversedFilter implements FilterInterface
* @param $valueToCompare
* @return bool
*/
public function match($value, $valueToCompare)
public function match($value, $valueToCompare, $dateFormat = null)
{
return in_array($valueToCompare, (array) $value);
}
Expand Down
27 changes: 27 additions & 0 deletions src/Filters/Criterion/LessThanDateFilter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php
/**
* This file is part of the ArrayQuery package.
*
* (c) Mauro Cassani<https://github.com/mauretto78>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace ArrayQuery\Filters\Criterion;

class LessThanDateFilter implements FilterInterface
{
/**
* @param $value
* @param $valueToCompare
* @return bool
*/
public function match($value, $valueToCompare, $dateFormat = null)
{
$valueDate = \DateTimeImmutable::createFromFormat(($dateFormat) ?: 'Y-m-d', $value);
$valueToComapareDate = \DateTimeImmutable::createFromFormat(($dateFormat) ?: 'Y-m-d', $valueToCompare);

return $valueDate < $valueToComapareDate;
}
}
27 changes: 27 additions & 0 deletions src/Filters/Criterion/LessThanEqualsDateFilter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php
/**
* This file is part of the ArrayQuery package.
*
* (c) Mauro Cassani<https://github.com/mauretto78>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace ArrayQuery\Filters\Criterion;

class LessThanEqualsDateFilter implements FilterInterface
{
/**
* @param $value
* @param $valueToCompare
* @return bool
*/
public function match($value, $valueToCompare, $dateFormat = null)
{
$valueDate = \DateTimeImmutable::createFromFormat(($dateFormat) ?: 'Y-m-d', $value);
$valueToComapareDate = \DateTimeImmutable::createFromFormat(($dateFormat) ?: 'Y-m-d', $valueToCompare);

return $valueDate <= $valueToComapareDate;
}
}
2 changes: 1 addition & 1 deletion src/Filters/Criterion/LessThanEqualsFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class LessThanEqualsFilter implements FilterInterface
* @param $valueToCompare
* @return bool
*/
public function match($value, $valueToCompare)
public function match($value, $valueToCompare, $dateFormat = null)
{
return $value <= $valueToCompare;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Filters/Criterion/LessThanFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class LessThanFilter implements FilterInterface
* @param $valueToCompare
* @return bool
*/
public function match($value, $valueToCompare)
public function match($value, $valueToCompare, $dateFormat = null)
{
return $value < $valueToCompare;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Filters/Criterion/NotEqualsFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class NotEqualsFilter implements FilterInterface
* @param $valueToCompare
* @return bool
*/
public function match($value, $valueToCompare)
public function match($value, $valueToCompare, $dateFormat = null)
{
return $value !== $valueToCompare;
}
Expand Down
7 changes: 6 additions & 1 deletion src/Filters/CriterionFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ class CriterionFilter extends AbstractFilter
'<' => 'LessThanFilter',
'<=' => 'LessThanEqualsFilter',
'!=' => 'NotEqualsFilter',
'GT_DATE' => 'GreaterThanDateFilter',
'GTE_DATE' => 'GreaterThanEqualsDateFilter',
'LT_DATE' => 'LessThanDateFilter',
'LTE_DATE' => 'LessThanEqualsDateFilter',
'EQUALS_DATE' => 'EqualsDateFilter',
'IN_ARRAY' => 'InArrayFilter',
'IN_ARRAY_INVERSED' => 'InArrayInversedFilter',
'ARRAY_MATCH' => 'ArrayMatchFilter',
Expand All @@ -44,6 +49,6 @@ public static function filter($criterion, $element)
/** @var FilterInterface $filter */
$filter = new $filterClass();

return $filter->match($value, $valueToCompare);
return $filter->match($value, $valueToCompare, $criterion['date_format']);
}
}
7 changes: 5 additions & 2 deletions src/QueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,11 @@ private function setArray(array $array)
* @param $key
* @param $value
* @param string $operator
* @param null $dateFormat
* @return $this
* @throws NotValidCriterionOperatorException
*/
public function addCriterion($key, $value, $operator = '=')
public function addCriterion($key, $value, $operator = '=', $dateFormat = null)
{
if (!$this->isAValidCriterionOperator($operator)) {
throw new NotValidCriterionOperatorException($operator.' is not a valid operator.');
Expand All @@ -91,6 +92,7 @@ public function addCriterion($key, $value, $operator = '=')
'key' => $key,
'value' => $value,
'operator' => $operator,
'date_format' => $dateFormat
];

return $this;
Expand Down Expand Up @@ -209,7 +211,8 @@ private function applyCriteriaFilter()
$results = array_filter(
(isset($results)) ? $results : $this->array, function ($element) use ($criterion) {
return CriterionFilter::filter($criterion, $element);
});
}
);
}

return $results;
Expand Down
Loading

0 comments on commit 1774329

Please sign in to comment.