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