Passed
Push — master ( dc3688...f2f34e )
by Pavel
02:13
created

StringFilterParser::applyFilter()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 4
nc 2
nop 2
dl 0
loc 9
ccs 5
cts 5
cp 1
crap 3
rs 9.6666
c 0
b 0
f 0
1
<?php namespace Pz\Doctrine\Rest\QueryParser;
2
3
use Doctrine\Common\Collections\Criteria;
4
use Pz\Doctrine\Rest\Contracts\RestRequestContract;
5
6
class StringFilterParser extends FilterParserAbstract
7
{
8
    const PARAM_PREFIX = '';
9
10
    /**
11
     * @var string|bool
12
     */
13
    protected $property;
14
15
    /**
16
     * StringParser constructor.
17
     *
18
     * @param RestRequestContract $request
19
     * @param string              $property Property name that will be filtered by query.
20
     */
21 8
    public function __construct(RestRequestContract $request, $property)
22
    {
23 8
        parent::__construct($request);
24 8
        $this->property = $property;
25 8
    }
26
27
    /**
28
     * Assign LIKE operator on property if query is string.
29
     *
30
     * @param Criteria $criteria
31
     * @param          $filter
32
     *
33
     * @return Criteria
34
     */
35 8
    public function applyFilter(Criteria $criteria, $filter)
36
    {
37 8
        if (is_string($filter) && is_string($this->property)) {
38 2
            $criteria->andWhere(
39 2
                $criteria->expr()->contains($this->property, $filter)
40
            );
41
        }
42
43 8
        return $criteria;
44
    }
45
}
46