-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathFilterTrashed.php
64 lines (55 loc) · 1.5 KB
/
FilterTrashed.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<?php
/**
* @link https://github.com/illuminatech
* @copyright Copyright (c) 2019 Illuminatech
* @license [New BSD License](http://www.opensource.org/licenses/bsd-license.php)
*/
namespace Illuminatech\DataProvider\Filters;
use Illuminatech\DataProvider\Exceptions\InvalidQueryException;
use Illuminatech\DataProvider\FilterContract;
/**
* FilterTrashed provides filter for soft deleted (trashed) records.
*
* This filter responds to particular values:
*
* - 'with' - include 'trashed' records to the result set.
* - 'only' - return only 'trashed' records at the result set.
* - any other - return only records without 'trashed' at the result set.
*
* Usage example:
*
* ```php
* DataProvider(Item::class)
* ->filters([
* 'trashed' => new FilterTrashed,
* // ...
* ]);
* ```
*
* @see \Illuminate\Database\Eloquent\SoftDeletes
*
* @author Paul Klimov <[email protected]>
* @since 1.0
*/
class FilterTrashed implements FilterContract
{
/**
* {@inheritdoc}
*/
public function apply(object $source, string $name, $value): object
{
if (!is_scalar($value)) {
throw new InvalidQueryException('Filter "' . $name . '" requires scalar value.');
}
if ($value === 'with') {
$source->withTrashed();
return $source;
}
if ($value === 'only') {
$source->onlyTrashed();
return $source;
}
$source->withoutTrashed();
return $source;
}
}