1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Staudenmeir\LaravelAdjacencyList\Eloquent\Relations\Traits; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\SoftDeletingScope; |
6
|
|
|
|
7
|
|
|
trait TracksIntermediateScopes |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* Applied intermediate scopes. |
11
|
|
|
* |
12
|
|
|
* @var array |
13
|
|
|
*/ |
14
|
|
|
protected $intermediateScopes = []; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Removed intermediate scopes. |
18
|
|
|
* |
19
|
|
|
* @var array |
20
|
|
|
*/ |
21
|
|
|
protected $removedIntermediateScopes = []; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Register a new intermediate scope. |
25
|
|
|
* |
26
|
|
|
* @param string $identifier |
27
|
|
|
* @param \Illuminate\Database\Eloquent\Scope|\Closure $scope |
28
|
|
|
* @return $this |
29
|
|
|
*/ |
30
|
100 |
|
public function withIntermediateScope($identifier, $scope) |
31
|
|
|
{ |
32
|
100 |
|
$this->intermediateScopes[$identifier] = $scope; |
33
|
|
|
|
34
|
100 |
|
if (method_exists($scope, 'extend')) { |
35
|
100 |
|
$scope->extend($this); |
36
|
|
|
} |
37
|
|
|
|
38
|
100 |
|
return $this; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Remove a registered intermediate scope. |
43
|
|
|
* |
44
|
|
|
* @param \Illuminate\Database\Eloquent\Scope|string $scope |
45
|
|
|
* @return $this |
46
|
|
|
*/ |
47
|
100 |
|
public function withoutIntermediateScope($scope) |
48
|
|
|
{ |
49
|
100 |
|
if (!is_string($scope)) { |
50
|
20 |
|
$scope = get_class($scope); |
51
|
|
|
} |
52
|
|
|
|
53
|
100 |
|
unset($this->intermediateScopes[$scope]); |
54
|
|
|
|
55
|
100 |
|
$this->removedIntermediateScopes[] = $scope; |
56
|
|
|
|
57
|
100 |
|
return $this; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Remove all or passed registered intermediate scopes. |
62
|
|
|
* |
63
|
|
|
* @param array|null $scopes |
64
|
|
|
* @return $this |
65
|
|
|
*/ |
66
|
20 |
|
public function withoutIntermediateScopes(?array $scopes = null) |
67
|
|
|
{ |
68
|
20 |
|
if (!is_array($scopes)) { |
69
|
20 |
|
$scopes = array_keys($this->intermediateScopes); |
70
|
|
|
} |
71
|
|
|
|
72
|
20 |
|
foreach ($scopes as $scope) { |
73
|
20 |
|
$this->withoutIntermediateScope($scope); |
74
|
|
|
} |
75
|
|
|
|
76
|
20 |
|
return $this; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Include trashed descendants in the query. |
81
|
|
|
* |
82
|
|
|
* @return $this |
83
|
|
|
*/ |
84
|
20 |
|
public function withTrashedDescendants() |
85
|
|
|
{ |
86
|
20 |
|
return $this->withoutIntermediateScope(SoftDeletingScope::class); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Get applied intermediate scopes. |
91
|
|
|
* |
92
|
|
|
* @return array |
93
|
|
|
*/ |
94
|
20 |
|
public function intermediateScopes() |
95
|
|
|
{ |
96
|
20 |
|
return $this->intermediateScopes; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Get removed intermediate scopes. |
101
|
|
|
* |
102
|
|
|
* @return array |
103
|
|
|
*/ |
104
|
20 |
|
public function removedIntermediateScopes() |
105
|
|
|
{ |
106
|
20 |
|
return $this->removedIntermediateScopes; |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|