Esempio n. 1
0
 /**
  * Renders the $content so that it can be used as output for the $view,
  * including output escaping and encoding correction.
  *
  * This functions handles \MUtil_Html_HtmlInterface and \MUtil_Lazy_LazyInterface
  * objects natively, as well as array, scalar values and objects with a
  * __toString function.
  *
  * Other objects a definition should have a render function in getClassRenderList().
  *
  * All Lazy variabables are raised.
  *
  * @param \Zend_View_Abstract $view
  * @param mixed $content Anything HtmlInterface, number, string, array, object with __toString
  *                      or an object that has a defined render function in getClassRenderList().
  * @return string Output to echo to the user
  */
 public function renderArray(\Zend_View_Abstract $view, $content, $glue = '', $stack = null)
 {
     // \MUtil_Echo::timeFunctionStart(__FUNCTION__);
     $output = array();
     // \MUtil_Echo::countOccurences('render');
     foreach ($content as $key => $value) {
         // Resolve first as this function as recursion heavy enough as it is.
         if ($value instanceof \MUtil_Lazy_LazyInterface) {
             if (!$stack) {
                 $stack = \MUtil_Lazy::getStack();
             }
             // \MUtil_Echo::countOccurences('lazyIf');
             while ($value instanceof \MUtil_Lazy_LazyInterface) {
                 // \MUtil_Echo::countOccurences('lazyWhile');
                 $value = $value->__toValue($stack);
             }
         }
         if (is_scalar($value)) {
             // \MUtil_Echo::countOccurences('scalar');
             // \MUtil_Echo::timeFunctionStart('escape2');
             $output[$key] = $view->escape((string) $value);
             // \MUtil_Echo::timeFunctionStop('escape2');
         } elseif ($value instanceof \MUtil_Html_HtmlInterface) {
             // \MUtil_Echo::countOccurences('interface');
             $output[$key] = $value->render($view);
         } elseif (null === $value) {
             // \MUtil_Echo::countOccurences('null');
         } elseif (is_array($value)) {
             // \MUtil_Echo::countOccurences('array');
             $output[$key] = self::renderAny($view, $value, '', $stack);
         } elseif (is_object($value)) {
             $function = $this->_classRenderFunctions->get($value);
             if ($function) {
                 // \MUtil_Echo::countOccurences('function');
                 $output[$key] = call_user_func($function, $view, $value);
             } elseif (method_exists($value, '__toString')) {
                 // \MUtil_Echo::countOccurences('toString');
                 // \MUtil_Echo::countOccurences('toString.' . get_class($value));
                 $output[$key] = $view->escape($value->__toString());
             } else {
                 // $output[$key] = 'WARNING: Object of type ' . get_class($value) . ' cannot be converted to string.';
                 throw new \MUtil_Html_HtmlException('WARNING: Object of type ' . get_class($value) . ' cannot be converted to string.');
             }
         } elseif ($value instanceof \__PHP_Incomplete_Class) {
             \MUtil_Echo::r($value, __CLASS__ . '->' . __FUNCTION__);
             $output[$key] = '';
         } else {
             // Mop up, should not occur
             // \MUtil_Echo::countOccurences('scalar else');
             $output[$key] = $view->escape((string) $value);
         }
     }
     if (false === $glue || null === $glue) {
         // \MUtil_Echo::timeFunctionStop(__FUNCTION__);
         return $output;
     }
     $output = implode($glue, $output);
     // \MUtil_Echo::timeFunctionStop(__FUNCTION__);
     return $output;
 }