Passed
Push — feature/optimize ( cf938e...7e8046 )
by Fu
03:43
created

ParseSearchableTrait::parseSearchable()   A

Complexity

Conditions 5
Paths 3

Size

Total Lines 19
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 13
nc 3
nop 0
dl 0
loc 19
rs 9.5222
c 0
b 0
f 0
1
<?php
2
3
4
namespace Modules\Core\Traits\Criteria;
5
6
trait ParseSearchableTrait
7
{
8
    /** @var \Illuminate\Http\Request $request */
9
    protected $request;
10
    /** @var \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model|\Illuminate\Database\Query\Builder $model */
11
    protected $model;
12
    /** @var \Prettus\Repository\Contracts\RepositoryInterface $repository */
13
    protected $repository;
14
    protected $search;
15
    protected $searchData;
16
    protected $searchFields;
17
    protected $isFirstField;
18
    protected $modelForceAndWhere;
19
    protected $fieldsSearchable;
20
    protected $fields;
21
    protected $filter;
22
    protected $orderBy;
23
    protected $sortedBy;
24
    protected $with;
25
    protected $searchJoin;
26
    protected $acceptedConditions;
27
    protected $originalFields;
28
29
    use ParseFieldsSearchTrait;
30
    use ParseSearchWhereTrait;
31
    use ParseSearchDataTrait;
32
    use ParseSearchTrait;
33
    use ParseForceAndWhereTrait;
34
    use ParseValueTrait;
35
36
    /**
37
     * @throws \Exception
38
     */
39
    protected function parseSearchable()
40
    {
41
        $this->fieldsSearchable = $this->repository->getFieldsSearchable();
42
        $this->search = $this->request->get(config('repository.criteria.params.search', 'search'), null);
43
44
        if ($this->search && is_array($this->fieldsSearchable) && count($this->fieldsSearchable)) {
45
46
            $this->parseFieldsSearch();
47
            $this->parseSearchData();
48
            $this->parseSearch();
49
            $this->parseForceAndWhere();
50
51
            foreach ($this->fields as $field => $condition) {
52
53
                $this->getConditionField($field, $condition);
54
                $value = $this->parseValue($condition, $field);
55
                $relation = null;
56
                $this->getRelationField($field, $relation);
57
                $this->parseSearchWhere($value, $relation, $field, $condition);
58
            }
59
        }
60
    }
61
62
    protected function getConditionField(&$field, &$condition)
63
    {
64
        if (is_numeric($field)) {
65
            $field = $condition;
66
            $condition = "=";
67
        }
68
    }
69
70
    protected function getRelationField(&$field, &$relation)
71
    {
72
        if (stripos($field, '.')) {
73
            $explode = explode('.', $field);
74
            $field = array_pop($explode);
75
            $relation = implode('.', $explode);
76
        }
77
    }
78
}