1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This software package is licensed under `AGPL, Commercial` license[s]. |
5
|
|
|
* |
6
|
|
|
* @package maslosoft/zamm |
7
|
|
|
* @license AGPL, Commercial |
8
|
|
|
* |
9
|
|
|
* @copyright Copyright (c) Peter Maselkowski <[email protected]> |
10
|
|
|
* @link https://maslosoft.com/zamm/ |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace Maslosoft\Zamm\Decorators; |
14
|
|
|
|
15
|
|
|
use Maslosoft\Zamm\Interfaces\DecoratorInterface; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Abstract class implementgin common decorator methods |
19
|
|
|
* |
20
|
|
|
* @author Piotr Maselkowski <pmaselkowski at gmail.com> |
21
|
|
|
*/ |
22
|
|
|
abstract class AbstractDecorator |
23
|
|
|
{ |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Decorators |
27
|
|
|
* @var DecoratorInterface[] |
28
|
|
|
*/ |
29
|
|
|
private $decorators = []; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Apply decorators configuration. |
33
|
|
|
* Schema is described in Zamm class |
34
|
|
|
* @param string[][] $decoratorsConfig |
35
|
|
|
*/ |
36
|
|
|
protected function apply($decoratorsConfig) |
37
|
|
|
{ |
38
|
|
|
foreach ($decoratorsConfig as $interface => $decorators) |
39
|
|
|
{ |
40
|
|
|
if (!$this->decorated() instanceof $interface) |
41
|
|
|
{ |
42
|
|
|
continue; |
43
|
|
|
} |
44
|
|
|
foreach ($decorators as $decorator) |
45
|
|
|
{ |
46
|
|
|
$this->addDecorator($decorator); |
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Add decorator. This will perform uniqueness check. |
53
|
|
|
* @param string $className |
54
|
|
|
* @return boolean true if added |
55
|
|
|
*/ |
56
|
|
|
public function addDecorator($className) |
57
|
|
|
{ |
58
|
|
|
if (isset($this->decorators[$className])) |
59
|
|
|
{ |
60
|
|
|
return false; |
61
|
|
|
} |
62
|
|
|
$decorator = new $className(); |
63
|
|
|
assert($decorator instanceof DecoratorInterface); |
64
|
|
|
$this->decorators[$className] = $decorator; |
65
|
|
|
|
66
|
|
|
return true; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
abstract protected function init(DecoratorInterface $decorator); |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Decorated entity |
73
|
|
|
* @return object |
74
|
|
|
*/ |
75
|
|
|
abstract protected function decorated(); |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Decorate doc comment |
79
|
|
|
* @param string $docComment |
80
|
|
|
*/ |
81
|
|
|
public function decorate(&$docComment) |
82
|
|
|
{ |
83
|
|
|
foreach ($this->decorators as $decorator) |
84
|
|
|
{ |
85
|
|
|
$decorator->decorate($docComment); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
} |
90
|
|
|
|