FiltersPartial   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 0
Metric Value
wmc 6
lcom 0
cbo 4
dl 0
loc 37
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B __invoke() 0 34 6
1
<?php
2
3
namespace Spatie\QueryBuilder\Filters;
4
5
use Illuminate\Database\Eloquent\Builder;
6
7
class FiltersPartial extends FiltersExact implements Filter
8
{
9
    public function __invoke(Builder $query, $value, string $property)
10
    {
11
        if ($this->addRelationConstraint) {
12
            if ($this->isRelationProperty($query, $property)) {
13
                $this->withRelationConstraint($query, $value, $property);
14
15
                return;
16
            }
17
        }
18
19
        $wrappedProperty = $query->getQuery()->getGrammar()->wrap($property);
20
21
        $sql = "LOWER({$wrappedProperty}) LIKE ?";
22
23
        if (is_array($value)) {
24
            if (count(array_filter($value)) === 0) {
25
                return $query;
26
            }
27
28
            $query->where(function (Builder $query) use ($value, $sql) {
29
                foreach (array_filter($value) as $partialValue) {
30
                    $partialValue = mb_strtolower($partialValue, 'UTF8');
31
32
                    $query->orWhereRaw($sql, ["%{$partialValue}%"]);
33
                }
34
            });
35
36
            return;
37
        }
38
39
        $value = mb_strtolower($value, 'UTF8');
40
41
        $query->whereRaw($sql, ["%{$value}%"]);
42
    }
43
}
44