DUF: Declarative Ugly Forms  v0.1-94-g0b7957c
 All Classes Functions Variables Pages
Static Public Member Functions | List of all members
CollectionWalker Class Reference

Helper to walk multi-dimensional collections. More...

Static Public Member Functions

static walkCollection ($collection, $dimension, $render_function, $on_enter=null, $on_leave=null, $depth=0, &$collection_key=null)
 Walk multi-dimensional collection. More...
 
static walkCollectionWithTarget ($collection, &$target, $dimension, $render_function, $on_enter=null, $on_leave=null, $depth=0, &$collection_key=null)
 Walk multi-dimensional collection and keep reference to parallel collection. More...
 

Detailed Description

Helper to walk multi-dimensional collections.

Member Function Documentation

static walkCollection (   $collection,
  $dimension,
  $render_function,
  $on_enter = null,
  $on_leave = null,
  $depth = 0,
$collection_key = null 
)
static

Walk multi-dimensional collection.

This method is designed for use with anonymous functions with binded variables (use keyword). Other way would be to pass variables via method arguments and it means to specify them three times, which is not very practical.

Trees are not supported. But if tree nodes are given in list, it is possible to indent them manually.

Note
Do not forget to set Form collection key before rendering any child widgets in $render_function.
Example
To walk the collection from your IWidgetRenderer::renderWidget() method, use code like this:
$group_id = $widget_conf['group_id'];
$group = $form->getFieldGroup($group_id);
self::walkCollection($form->getRawData($group_id), $group['collection_dimensions'],
function($collection_key, $item) use ($form, $template_engine, $group_id, $widget_conf) {
$form->setCollectionKey($group_id, $collection_key);
$form->renderWidgets($template_engine, $widget_conf['widgets']);
},
function($depth) use () {
echo "<div>\n";
},
function($depth) use () {
echo "</div>\n";
});
$form->unsetCollectionKey($group_id);
See also
Form::getRawData(), Form::setCollectionKey(), Form::unsetCollectionKey()
Parameters
$collectionis the collection to walk.
$dimensionis amount of dimensions to traverse (0 = single item, 1 = list, 2 = matrix, ...).
$render_functionis function($collection_key, $item) called for each item in collection.
$on_enteris function($depth) called when going deeper.
$on_leaveis function($depth) called when going back.
$depthis for internal use, leave it unspecified.
$collection_keyis for internal use, leave it unspecified.

Here is the caller graph for this function:

static walkCollectionWithTarget (   $collection,
$target,
  $dimension,
  $render_function,
  $on_enter = null,
  $on_leave = null,
  $depth = 0,
$collection_key = null 
)
static

Walk multi-dimensional collection and keep reference to parallel collection.

Just like walkCollection(), but $target parameter is walked in parallel, which means the $target_item reference points to the same position as $item, but in $target structure.

Parameters
$collectionis the collection to walk.
$dimensionis amount of dimensions to traverse (0 = single item, 1 = list, 2 = matrix, ...).
$render_functionis function($collection_key, $item, $target_item) called for each item in collection.
$on_enteris function($depth) called when going deeper.
$on_leaveis function($depth) called when going back.
$depthis for internal use, leave it unspecified.
$collection_keyis for internal use, leave it unspecified.
See also
walkCollection()