|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This software package is licensed under AGPL or Commercial license. |
|
5
|
|
|
* |
|
6
|
|
|
* @package maslosoft/mangan |
|
7
|
|
|
* @licence AGPL or Commercial |
|
8
|
|
|
* @copyright Copyright (c) Piotr Masełkowski <[email protected]> |
|
9
|
|
|
* @copyright Copyright (c) Maslosoft |
|
10
|
|
|
* @copyright Copyright (c) Others as mentioned in code |
|
11
|
|
|
* @link https://maslosoft.com/mangan/ |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
namespace Maslosoft\Mangan\Annotations; |
|
15
|
|
|
|
|
16
|
|
|
use function is_a; |
|
17
|
|
|
use Maslosoft\Addendum\Helpers\ParamsExpander; |
|
18
|
|
|
use Maslosoft\Addendum\Utilities\ClassChecker; |
|
19
|
|
|
use Maslosoft\Mangan\Interfaces\CriteriaInterface; |
|
20
|
|
|
use Maslosoft\Mangan\Interfaces\ScopeInterface; |
|
21
|
|
|
use Maslosoft\Mangan\Meta\ManganTypeAnnotation; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Annotation can be used to add extra scope |
|
25
|
|
|
* for model. Scopes will add extra criteria when |
|
26
|
|
|
* finding, updating and removing model. |
|
27
|
|
|
* |
|
28
|
|
|
* The scope is a simple class implementing `ScopeInterface`. |
|
29
|
|
|
* The `ScopeInterface` method `getCriteria` must return `CriteriaInterface`, |
|
30
|
|
|
* most likely `Criteria` instance. |
|
31
|
|
|
* |
|
32
|
|
|
* Usage: |
|
33
|
|
|
* |
|
34
|
|
|
* ``` |
|
35
|
|
|
* @Scope(EmbeddedClassName) |
|
36
|
|
|
* ``` |
|
37
|
|
|
* |
|
38
|
|
|
* @Target('class') |
|
39
|
|
|
* @template Scope(${defaultClassName}) |
|
40
|
|
|
* @see ScopeInterface |
|
41
|
|
|
* @see CriteriaInterface |
|
42
|
|
|
* @author Piotr |
|
43
|
|
|
*/ |
|
44
|
|
|
class ScopeAnnotation extends ManganTypeAnnotation |
|
45
|
|
|
{ |
|
46
|
|
|
|
|
47
|
|
|
public $value = true; |
|
48
|
|
|
|
|
49
|
2 |
|
public function init() |
|
50
|
|
|
{ |
|
51
|
2 |
|
$data = ParamsExpander::expand($this, ['class']); |
|
52
|
2 |
|
$className = $data['class']; |
|
53
|
2 |
|
assert(ClassChecker::exists($className)); |
|
54
|
2 |
|
assert(is_a($className, ScopeInterface::class, true)); |
|
55
|
2 |
|
$this->getEntity()->scopes[] = $className; |
|
56
|
2 |
|
} |
|
57
|
|
|
|
|
58
|
|
|
} |
|
59
|
|
|
|