Completed
Push — master ( d5af7a...83c102 )
by
unknown
02:50 queued 01:21
created

FiltersTrashed   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 20
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
wmc 3
lcom 0
cbo 0
dl 0
loc 20
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 16 3
1
<?php
2
3
namespace Spatie\QueryBuilder\Filters;
4
5
use Illuminate\Database\Eloquent\Builder;
6
7
/**
8
 * FiltersTrashed provides filter for soft deleted (trashed) records.
9
 *
10
 * This filter responds to particular values:
11
 *
12
 * - 'with' - include 'trashed' records to the result set.
13
 * - 'only' - return only 'trashed' records at the result set.
14
 * - any other - return only records without 'trashed' at the result set.
15
 *
16
 * Usage example:
17
 *
18
 * ```php
19
 * QueryBuilder::for(Item::class)
20
 *     ->allowedFilters([
21
 *         AllowedFilter::trashed(),
22
 *         // ...
23
 *     ]);
24
 * ```
25
 *
26
 * @see \Illuminate\Database\Eloquent\SoftDeletes
27
 * @see \Spatie\QueryBuilder\AllowedFilter::trashed()
28
 */
29
class FiltersTrashed implements Filter
30
{
31
    /** {@inheritdoc} */
32
    public function __invoke(Builder $query, $value, string $property)
33
    {
34
        if ($value === 'with') {
35
            $query->withTrashed();
36
37
            return;
38
        }
39
40
        if ($value === 'only') {
41
            $query->onlyTrashed();
42
43
            return;
44
        }
45
46
        $query->withoutTrashed();
47
    }
48
}
49