Completed
Push — master ( ceee37...536973 )
by Enea
02:33
created

Wrap::bind()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
eloc 2
nc 2
nop 2
dl 0
loc 4
rs 10
c 1
b 0
f 1
1
<?php
2
/**
3
 * Created by enea dhack - 18/07/2020 21:32.
4
 */
5
6
namespace Vaened\Searcher\Constraints;
7
8
use Closure;
9
use Illuminate\Database\Eloquent\Builder;
10
use Vaened\Searcher\Constraint;
11
12
class Wrap implements Constraint
13
{
14
    private array $constraints;
15
16
    public function __construct(array $constraints)
17
    {
18
        $this->constraints = $constraints;
19
    }
20
21
    public function condition(Builder $builder): Builder
22
    {
23
        return $builder->where($this->apply($this->constraints));
24
    }
25
26
    private function apply(array $constraints): Closure
27
    {
28
        return fn(Builder $builder) => $this->bind($builder, $constraints);
29
    }
30
31
    private function bind(Builder $builder, array $constraints): void
32
    {
33
        foreach ($constraints as $constraint) {
34
            $constraint->condition($builder);
35
        }
36
    }
37
}
38