ParseFieldsSearchTrait::assertFieldsNotAccepted()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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