-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
271 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
/** | ||
* Pimcore | ||
* | ||
* This source file is available under two different licenses: | ||
* - GNU General Public License version 3 (GPLv3) | ||
* - Pimcore Commercial License (PCL) | ||
* Full copyright and license information is available in | ||
* LICENSE.md which is distributed with this source code. | ||
* | ||
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org) | ||
* @license http://www.pimcore.org/license GPLv3 and PCL | ||
*/ | ||
|
||
namespace Pimcore\Bundle\StudioBackendBundle\Exception; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
final class InvalidFilterException extends AbstractApiException | ||
{ | ||
public function __construct(string $filter) | ||
{ | ||
parent::__construct(400, sprintf( | ||
'Invalid filter: %s', | ||
$filter) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
/** | ||
* Pimcore | ||
* | ||
* This source file is available under two different licenses: | ||
* - GNU General Public License version 3 (GPLv3) | ||
* - Pimcore Commercial License (PCL) | ||
* Full copyright and license information is available in | ||
* LICENSE.md which is distributed with this source code. | ||
* | ||
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org) | ||
* @license http://www.pimcore.org/license GPLv3 and PCL | ||
*/ | ||
|
||
namespace Pimcore\Bundle\StudioBackendBundle\Note\Service; | ||
|
||
use Exception; | ||
use Pimcore\Bundle\StaticResolverBundle\Db\DbResolverInterface; | ||
use Pimcore\Bundle\StudioBackendBundle\Exception\InvalidFilterException; | ||
use Pimcore\Bundle\StudioBackendBundle\Note\Request\NoteElement; | ||
use Pimcore\Bundle\StudioBackendBundle\Note\Request\NoteParameters; | ||
use Pimcore\Model\Element\Note\Listing as NoteListing; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
final class FilterService implements FilterServiceInterface | ||
{ | ||
public function applyFilter(NoteListing $list, NoteParameters $parameters): void | ||
{ | ||
if ($parameters->getFilter()) { | ||
$list->addConditionParam( | ||
$this->createFilterCondition($parameters->getFilter()), | ||
['filter' => '%' . $parameters->getFilter() . '%'] | ||
); | ||
} | ||
} | ||
|
||
public function applyFieldFilters(NoteListing $list, NoteParameters $parameters): void | ||
{ | ||
try { | ||
if(!$parameters->getFieldFilters() || empty($parameters->getFieldFilters())) { | ||
Check failure on line 44 in src/Note/Service/FilterService.php GitHub Actions / static-analysis / Static Analysis with PHPStan (8.2, highest, false)
|
||
return; | ||
} | ||
|
||
$propertyKey = 'field'; | ||
|
||
foreach ($parameters->getFieldFilters() as $filter) { | ||
$operator = $this->findOperator($filter['type'], $filter['operator']); | ||
$value = $this->prepareValue($filter['type'], $filter['operator'], $filter['value']); | ||
|
||
if ($operator === 'LIKE') { | ||
$value = '%' . $value . '%'; | ||
} | ||
|
||
if ($filter[$propertyKey] === 'user') { | ||
$list->addConditionParam( | ||
'`user` IN (SELECT `id` FROM `users` WHERE `name` ' . $operator . ' :user)', | ||
['user' => $value] | ||
); | ||
} | ||
|
||
if ($filter['type'] === 'date' && $filter['operator'] === 'eq') { | ||
$maxTime = $value + (86400 - 1); //specifies the top point of the range used in the condition | ||
$dateCondition = '`' . $filter[$propertyKey] . '` ' . ' BETWEEN :minTime AND :maxTime'; | ||
$list->addConditionParam($dateCondition, ['minTime' => $value, 'maxTime' => $maxTime]); | ||
} else { | ||
$list->addConditionParam( | ||
'`' . $filter[$propertyKey] . '` ' . $operator . ' :' . $filter[$propertyKey], | ||
[$filter[$propertyKey] => $value] | ||
); | ||
} | ||
} | ||
} catch (Exception) { | ||
throw new InvalidFilterException('fieldFilters'); | ||
} | ||
} | ||
|
||
public function applyElementFilter(NoteListing $list, NoteElement $noteElement): void | ||
{ | ||
if ($noteElement->getId() && $noteElement->getType()) { | ||
$list->addConditionParam( | ||
'(cid = :id AND ctype = :type)', | ||
['id' => $noteElement->getId(), 'type' => $noteElement->getType()] | ||
); | ||
} | ||
} | ||
|
||
private function prepareValue(string $type, string $operator, string $value): mixed | ||
{ | ||
return match($type) { | ||
'date' => strtotime($value), | ||
default => $this->matchValueOperator($operator, $value) | ||
}; | ||
} | ||
|
||
|
||
private function matchValueOperator(string $operator, mixed $value): mixed | ||
{ | ||
return match($operator) { | ||
'boolean' => (int)$value, | ||
default => $value | ||
}; | ||
} | ||
|
||
private function createFilterCondition(string $filter): string | ||
{ | ||
return '(' | ||
. '`title` LIKE :filter' | ||
. ' OR `description` LIKE :filter' | ||
. ' OR `type` LIKE :filter' | ||
. ' OR `user` IN (SELECT `id` FROM `users` WHERE `name` LIKE :filter)' | ||
. " OR DATE_FORMAT(FROM_UNIXTIME(`date`), '%Y-%m-%d') LIKE :filter" | ||
. ')'; | ||
} | ||
|
||
|
||
private function findOperator(string $type, string $operator): string | ||
{ | ||
return match($type) { | ||
'string' => 'LIKE', | ||
'numeric', 'date' => $this->matchNumericOperator($operator), | ||
default => '=' | ||
}; | ||
} | ||
|
||
private function matchNumericOperator(string $operator): string | ||
{ | ||
return match($operator) { | ||
'lt' => '<', | ||
'lte' => '<=', | ||
'gt' => '>', | ||
'gte' => '>=', | ||
default => '=' | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
/** | ||
* Pimcore | ||
* | ||
* This source file is available under two different licenses: | ||
* - GNU General Public License version 3 (GPLv3) | ||
* - Pimcore Commercial License (PCL) | ||
* Full copyright and license information is available in | ||
* LICENSE.md which is distributed with this source code. | ||
* | ||
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org) | ||
* @license http://www.pimcore.org/license GPLv3 and PCL | ||
*/ | ||
|
||
namespace Pimcore\Bundle\StudioBackendBundle\Note\Service; | ||
|
||
use Pimcore\Bundle\StudioBackendBundle\Note\Request\NoteElement; | ||
use Pimcore\Bundle\StudioBackendBundle\Note\Request\NoteParameters; | ||
use Pimcore\Model\Element\Note\Listing as NoteListing; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
interface FilterServiceInterface | ||
{ | ||
public function applyFilter(NoteListing $list, NoteParameters $parameters): void; | ||
|
||
public function applyFieldFilters(NoteListing $list, NoteParameters $parameters): void; | ||
|
||
public function applyElementFilter(NoteListing $list, NoteElement $noteElement): void; | ||
} |
35 changes: 35 additions & 0 deletions
35
src/OpenApi/Attributes/Parameters/Query/FieldFilterParameter.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
/** | ||
* Pimcore | ||
* | ||
* This source file is available under two different licenses: | ||
* - GNU General Public License version 3 (GPLv3) | ||
* - Pimcore Commercial License (PCL) | ||
* Full copyright and license information is available in | ||
* LICENSE.md which is distributed with this source code. | ||
* | ||
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org) | ||
* @license http://www.pimcore.org/license GPLv3 and PCL | ||
*/ | ||
|
||
namespace Pimcore\Bundle\StudioBackendBundle\OpenApi\Attributes\Parameters\Query; | ||
|
||
use Attribute; | ||
use OpenApi\Attributes\QueryParameter as OpenApiQueryParameter; | ||
|
||
#[Attribute(Attribute::TARGET_METHOD)] | ||
final class FieldFilterParameter extends OpenApiQueryParameter | ||
{ | ||
public function __construct() | ||
{ | ||
parent::__construct( | ||
name: 'fieldFilters', | ||
description: 'Filter for specific fields, will be json decoded to an array.', | ||
in: 'query', | ||
required: false, | ||
example: '[{"operator":"like","value":"John","field":"name","type":"string"}, {"operator":"eq","value":"10","property":"count","type":"numeric"} ]' | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters