Passed
Push — master ( bd9b88...11a7f9 )
by Fu
08:59 queued 03:53
created

ParseFieldsSearchTrait::parseFieldsSearch()   A

Complexity

Conditions 6
Paths 2

Size

Total Lines 26
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 13
nc 2
nop 0
dl 0
loc 26
rs 9.2222
c 0
b 0
f 0
1
<?php
2
3
4
namespace Modules\Core\Traits\Criteria;
5
6
7
use Exception;
8
9
trait ParseFieldsSearchTrait
10
{
11
    /** @var \Illuminate\Http\Request $request */
12
    protected $request;
13
    /** @var \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model|\Illuminate\Database\Query\Builder $model */
14
    protected $model;
15
    /** @var \Prettus\Repository\Contracts\RepositoryInterface $repository */
16
    protected $repository;
17
    protected $search;
18
    protected $searchData;
19
    protected $searchFields;
20
    protected $isFirstField;
21
    protected $modelForceAndWhere;
22
    protected $fieldsSearchable;
23
    protected $fields;
24
    protected $filter;
25
    protected $orderBy;
26
    protected $sortedBy;
27
    protected $with;
28
    protected $searchJoin;
29
    protected $acceptedConditions;
30
    protected $originalFields;
31
32
    use ParseSearchFieldsTrait;
33
34
    /**
35
     * @throws \Exception
36
     */
37
    protected function parseFieldsSearch()
38
    {
39
        $this->parseSearchFields();
40
41
        $fields = $this->fieldsSearchable;
42
43
        if (!is_null($this->searchFields) && is_array($this->searchFields)) {
44
45
            $this->parseOriginalFields();
46
47
            $fields = [];
48
49
            foreach ($this->originalFields as $field => $condition) {
50
                if (is_numeric($field)) {
51
                    $field = $condition;
52
                    $condition = "=";
53
                }
54
                if (in_array($field, (array) $this->searchFields)) {
55
                    $fields[$field] = $condition;
56
                }
57
            }
58
59
            $this->assertFieldsNotAccepted($fields);
60
        }
61
62
        $this->fields = $fields;
63
    }
64
65
    protected function parseOriginalFields()
66
    {
67
        $this->acceptedConditions = config('repository.criteria.acceptedConditions', ['=', 'like']);
68
69
        $this->originalFields = $this->fieldsSearchable;
70
71
        foreach ($this->searchFields as $index => $field) {
72
            $field_parts = explode(':', $field);
73
            $temporaryIndex = array_search($field_parts[0], $this->originalFields);
74
75
            if (count($field_parts) == 2) {
76
                if (in_array($field_parts[1], $this->acceptedConditions)) {
77
                    unset($this->originalFields[$temporaryIndex]);
78
                    $field = $field_parts[0];
79
                    $condition = $field_parts[1];
80
                    $this->originalFields[$field] = $condition;
81
                    $this->searchFields[$index] = $field;
82
                }
83
            }
84
        }
85
    }
86
87
    /**
88
     * @param array $fields
89
     * @throws \Exception
90
     */
91
    protected function assertFieldsNotAccepted($fields)
92
    {
93
        if (count($fields) == 0) {
94
            throw new Exception((string) trans('repository::criteria.fields_not_accepted', ['field' => implode(',', (array) $this->searchFields)]));
95
        }
96
    }
97
}