Visitor.php
1 <?php
2 /**
3  * wCMF - wemove Content Management Framework
4  * Copyright (C) 2005-2020 wemove digital solutions GmbH
5  *
6  * Licensed under the terms of the MIT License.
7  *
8  * See the LICENSE file distributed with this work for
9  * additional information.
10  */
11 namespace wcmf\lib\model\visitor;
12 
14 
15 /**
16  * Visitor is used to extend an object's functionality by not extending
17  * its interface. Classes to use with the Visitor must implement the acceptVisitor() method.
18  * Visitor implements the 'Visitor Pattern'.
19  * It implements the 'Template Method Pattern' to allow subclasses
20  * to do any Pre- and Post Visit operations (doPreVisit() and doPostVisit() methods).
21  * The abstract base class Visitor defines the interface for all
22  * specialized Visitor classes.
23  *
24  * @author ingo herwig <ingo@wemove.com>
25  */
26 abstract class Visitor {
27 
28  /**
29  * Start the visiting process by iterating over all objects using
30  * the given NodeIterator. The visit() method is called by every
31  * visited object.
32  * @param $iterator NodeIterator to use (configured with the start object).
33  */
34  public function startIterator(NodeIterator $iterator) {
35  $this->doPreVisit();
36  foreach($iterator as $oid => $currentObject) {
37  $currentObject->acceptVisitor($this);
38  }
39  $this->doPostVisit();
40  }
41 
42  /**
43  * Start the visiting process by iterating over all elements of a given array.
44  * The visit() method is called by every visited object.
45  * @param $array An array holding references to the objects to visit.
46  */
47  public function startArray($array) {
48  $this->doPreVisit();
49  foreach($array as $currentObject) {
50  $currentObject->acceptVisitor($this);
51  }
52  $this->doPostVisit();
53  }
54 
55  /**
56  * Visit the current object in iteration.
57  * Subclasses of Visitor override this method to implement the specialized
58  * functionality.
59  * @param $obj PersistentObject instance
60  */
61  public abstract function visit($obj);
62 
63  /**
64  * Subclasses may override this method to do any operations before the visiting process here.
65  */
66  public function doPreVisit() {}
67 
68  /**
69  * Subclasses may override this method to do any operations after the visiting process here.
70  */
71  public function doPostVisit() {}
72 }
73 ?>
startIterator(NodeIterator $iterator)
Start the visiting process by iterating over all objects using the given NodeIterator.
Definition: Visitor.php:34
doPostVisit()
Subclasses may override this method to do any operations after the visiting process here.
Definition: Visitor.php:71
visit($obj)
Visit the current object in iteration.
Visitor is used to extend an object's functionality by not extending its interface.
Definition: Visitor.php:26
NodeIterator is used to iterate over a tree/list built of Nodes using a Depth-First-Algorithm.
doPreVisit()
Subclasses may override this method to do any operations before the visiting process here.
Definition: Visitor.php:66
startArray($array)
Start the visiting process by iterating over all elements of a given array.
Definition: Visitor.php:47