|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Created by PhpStorm. |
|
4
|
|
|
* User: peter |
|
5
|
|
|
* Date: 18.06.18 |
|
6
|
|
|
* Time: 21:59 |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace Maslosoft\Mangan; |
|
10
|
|
|
|
|
11
|
|
|
|
|
12
|
|
|
use Maslosoft\Addendum\Interfaces\AnnotatedInterface; |
|
13
|
|
|
use Maslosoft\Mangan\Interfaces\AspectsInterface; |
|
14
|
|
|
|
|
15
|
|
|
class AspectManager |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* Gracefully add aspect, this will check |
|
19
|
|
|
* if model implements AspectsInterface |
|
20
|
|
|
* and add only if it does. |
|
21
|
|
|
* |
|
22
|
|
|
* @see AspectsInterface |
|
23
|
|
|
* @param AnnotatedInterface $model |
|
24
|
|
|
* @param $aspect |
|
25
|
|
|
*/ |
|
26
|
178 |
|
public static function addAspect(AnnotatedInterface $model = null, $aspect = '') |
|
27
|
|
|
{ |
|
28
|
178 |
|
if(empty($model)) |
|
29
|
|
|
{ |
|
30
|
105 |
|
return; |
|
31
|
|
|
} |
|
32
|
166 |
|
if(empty($aspect)) |
|
33
|
|
|
{ |
|
34
|
|
|
return; |
|
35
|
|
|
} |
|
36
|
166 |
|
if($model instanceof AspectsInterface) |
|
37
|
|
|
{ |
|
38
|
69 |
|
$model->addAspect($aspect); |
|
39
|
|
|
} |
|
40
|
166 |
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Gracefully remove aspect, this will check |
|
44
|
|
|
* if model implements AspectsInterface |
|
45
|
|
|
* and remove only if it does. |
|
46
|
|
|
* |
|
47
|
|
|
* @see AspectsInterface |
|
48
|
|
|
* @param AnnotatedInterface $model |
|
49
|
|
|
* @param $aspect |
|
50
|
|
|
*/ |
|
51
|
177 |
|
public static function removeAspect(AnnotatedInterface $model = null, $aspect = '') |
|
52
|
|
|
{ |
|
53
|
177 |
|
if(empty($model)) |
|
54
|
|
|
{ |
|
55
|
105 |
|
return; |
|
56
|
|
|
} |
|
57
|
177 |
|
if(empty($aspect)) |
|
58
|
|
|
{ |
|
59
|
|
|
return; |
|
60
|
|
|
} |
|
61
|
177 |
|
if($model instanceof AspectsInterface) |
|
62
|
|
|
{ |
|
63
|
72 |
|
$model->removeAspect($aspect); |
|
64
|
|
|
} |
|
65
|
177 |
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Gracefully check if has aspect, this will check |
|
69
|
|
|
* if model implements `AspectsInterface` |
|
70
|
|
|
* and check only if it does. If model does not |
|
71
|
|
|
* implement AspectsInterface it will return `false`, |
|
72
|
|
|
* like if it has no such aspect. |
|
73
|
|
|
* |
|
74
|
|
|
* @see AspectsInterface |
|
75
|
|
|
* @param AnnotatedInterface $model |
|
76
|
|
|
* @param $aspect |
|
77
|
|
|
* @return bool |
|
78
|
|
|
*/ |
|
79
|
27 |
|
public static function hasAspect(AnnotatedInterface $model = null, $aspect = '') |
|
80
|
|
|
{ |
|
81
|
27 |
|
if(empty($model)) |
|
82
|
|
|
{ |
|
83
|
|
|
return false; |
|
84
|
|
|
} |
|
85
|
27 |
|
if(empty($aspect)) |
|
86
|
|
|
{ |
|
87
|
|
|
return false; |
|
88
|
|
|
} |
|
89
|
27 |
|
if($model instanceof AspectsInterface) |
|
90
|
|
|
{ |
|
91
|
14 |
|
return $model->hasAspect($aspect); |
|
92
|
|
|
} |
|
93
|
13 |
|
return false; |
|
94
|
|
|
} |
|
95
|
|
|
} |