SearchTrait::checkSearchField()   A
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 5
eloc 6
c 3
b 0
f 0
nc 5
nop 5
dl 0
loc 13
rs 9.6111
1
<?php
2
3
namespace Blasttech\EloquentRelatedPlus;
4
5
use Illuminate\Database\Eloquent\Builder;
6
use Illuminate\Database\Eloquent\Model;
7
8
/**
9
 * Trait SearchTrait
10
 *
11
 * @property array attributes
12
 * @property array orderFields
13
 * @property array orderDefaults
14
 * @property array orderRelations
15
 * @property array orderWith
16
 * @property array searchFields
17
 * @property string connection
18
 */
19
trait SearchTrait
20
{
21
    /**
22
     * Add where statements for search fields to search for searchText
23
     *
24
     * @param Builder $query
25
     * @param string $searchText
26
     * @return Builder
27
     */
28
    protected function checkSearchFields($query, $searchText)
29
    {
30
        return $query->where(function (Builder $query) use ($searchText) {
31
            if (isset($this->searchFields) && !empty($this->searchFields)) {
32
                /** @var Model $this */
33
                $table = $this->getTable();
34
                foreach ($this->searchFields as $searchField => $searchFieldParameters) {
35
                    $query = $this->checkSearchField($query, $table, $searchField, $searchFieldParameters, $searchText);
36
                }
37
            }
38
39
            return $query;
40
        });
41
    }
42
43
    /**
44
     * Add where statement for a search field
45
     *
46
     * @param Builder $query
47
     * @param string $table
48
     * @param string $searchField
49
     * @param array $fieldParameters
50
     * @param string $searchText
51
     * @return Builder
52
     */
53
    protected function checkSearchField($query, $table, $searchField, $fieldParameters, $searchText)
54
    {
55
        if (!isset($fieldParameters['regex']) || preg_match($fieldParameters['regex'], $searchText)) {
56
            $searchColumn = is_array($fieldParameters) ? $searchField : $fieldParameters;
57
58
            if (isset($fieldParameters['relation'])) {
59
                return $this->searchRelation($query, $fieldParameters, $searchColumn, $searchText);
60
            }
61
62
            return $this->searchThis($query, $fieldParameters, $table, $searchColumn, $searchText);
63
        }
64
65
        return $query;
66
    }
67
68
    /**
69
     * Add where condition to search a relation
70
     *
71
     * @param Builder $query
72
     * @param array $fieldParameters
73
     * @param string $searchColumn
74
     * @param string $searchText
75
     * @return Builder
76
     */
77
    protected function searchRelation(Builder $query, $fieldParameters, $searchColumn, $searchText)
78
    {
79
        $relation = $fieldParameters['relation'];
80
        $relatedTable = $this->$relation()->getRelated()->getTable();
81
82
        return $query->orWhere(function (Builder $query) use (
83
            $searchText,
84
            $searchColumn,
85
            $relation,
86
            $relatedTable
87
        ) {
88
            return $query->orWhereHas($relation, function (Builder $query2) use (
89
                $searchText,
90
                $searchColumn,
91
                $relatedTable
92
            ) {
93
                return $query2->where($relatedTable . '.' . $searchColumn, 'like', $searchText . '%');
94
            });
95
        });
96
    }
97
98
    /**
99
     * Add where condition to search current model
100
     *
101
     * @param Builder $query
102
     * @param array $fieldParameters
103
     * @param string $table
104
     * @param string $searchColumn
105
     * @param string $searchText
106
     * @return Builder
107
     */
108
    protected function searchThis(Builder $query, $fieldParameters, $table, $searchColumn, $searchText)
109
    {
110
        $searchOperator = $fieldParameters['operator'] ?? 'like';
111
        $searchValue = $fieldParameters['value'] ?? '%{{search}}%';
112
113
        return $query->orWhere(
114
            $table . '.' . $searchColumn,
115
            $searchOperator,
116
            str_replace('{{search}}', $searchText, $searchValue)
117
        );
118
    }
119
}
120