Has::condition()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 3
c 1
b 0
f 1
dl 0
loc 7
rs 10
cc 2
nc 2
nop 1
1
<?php
2
/**
3
 * Created by enea dhack - 15/06/2020 16:16.
4
 */
5
6
namespace Vaened\Searcher\Constraints;
7
8
use Illuminate\Database\Eloquent\Builder;
9
use Vaened\Searcher\Constraint;
10
11
class Has implements Constraint
12
{
13
    private string $relationship;
14
15
    private ?Constraint $constraint;
16
17
    public function __construct(string $relationship, ?Constraint $constraint = null)
18
    {
19
        $this->relationship = $relationship;
20
        $this->constraint = $constraint;
21
    }
22
23
    public function condition(Builder $builder): Builder
24
    {
25
        if ($this->constraint == null) {
26
            return $builder->whereHas($this->relationship);
27
        }
28
29
        return $builder->whereHas($this->relationship, fn(Builder $builder) => $this->constraint->condition($builder));
30
    }
31
}
32