1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace WebinoViewLib\Component; |
4
|
|
|
|
5
|
|
|
use WebinoAppLib\Event\AppEvent; |
6
|
|
|
use WebinoAppLib\Event\DispatchEvent; |
7
|
|
|
use WebinoAppLib\Feature\HttpListener; |
8
|
|
|
use WebinoAppLib\Service\Initializer\RoutingAwareTrait; |
9
|
|
|
use WebinoConfigLib\Feature\FeatureInterface; |
10
|
|
|
use WebinoDomLib\Dom\Config\SpecConfigAggregateInterface; |
11
|
|
|
use WebinoDomLib\Dom\NodeInterface; |
12
|
|
|
use WebinoDomLib\Event\RenderEvent; |
13
|
|
|
use WebinoEventLib\ListenerAggregateTrait; |
14
|
|
|
use WebinoViewLib\Feature\NodeView; |
15
|
|
|
use WebinoViewLib\Feature\ViewListener; |
16
|
|
|
use Zend\EventManager\ListenerAggregateInterface; |
17
|
|
|
use Zend\Stdlib\CallbackHandler; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Class ComponentStateTrait |
21
|
|
|
* @TODO concept |
22
|
|
|
*/ |
23
|
|
|
trait ComponentStateTrait |
24
|
|
|
{ |
25
|
|
|
use RoutingAwareTrait; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var \stdClass |
29
|
|
|
*/ |
30
|
|
|
private $state; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @TODO concept |
34
|
|
|
* @var NodeInterface[] |
35
|
|
|
*/ |
36
|
|
|
private $nodes = []; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @return RenderEvent |
40
|
|
|
*/ |
41
|
|
|
abstract protected function getRenderEvent(); |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* TODO concept |
45
|
|
|
* @param DispatchEvent $event |
46
|
|
|
*/ |
47
|
|
|
public function onDispatchState(DispatchEvent $event) |
48
|
|
|
{ |
49
|
|
|
// TODO resolve state |
50
|
|
|
$state = $event->getRequest()->getQuery()->counter[$this->getIndex()]; |
|
|
|
|
51
|
|
|
|
52
|
|
|
empty($state) or $this->mergeState($state); |
|
|
|
|
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @TODO concept |
57
|
|
|
* @param string $name Node name |
58
|
|
|
* @param string|\Closure $method |
59
|
|
|
* @return $this |
60
|
|
|
*/ |
61
|
|
|
public function onStateChange($name, $method) |
62
|
|
|
{ |
63
|
|
|
// TODO create href concept |
64
|
|
|
$clone = clone $this; |
65
|
|
|
|
66
|
|
|
// TODO refactor |
67
|
|
|
if (is_string($method)) { |
68
|
|
|
$clone->$method(); |
69
|
|
|
} elseif ($method instanceof \Closure) { |
70
|
|
|
call_user_func($method->bindTo($clone)); |
71
|
|
|
} else { |
|
|
|
|
72
|
|
|
// TODO invalid argument exception |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
// TODO common |
76
|
|
|
$query = ['counter' => [$this->getIndex() => (array) $clone->getState()]]; |
|
|
|
|
77
|
|
|
|
78
|
|
|
$href = $this->getRouter()->url()->setOption('query', $query); |
|
|
|
|
79
|
|
|
|
80
|
|
|
// TODO concept |
81
|
|
|
$node = $this->getRenderEvent()->getNode($name); |
82
|
|
|
$node->setAttribute('href', $href); |
83
|
|
|
|
84
|
|
|
return $this; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @return \stdClass |
89
|
|
|
*/ |
90
|
|
|
public function getState() |
91
|
|
|
{ |
92
|
|
|
if (null === $this->state) { |
93
|
|
|
$this->state = new \stdClass; |
94
|
|
|
$this->initState($this->state); |
95
|
|
|
} |
96
|
|
|
return $this->state; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @param \stdClass $state |
101
|
|
|
* @return $this |
102
|
|
|
*/ |
103
|
|
|
public function setState(\stdClass $state) |
104
|
|
|
{ |
105
|
|
|
$this->state = $state; |
106
|
|
|
return $this; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @param array|\stdClass $state |
111
|
|
|
* @return $this |
112
|
|
|
*/ |
113
|
|
|
public function mergeState($state) |
114
|
|
|
{ |
115
|
|
|
$this->state = (object) array_replace_recursive((array) $this->getState(), (array) $state); |
116
|
|
|
return $this; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @param \stdClass $state |
121
|
|
|
*/ |
122
|
|
|
public function initState(\stdClass $state) |
|
|
|
|
123
|
|
|
{ |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @param string $name |
128
|
|
|
* @return ComponentNode |
129
|
|
|
*/ |
130
|
|
|
// protected function getNode($name) |
|
|
|
|
131
|
|
|
// { |
132
|
|
|
// if (empty($this->nodes[$name])) { |
133
|
|
|
// $this->nodes[$name] = new ComponentNode($this->getRenderEvent()->getNode($name), $this); |
134
|
|
|
// } |
135
|
|
|
// return $this->nodes[$name]; |
136
|
|
|
// } |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @TODO concept |
140
|
|
|
*/ |
141
|
|
|
public function __clone() |
142
|
|
|
{ |
143
|
|
|
$this->state = clone $this->getState(); |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: