Passed
Push — feature/optimize ( 0e0305...fdd242 )
by Fu
03:04
created

ParserSearchableTrait::getRelationField()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 2
dl 0
loc 6
rs 10
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
 * @property $search
12
 * @property $searchData
13
 * @property $searchFields
14
 * @property $isFirstField = true
15
 * @property $modelForceAndWhere
16
 * @property $fieldsSearchable
17
 * @property $fields
18
 * @property $filter
19
 * @property $orderBy
20
 * @property $sortedBy
21
 * @property $with
22
 * @property $searchJoin
23
 * @property $acceptedConditions
24
 * @property $originalFields
25
 * @package Modules\Core\Traits\Criteria
26
 */
0 ignored issues
show
Documentation Bug introduced by
The doc comment $isFirstField at position 0 could not be parsed: Unknown type name '$isFirstField' at position 0 in $isFirstField.
Loading history...
27
trait ParserSearchableTrait
28
{
29
    use ParserFieldsSearchTrait;
30
    use ParserSearchWhereTrait;
31
    use ParserSearchDataTrait;
32
    use ParserSearchTrait;
33
    use ParserForceAndWhereTrait;
34
    use ParserValueTrait;
35
36
    /**
37
     * @throws \Exception
38
     */
39
    protected function parserSearchable()
40
    {
41
        $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...
42
        $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...
43
44
        if ($this->search && is_array($this->fieldsSearchable) && count($this->fieldsSearchable)) {
45
46
            $this->parserFieldsSearch();
47
            $this->parserSearchData();
48
            $this->parserSearch();
49
            $this->parserForceAndWhere();
50
51
            foreach ($this->fields as $field => $condition) {
52
53
                $this->getConditionField($field, $condition);
54
                $value = $this->parserValue($condition, $field);
55
                $relation = null;
56
                $this->getRelationField($field, $relation);
57
                $this->parserSearchWhere($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
}