Completed
Push — master ( 24a748...5cfece )
by
unknown
01:45
created

FiltersPartial   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 24
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 1
dl 0
loc 24
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 16 3
A escapeLike() 0 4 1
1
<?php
2
3
namespace Spatie\QueryBuilder\Filters;
4
5
use Illuminate\Support\Facades\DB;
6
use Illuminate\Database\Eloquent\Builder;
7
8
class FiltersPartial implements Filter
9
{
10
    public function __invoke(Builder $query, $value, string $property) : Builder
11
    {
12
        if (is_array($value)) {
13
            return $query->where(function (Builder $query) use ($value, $property) {
14
                foreach ($value as $partialValue) {
15
                    $query
16
                        ->orWhere($property, 'LIKE', DB::raw('? ESCAPE "\"'))
17
                        ->addBinding("%{$this->escapeLike($partialValue)}%");
18
                }
19
            });
20
        }
21
22
        return $query
23
            ->where($property, 'LIKE', DB::raw('? ESCAPE "\"'))
24
            ->addBinding("%{$this->escapeLike($value)}%");
25
    }
26
27
    protected function escapeLike(string $value): string
28
    {
29
        return addcslashes($value, '\%_');
30
    }
31
}
32