Completed
Push — master ( cec9f3...d27170 )
by Kamil
22:53
created

StringFilter   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 3
Bugs 1 Features 0
Metric Value
wmc 16
c 3
b 1
f 0
lcom 0
cbo 2
dl 0
loc 83
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
B apply() 0 27 6
D getExpression() 0 32 10
1
<?php
2
3
/*
4
 * This file is part of the Sylius package.
5
 *
6
 * (c) Paweł Jędrzejewski
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sylius\Component\Grid\Filter;
13
14
use Sylius\Component\Grid\Data\DataSourceInterface;
15
use Sylius\Component\Grid\Data\ExpressionBuilderInterface;
16
use Sylius\Component\Grid\Filtering\FilterInterface;
17
18
/**
19
 * @author Paweł Jędrzejewski <[email protected]>
20
 */
21
class StringFilter implements FilterInterface
22
{
23
    const NAME = 'string';
24
25
    const TYPE_EQUAL = 'equal';
26
    const TYPE_EMPTY = 'empty';
27
    const TYPE_NOT_EMPTY = 'not_empty';
28
    const TYPE_CONTAINS = 'contains';
29
    const TYPE_NOT_CONTAINS = 'not_contains';
30
    const TYPE_STARTS_WITH = 'starts_with';
31
    const TYPE_ENDS_WITH = 'ends_with';
32
    const TYPE_IN = 'in';
33
    const TYPE_NOT_IN = 'not_in';
34
35
    /**
36
     * {@inheritdoc}
37
     */
38
    public function apply(DataSourceInterface $dataSource, $name, $data, array $options)
39
    {
40
        $expressionBuilder = $dataSource->getExpressionBuilder();
41
42
        if (!is_array($data)) {
43
            $data = ['type' => self::TYPE_CONTAINS, 'value' => $data];
44
        }
45
46
        $fields = array_key_exists('fields', $options) ? $options['fields'] : [$name];
47
48
        $type = $data['type'];
49
        $value = array_key_exists('value', $data) ? $data['value'] : null;
50
51
        if (1 === count($fields)) {
52
            $expression = $this->getExpression($expressionBuilder, $type, $name, $value);
53
        } else {
54
            $expressions = [];
55
56
            foreach ($fields as $field) {
57
                $expressions[] = $this->getExpression($expressionBuilder, $type, $field, $value);
58
            }
59
60
            $expression = $expressionBuilder->orX($expressions);
61
        }
62
63
        $dataSource->restrict($expression);
64
    }
65
66
    /**
67
     * @param string $type
68
     * @param string $field
69
     * @param mixed  $value
70
     */
71
    private function getExpression(ExpressionBuilderInterface $expressionBuilder, $type, $field, $value)
72
    {
73
        switch ($type) {
74
            case self::TYPE_EQUAL:
75
                return $expressionBuilder->equals($field, $value);
76
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
77
            case self::TYPE_EMPTY:
78
                return $expressionBuilder->isNull($field);
79
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
80
            case self::TYPE_NOT_EMPTY:
81
                return $expressionBuilder->isNotNull($field);
82
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
83
            case self::TYPE_CONTAINS:
84
                return $expressionBuilder->like($field, '%'.$value.'%');
85
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
86
            case self::TYPE_NOT_CONTAINS:
87
                return $expressionBuilder->notLike($field, '%'.$value.'%');
88
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
89
            case self::TYPE_STARTS_WITH:
90
                return $expressionBuilder->like($field, $value.'%');
91
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
92
            case self::TYPE_ENDS_WITH:
93
                return $expressionBuilder->like($field, '%'.$value);
94
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
95
            case self::TYPE_IN:
96
                return $expressionBuilder->in($field, array_map('trim', explode(',', $value)));
97
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
98
            case self::TYPE_NOT_IN:
99
                return $expressionBuilder->notIn($field, array_map('trim', explode(',', $value)));
100
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
101
        }
102
    }
103
}
104