/
RelationFilterConditionParser.php
47 lines (45 loc) · 1.42 KB
/
RelationFilterConditionParser.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
<?php
/**
* @category pimcore
* @date 25/06/2021
* @author Filip Szenborn <fszenborn@divante.co>
*/
declare(strict_types=1);
namespace Pimcore\Model\DataObject\ClassDefinition\Data\Extension;
/**
* Trait RelationFilterConditionParser
* @package Pimcore\Model\DataObject\ClassDefinition\Data\Extension
*/
trait RelationFilterConditionParser
{
/**
* Parses filter value of a relation field and creates the filter condition
* @param $value
* @param $operator
* @param $name
* @return string
*/
public function getRelationFilterCondition($value, $operator, $name)
{
if ($operator == '=') {
$value = "'%," . $value . ",%'";
return '`' . $name . '` LIKE ' . $value . ' ';
} elseif ($operator == 'LIKE') {
$result = $name . " IS NULL";
$values = explode(',', (string)$value ?? '');
if (is_array($values) && !empty($values)) {
$fieldConditions = [];
foreach ($values as $value) {
if (empty($value)) {
continue;
}
$fieldConditions[] = '`' . $name . "` LIKE '%" . $value . "%' ";
}
if (!empty($fieldConditions)) {
$result = '(' . implode(' AND ', $fieldConditions) . ')';
}
}
return $result;
}
}
}