SearchFilterParser   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 15
dl 0
loc 53
ccs 13
cts 13
cp 1
rs 10
c 0
b 0
f 0
wmc 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A applyFilter() 0 15 5
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