Completed
Pull Request — master (#223)
by Axel
04:10
created

SearchesPartial   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 4
dl 0
loc 32
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 24 4
A encloseValue() 0 4 1
1
<?php
2
3
namespace Spatie\QueryBuilder\Searches;
4
5
use Illuminate\Database\Eloquent\Builder;
6
7
class SearchesPartial extends SearchesBase
8
{
9
    public function __invoke(Builder $query, $value, string $property, ?string $modifier = null): Builder
10
    {
11
        if ($this->isRelationProperty($query, $property)) {
12
            return $this->withRelationConstraint($query, $value, $property);
13
        }
14
15
        $wrappedProperty = $query->getQuery()->getGrammar()->wrap($property);
16
17
        $sql = "LOWER({$wrappedProperty}) LIKE ?";
18
19
        if (is_array($value)) {
20
            return $query->orWhere(function (Builder $query) use ($value, $sql) {
21
                foreach ($value as $partialValue) {
22
                    $partialValue = mb_strtolower($partialValue, 'UTF8');
23
24
                    $query->orWhereRaw($sql, [$this->encloseValue($partialValue)]);
25
                }
26
            });
27
        }
28
29
        $value = mb_strtolower($value, 'UTF8');
30
31
        return $query->orWhereRaw($sql, [$this->encloseValue($value)]);
32
    }
33
34
    protected function encloseValue($value)
35
    {
36
        return "%{$value}%";
37
    }
38
}
39