FilterHandler::apply()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 11
cts 11
cp 1
rs 9.4285
c 0
b 0
f 0
nc 3
cc 3
eloc 8
nop 1
crap 3
1
<?php
2
3
namespace Nayjest\Querying\Handler\PostProcess;
4
5
use InvalidArgumentException;
6
use Nayjest\Querying\Handler\AbstractHandler;
7
use Nayjest\Querying\Handler\Priority;
8
use Nayjest\Querying\Operation\FilterOperation;
9
use Nayjest\Querying\Row\RowInterface;
10
11
/**
12
 * Class FilterHandler
13
 */
14
class FilterHandler extends AbstractHandler
15
{
16
    const PRIORITY = Priority::POST_PROCESS;
17
18 1
    public function getPriority()
19
    {
20 1
        return self::PRIORITY;
21
    }
22
    /**
23
     * Applies operation to source and returns modified source.
24
     *
25
     */
26 1
    public function apply($dataSource)
27
    {
28
        /** @var FilterOperation $operation */
29 1
        $operation = $this->operation;
30 1
        foreach ($dataSource as $row) {
31
            /** @var RowInterface $row */
32 1
            $testedValue = $row->get($operation->getField());
33 1
            $argument = $operation->getValue();
34 1
            $operator = $operation->getOperator();
35 1
            if ($this->checkValue($testedValue, $operator, $argument)) {
36 1
                yield $row;
37 1
            }
38 1
        }
39 1
    }
40
41
    /**
42
     * @param $testedValue
43
     * @param string $operator
44
     * @param $argument
45
     * @return bool
46
     * @throws InvalidArgumentException
47
     *
48
     */
49 1
    protected function checkValue($testedValue, $operator, $argument)
50
    {
51
        switch ($operator) {
52 1
            case FilterOperation::OPERATOR_EQ:
53
                return $testedValue == $argument;
54 1
            case FilterOperation::OPERATOR_GT:
55
                return $testedValue > $argument;
56 1
            case FilterOperation::OPERATOR_GTE:
57
                return $testedValue >= $argument;
58 1
            case FilterOperation::OPERATOR_LT:
59
                return $testedValue < $argument;
60 1
            case FilterOperation::OPERATOR_LTE:
61 1
                return $testedValue <= $argument;
62 1
            case FilterOperation::OPERATOR_NOT_EQ:
63 1
                return $testedValue != $argument;
64
            case FilterOperation::OPERATOR_STR_CONTAINS:
65
                return strpos($testedValue, $argument) !== false;
66
            case FilterOperation::OPERATOR_STR_STARTS_WITH:
67
                return strpos($testedValue, $argument) === 0;
68
            case FilterOperation::OPERATOR_STR_ENDS_WITH:
69
                return strpos($testedValue, $argument, strlen($testedValue) - strlen($argument)) !== false;
70
            default:
71
                throw new InvalidArgumentException(
72
                    'Unsupported operator ' . $operator
73
                );
74
        }
75
    }
76
}
77