|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Webino (http://webino.sk) |
|
4
|
|
|
* |
|
5
|
|
|
* @link https://github.com/webino/WebinoDraw for the canonical source repository |
|
6
|
|
|
* @copyright Copyright (c) 2012-2017 Webino, s. r. o. (http://webino.sk) |
|
7
|
|
|
* @author Peter Bačinský <[email protected]> |
|
8
|
|
|
* @license BSD-3-Clause |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace WebinoDraw\Manipulator; |
|
12
|
|
|
|
|
13
|
|
|
use WebinoDraw\Dom\NodeInterface; |
|
14
|
|
|
use WebinoDraw\Manipulator\Plugin\PluginArgument; |
|
15
|
|
|
use WebinoDraw\Manipulator\Plugin\PluginInterface; |
|
16
|
|
|
use WebinoDraw\VarTranslator\VarTranslator; |
|
17
|
|
|
use Zend\Stdlib\PriorityQueue; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Class Manipulator |
|
21
|
|
|
*/ |
|
22
|
|
|
class Manipulator |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* @var VarTranslator |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $varTranslator; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var PriorityQueue |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $plugins; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @param VarTranslator $varTranslator |
|
36
|
|
|
*/ |
|
37
|
|
|
public function __construct(VarTranslator $varTranslator) |
|
38
|
|
|
{ |
|
39
|
|
|
$this->varTranslator = $varTranslator; |
|
40
|
|
|
$this->plugins = new PriorityQueue; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @param PluginInterface $plugin |
|
45
|
|
|
* @param int $priority |
|
46
|
|
|
* @return self |
|
47
|
|
|
*/ |
|
48
|
|
|
public function setPlugin(PluginInterface $plugin, $priority = 1) |
|
49
|
|
|
{ |
|
50
|
|
|
$this->plugins->insert($plugin, $priority); |
|
51
|
|
|
return $this; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @param array $options |
|
56
|
|
|
* @return self |
|
57
|
|
|
*/ |
|
58
|
|
|
public function manipulate(array $options) |
|
59
|
|
|
{ |
|
60
|
|
|
$arg = $this->createPluginArgument($options); |
|
61
|
|
|
|
|
62
|
|
|
$this->eachPlugin( |
|
63
|
|
|
Plugin\PreLoopPluginInterface::class, |
|
64
|
|
|
function ($plugin) use ($arg) { |
|
65
|
|
|
$plugin->preLoop($arg); |
|
66
|
|
|
} |
|
67
|
|
|
); |
|
68
|
|
|
|
|
69
|
|
|
if ($arg->isManipulationStopped()) { |
|
70
|
|
|
return $this; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
foreach ($arg->getNodes()->toArray() as $node) { |
|
74
|
|
|
if (!($node instanceof NodeInterface) || empty($node->parentNode)) { |
|
|
|
|
|
|
75
|
|
|
continue; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
$arg->setNode($node); |
|
79
|
|
|
|
|
80
|
|
|
$this->eachPlugin( |
|
81
|
|
|
Plugin\InLoopPluginInterface::class, |
|
82
|
|
|
function ($plugin) use ($arg) { |
|
83
|
|
|
$plugin->inLoop($arg); |
|
84
|
|
|
} |
|
85
|
|
|
); |
|
86
|
|
|
|
|
87
|
|
|
if ($arg->isManipulationStopped()) { |
|
88
|
|
|
return $this; |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
$this->eachPlugin( |
|
93
|
|
|
Plugin\PostLoopPluginInterface::class, |
|
94
|
|
|
function ($plugin) use ($arg) { |
|
95
|
|
|
$plugin->postLoop($arg); |
|
96
|
|
|
} |
|
97
|
|
|
); |
|
98
|
|
|
|
|
99
|
|
|
return $this; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* @param array $options |
|
104
|
|
|
* @return PluginArgument |
|
105
|
|
|
*/ |
|
106
|
|
|
protected function createPluginArgument(array $options) |
|
107
|
|
|
{ |
|
108
|
|
|
return new PluginArgument($options); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* @param string $className |
|
113
|
|
|
* @param callable $callback |
|
114
|
|
|
* @return self |
|
115
|
|
|
*/ |
|
116
|
|
|
protected function eachPlugin($className, callable $callback) |
|
117
|
|
|
{ |
|
118
|
|
|
foreach ($this->plugins as $plugin) { |
|
119
|
|
|
is_subclass_of($plugin, $className) |
|
|
|
|
|
|
120
|
|
|
and $callback($plugin); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
return $this; |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
|
If you access a property on an interface, you most likely code against a concrete implementation of the interface.
Available Fixes
Adding an additional type check:
Changing the type hint: