wCMF  3.6
 All Classes Namespaces Files Functions Variables Groups Pages
class.NodeProcessor.php
Go to the documentation of this file.
1 <?php
2 /**
3  * wCMF - wemove Content Management Framework
4  * Copyright (C) 2005-2014 wemove digital solutions GmbH
5  *
6  * Licensed under the terms of any of the following licenses
7  * at your choice:
8  *
9  * - GNU Lesser General Public License (LGPL)
10  * http://www.gnu.org/licenses/lgpl.html
11  * - Eclipse Public License (EPL)
12  * http://www.eclipse.org/org/documents/epl-v10.php
13  *
14  * See the license.txt file distributed with this work for
15  * additional information.
16  *
17  * $Id: class.NodeProcessor.php 1462 2014-02-04 23:52:27Z iherwig $
18  */
19 
20 /**
21  * @class NodeProcessor
22  * @ingroup Model
23  * @brief NodeProcessor is used to iterate over all values of a Node and
24  * apply a given callback function.
25  *
26  * @author ingo herwig <ingo@wemove.com>
27  */
29 {
30  var $_obj; // the object defining the callback
31  var $_callback; // the callback function to call on Node values
32  var $_params; // an array of paramters to pass to the callback
33  /**
34  * Constructor.
35  * @param callback The callback function to apply on Node values.
36  * @param params An array of paramters to pass to the callback (default: none).
37  * @param obj A reference to the object that defines the callback. If null the callback must
38  * be a global function (default: null).
39  *
40  * @note The callback function must have the following interface:
41  * callback(&$node, $valueName, $dataType, &$param0, &$param1, ...)
42  *
43  * usage example:
44  *
45  * @code
46  * // use NodeProcessor to iterate over all Node values
47  * // and call the global myCallback function on each
48  * $processor = new NodeProcessor('myCallback', array(&$result));
49  * $processor->run($node, false);
50  *
51  * // myCallback simply concats all node values to a result string
52  * function myCallback(&$node, $valueName, $dataType, &$result)
53  * {
54  * $value = $node->getValue($valueName, $dataType);
55  * // add the value to the result
56  * $result .= ' '.$value;
57  * }
58  * @endcode
59  */
60  function NodeProcessor($callback, $params=array(), $obj=null)
61  {
62  $this->_obj = &$obj;
63  $this->_callback = $callback;
64  $this->_params = &$params;
65  }
66  /**
67  * Run the processor.
68  * @param node The Node to start from.
69  * @param recursive True/False wether to process child Nodes or not (default: false).
70  */
71  function run(&$node, $recursive=false)
72  {
73  // iterate over all node values
74  foreach ($node->getDataTypes() as $dataType)
75  {
76  foreach ($node->getValueNames($dataType) as $valueName)
77  {
78  $params = array(&$node, $valueName, $dataType);
79  for($i=0; $i<sizeof($this->_params); $i++)
80  $params[sizeof($params)] = &$this->_params[$i];
81  // make params a reference instead of pass &$params
82  // (this avoids call-time pass-by-reference warning)
83  $ref = &$params;
84  if ($this->_obj === null)
85  call_user_func_array($this->_callback, $params);
86  else
87  call_user_method_array($this->_callback, $this->_obj, $params);
88  }
89  }
90  // recurse
91  if ($recursive)
92  {
93  $children = $node->getChildren();
94  for ($i=0; $i<sizeof($children); $i++)
95  $this->run($children[$i], $recursive);
96  }
97  }
98 }
99 ?>
NodeProcessor is used to iterate over all values of a Node and apply a given callback function...
run(&$node, $recursive=false)
NodeProcessor($callback, $params=array(), $obj=null)