All pastes #2090068 Raw Edit

Dmitri Pisarev

public php v1 · immutable
#2090068 ·published 2011-10-14 14:09 UTC
rendered paste body
<?phpclass Tx_Rmap_ViewHelpers_GroupedForViewHelper extends Tx_Fluid_ViewHelpers_GroupedForViewHelper {	/**	 * Groups the given array by the specified groupBy property.	 *	 * @param array $elements The array / traversable object to be grouped	 * @param string $groupBy Group by this property	 * @return array The grouped array in the form array('keys' => array('key1' => [key1value], 'key2' => [key2value], ...), 'values' => array('key1' => array([key1value] => [element1]), ...), ...)	 * @author Bastian Waidelich <bastian@typo3.org>	 */	protected function groupElements(array $elements, $groupBy) {		$groups = array('keys' => array(), 'values' => array());		foreach ($elements as $key => $value) {			if (is_array($value)) {				$currentGroupIndexs = isset($value[$groupBy]) ? $value[$groupBy] : NULL;			} elseif (is_object($value)) {				$currentGroupIndexs = Tx_Extbase_Reflection_ObjectAccess::getProperty($value, $groupBy);			} else {				throw new Tx_Fluid_Core_ViewHelper_Exception('GroupedForViewHelper only supports multi-dimensional arrays and objects', 1253120365);			}			if ($currentGroupIndexs instanceof Traversable) {				$currentGroupIndexs = iterator_to_array($currentGroupIndexs);			}			if (is_array($currentGroupIndexs)) {				foreach ($currentGroupIndexs as $cGI => $currentGroupIndex) {					$currentGroupKeyValue = $currentGroupIndex;					if (is_object($currentGroupIndex)) {						$currentGroupIndex = spl_object_hash($currentGroupIndex);					}					$groups['keys'][$currentGroupIndex] = $currentGroupKeyValue;					$groups['values'][$currentGroupIndex][$key] = $value;				}			} else {				$currentGroupIndex = $currentGroupIndexs;				$currentGroupKeyValue = $currentGroupIndex;				if (is_object($currentGroupIndex)) {					$currentGroupIndex = spl_object_hash($currentGroupIndex);				}				$groups['keys'][$currentGroupIndex] = $currentGroupKeyValue;				$groups['values'][$currentGroupIndex][$key] = $value;			}		}		return $groups;	}}?>