Passed
Push — feature/optimize ( 219eef...0e0305 )
by Fu
03:34
created

ParserSearchableTrait::parserSearchable()   B

Complexity

Conditions 7
Paths 6

Size

Total Lines 30
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 18
nc 6
nop 0
dl 0
loc 30
rs 8.8333
c 0
b 0
f 0
1
<?php
2
3
4
namespace Modules\Core\Traits\Criteria;
5
6
/**
7
 * Trait ParserSearchableTrait
8
 *
9
 * @property \Illuminate\Http\Request $request
10
 * @property \Prettus\Repository\Contracts\RepositoryInterface $repository
11
 * @package Modules\Core\Traits\Criteria
12
 */
13
trait ParserSearchableTrait
14
{
15
    use ParserFieldsSearchTrait;
16
    use ParserSearchWhereTrait;
17
    use ParserSearchFieldsTrait;
18
    use ParserSearchDataTrait;
19
    use ParserSearchTrait;
20
    use ParserForceAndWhereTrait;
21
    use ParserValueTrait;
22
23
    /**
24
     * @throws \Exception
25
     */
26
    protected function parserSearchable()
27
    {
28
        $this->fieldsSearchable = $this->repository->getFieldsSearchable();
0 ignored issues
show
Bug Best Practice introduced by
The property fieldsSearchable does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
29
        $this->search = $this->request->get(config('repository.criteria.params.search', 'search'), null);
0 ignored issues
show
Bug Best Practice introduced by
The property search does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
30
31
        if ($this->search && is_array($this->fieldsSearchable) && count($this->fieldsSearchable)) {
32
33
            $this->parserFieldsSearch();
34
            $this->parserSearchData();
35
            $this->parserSearch();
36
            $this->parserForceAndWhere();
37
38
            foreach ($this->fields as $field => $condition) {
39
40
                if (is_numeric($field)) {
41
                    $field = $condition;
42
                    $condition = "=";
43
                }
44
45
                $value = $this->parserValue($condition, $field);
46
47
                $relation = null;
48
49
                if (stripos($field, '.')) {
50
                    $explode = explode('.', $field);
51
                    $field = array_pop($explode);
52
                    $relation = implode('.', $explode);
53
                }
54
55
                $this->parserSearchWhere($value, $relation, $field, $condition);
56
            }
57
        }
58
    }
59
}