SearchFilterParser::applyFilter()   A
last analyzed

Complexity

Conditions 5
Paths 4

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 5

Importance

Changes 0
Metric Value
cc 5
eloc 7
nc 4
nop 2
dl 0
loc 15
ccs 8
cts 8
cp 1
crap 5
rs 9.6111
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 SearchFilterParser extends FilterParserAbstract
7
{
8
    const PARAM_PREFIX = '';
9
10
    const SEARCH_KEY = 'search';
11
12
    /**
13
     * @var string|bool
14
     */
15
    protected $property;
16
17
    /**
18
     * @var string
19
     */
20
    protected $searchKey;
21
22
    /**
23
     * StringParser constructor.
24
     *
25
     * @param RestRequestContract $request
26
     * @param string              $property Property name that will be filtered by query.
27
     * @param string              $searchKey
28
     */
29 14
    public function __construct(RestRequestContract $request, $property, $searchKey = self::SEARCH_KEY)
30
    {
31 14
        parent::__construct($request);
32 14
        $this->property = $property;
33 14
        $this->searchKey = $searchKey;
34 14
    }
35
36
    /**
37
     * Assign LIKE operator on property if query is string.
38
     *
39
     * @param Criteria $criteria
40
     * @param          $filter
41
     *
42
     * @return Criteria
43
     */
44 14
    public function applyFilter(Criteria $criteria, $filter)
45
    {
46 14
        if (is_string($filter) && is_string($this->property)) {
47 2
            $criteria->andWhere(
48 2
                $criteria->expr()->contains($this->property, $filter)
49
            );
50
        }
51
52 14
        if (is_array($filter) && isset($filter[$this->searchKey])) {
53 1
            $criteria->andWhere(
54 1
                $criteria->expr()->contains($this->property, $filter[$this->searchKey])
55
            );
56
        }
57
58 14
        return $criteria;
59
    }
60
}
61