File tree Expand file tree Collapse file tree 9 files changed +47
-47
lines changed Expand file tree Collapse file tree 9 files changed +47
-47
lines changed Original file line number Diff line number Diff line change @@ -20,7 +20,7 @@ public function getName(): string
20
20
return sprintf ('Group: %s ' , $ this ->name );
21
21
}
22
22
23
- public function accept (RoleVisitorInterface $ visitor )
23
+ public function accept (RoleVisitor $ visitor )
24
24
{
25
25
$ visitor ->visitGroup ($ this );
26
26
}
Original file line number Diff line number Diff line change 25
25
26
26
You can also find this code on `GitHub `_
27
27
28
- RoleVisitorInterface .php
28
+ RoleVisitor .php
29
29
30
- .. literalinclude :: RoleVisitorInterface .php
30
+ .. literalinclude :: RoleVisitor .php
31
31
:language: php
32
32
:linenos:
33
33
34
- RoleVisitor .php
34
+ RecordingVisitor .php
35
35
36
- .. literalinclude :: RoleVisitor .php
36
+ .. literalinclude :: RecordingVisitor .php
37
37
:language: php
38
38
:linenos:
39
39
@@ -65,4 +65,4 @@ Tests/VisitorTest.php
65
65
:linenos:
66
66
67
67
.. _`GitHub` : https://github.com/domnikl/DesignPatternsPHP/tree/master/Behavioral/Visitor
68
- .. __ : http://en.wikipedia.org/wiki/Visitor_pattern
68
+ .. __ : http://en.wikipedia.org/wiki/Visitor_pattern
Original file line number Diff line number Diff line change
1
+ <?php
2
+ declare (strict_types=1 );
3
+
4
+ namespace DesignPatterns \Behavioral \Visitor ;
5
+
6
+ class RecordingVisitor implements RoleVisitor
7
+ {
8
+ /**
9
+ * @var Role[]
10
+ */
11
+ private $ visited = [];
12
+
13
+ public function visitGroup (Group $ role )
14
+ {
15
+ $ this ->visited [] = $ role ;
16
+ }
17
+
18
+ public function visitUser (User $ role )
19
+ {
20
+ $ this ->visited [] = $ role ;
21
+ }
22
+
23
+ /**
24
+ * @return Role[]
25
+ */
26
+ public function getVisited (): array
27
+ {
28
+ return $ this ->visited ;
29
+ }
30
+ }
Original file line number Diff line number Diff line change 5
5
6
6
interface Role
7
7
{
8
- public function accept (RoleVisitorInterface $ visitor );
8
+ public function accept (RoleVisitor $ visitor );
9
9
}
Original file line number Diff line number Diff line change 3
3
4
4
namespace DesignPatterns \Behavioral \Visitor ;
5
5
6
- class RoleVisitor implements RoleVisitorInterface
6
+ /**
7
+ * Note: the visitor must not choose itself which method to
8
+ * invoke, it is the Visitee that make this decision
9
+ */
10
+ interface RoleVisitor
7
11
{
8
- /**
9
- * @var Role[]
10
- */
11
- private $ visited = [];
12
+ public function visitUser (User $ role );
12
13
13
- public function visitGroup (Group $ role )
14
- {
15
- $ this ->visited [] = $ role ;
16
- }
17
-
18
- public function visitUser (User $ role )
19
- {
20
- $ this ->visited [] = $ role ;
21
- }
22
-
23
- /**
24
- * @return Role[]
25
- */
26
- public function getVisited (): array
27
- {
28
- return $ this ->visited ;
29
- }
14
+ public function visitGroup (Group $ role );
30
15
}
Load Diff This file was deleted.
Original file line number Diff line number Diff line change 9
9
class VisitorTest extends TestCase
10
10
{
11
11
/**
12
- * @var Visitor\RoleVisitor
12
+ * @var Visitor\RecordingVisitor
13
13
*/
14
14
private $ visitor ;
15
15
16
16
protected function setUp (): void
17
17
{
18
- $ this ->visitor = new Visitor \RoleVisitor ();
18
+ $ this ->visitor = new Visitor \RecordingVisitor ();
19
19
}
20
20
21
21
public function provideRoles ()
Original file line number Diff line number Diff line change @@ -20,7 +20,7 @@ public function getName(): string
20
20
return sprintf ('User %s ' , $ this ->name );
21
21
}
22
22
23
- public function accept (RoleVisitorInterface $ visitor )
23
+ public function accept (RoleVisitor $ visitor )
24
24
{
25
25
$ visitor ->visitUser ($ this );
26
26
}
You can’t perform that action at this time.
0 commit comments