From: <gem...@li...> - 2012-11-01 11:08:45
|
Revision: 1004 http://gemstracker.svn.sourceforge.net/gemstracker/?rev=1004&view=rev Author: matijsdejong Date: 2012-11-01 11:08:38 +0000 (Thu, 01 Nov 2012) Log Message: ----------- removed renderAny added documentation Modified Paths: -------------- trunk/library/classes/MUtil/Html/HtmlElement.php trunk/library/classes/MUtil/Html/Renderer.php trunk/library/classes/MUtil/Html.php Modified: trunk/library/classes/MUtil/Html/HtmlElement.php =================================================================== --- trunk/library/classes/MUtil/Html/HtmlElement.php 2012-11-01 09:38:21 UTC (rev 1003) +++ trunk/library/classes/MUtil/Html/HtmlElement.php 2012-11-01 11:08:38 UTC (rev 1004) @@ -1062,14 +1062,14 @@ if ($this->_repeater->__start()) { $html = null; while ($this->_repeater->__next()) { - $html .= implode('', $renderer->renderAny($view, $this->_content)); + $html .= $renderer->renderAny($view, $this->_content); } return $html; } } elseif ($content = $renderer->renderAny($view, $this->_content)) { - return implode('', $content); + return $content; } } Modified: trunk/library/classes/MUtil/Html/Renderer.php =================================================================== --- trunk/library/classes/MUtil/Html/Renderer.php 2012-11-01 09:38:21 UTC (rev 1003) +++ trunk/library/classes/MUtil/Html/Renderer.php 2012-11-01 11:08:38 UTC (rev 1004) @@ -1,10 +1,9 @@ <?php - /** * Copyright (c) 2011, Erasmus MC * All rights reserved. - * + * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * Redistributions of source code must retain the above copyright @@ -15,7 +14,7 @@ * * Neither the name of Erasmus MC nor the * names of its contributors may be used to endorse or promote products * derived from this software without specific prior written permission. - * + * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE @@ -26,25 +25,50 @@ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * @package MUtil + * @subpackage Html + * @author Matijs de Jong <mj...@ma...> + * @copyright Copyright (c) 2011 Erasmus MC + * @license New BSD License + * @version $id: Renderer.php 362 2011-12-15 17:21:17Z matijsdejong $ */ /** - * @author Matijs de Jong - * @since 1.0 - * @version 1.1 - * @package MUtil + * Render output for a view. + * + * This object handles MUtil_Html_HtmlInterface and MUtil_Lazy_LazyInterface + * objects natively, as well as array, scalar values and objects with a + * __toString function. + * + * All other object types passed to the renderer should have a render function + * defined for them in the ClassRenderList. + * + * @package MUtil * @subpackage Html + * @copyright Copyright (c) 2011 Erasmus MC + * @license New BSD License + * @since Class available since version 1.0 */ - class MUtil_Html_Renderer { + /** + * + * @var MUtil_Util_ClassList + */ protected $_classRenderFunctions; - // The MUtil_Html_Renderer::doNotRender items allow you to pass these - // items to a content without triggering error messages. - // - // This is usefull if you want to pass an item but are not sure that - // it will be used in very case. + /** + * Default array of objects rendering functions. + * + * The MUtil_Html_Renderer::doNotRender function allows items to be passed + * as content without triggering error messages. + * + * This is usefull if you want to pass an item to sub objects, but are not + * sure that it will be used in every case. + * + * @var array classname => static output function + */ protected $_initialClassRenderFunctions = array( 'Zend_Db_Adapter_Abstract' => 'MUtil_Html_Renderer::doNotRender', 'Zend_Controller_Request_Abstract' => 'MUtil_Html_Renderer::doNotRender', @@ -54,11 +78,23 @@ 'Zend_Translate' => 'MUtil_Html_Renderer::doNotRender', ); + /** + * Create the renderer + * + * @param mixed $classRenderFunctions Array of classname => renderFunction or MUtil_Util_ClassList + * @param boolean $append Replace when false, append to default definitions otherwise + */ public function __construct($classRenderFunctions = null, $append = true) { $this->setClassRenderList($classRenderFunctions, $append); } + /** + * Check if the value can be rendered by this object + * + * @param mixed $value + * @return boolean True when the object can be rendered + */ public function canRender($value) { if (is_object($value)) { @@ -83,6 +119,16 @@ } } + /** + * Static helper function used for object types that should + * not produce output when (accidently) rendered. + * + * Eg Zend_Translate or Zend_Db_Adapter_Abstract + * + * @param Zend_View_Abstract $view + * @param mixed $content + * @return null + */ public static function doNotRender(Zend_View_Abstract $view, $content) { if (MUtil_Html::$verbose) { @@ -91,11 +137,33 @@ return null; } + /** + * Get the classlist containing render functions for non-builtin objects + * + * @return MUtil_Util_ClassList + */ public function getClassRenderList() { return $this->_classRenderFunctions; } + /** + * 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 renderAny(Zend_View_Abstract $view, $content) { // Resolve first as this function as recursion heavy enough as it is. @@ -111,14 +179,10 @@ // Again, skip on the recursion count foreach ($content as $key => $item) { - $new_content[$key] = $this->renderAny($view, $item); - // MUtil_Echo::r($key . '=>' . $new_content[$key]); - if (null === $new_content[$key]) { - unset($new_content[$key]); - } + $new_content[] = $this->renderAny($view, $item); } - return $new_content; + return implode('', $new_content); } else { if (is_object($content)) { @@ -141,25 +205,20 @@ return $new_content; - } elseif (! is_array($content)) { - return $content; // Returns 0 (zero) and '' when that is the value of $content } - } - public function renderArray(Zend_View_Abstract $view, array $content) - { - if ($content) { - foreach ($content as $key => $item) { - $content[$key] = $this->renderAny($view, $item); - if (null === $content[$key]) { - unset($content[$key]); - } - } - - return $content; + if (! is_array($content)) { // Skip empty array + return $content; // Returns 0 (zero) and '' when that is the value of $content } } + /** + * Change the list of non-builtin objects that can be rendered by this renderer. + * + * @param mixed $classRenderFunctions Array of classname => renderFunction or MUtil_Util_ClassList + * @param boolean $append Replace when false, append otherwise + * @return MUtil_Html_Renderer (continuation pattern) + */ public function setClassRenderList($classRenderFunctions = null, $append = false) { if ($classRenderFunctions instanceof MUtil_Util_ClassList) { Modified: trunk/library/classes/MUtil/Html.php =================================================================== --- trunk/library/classes/MUtil/Html.php 2012-11-01 09:38:21 UTC (rev 1003) +++ trunk/library/classes/MUtil/Html.php 2012-11-01 11:08:38 UTC (rev 1004) @@ -98,6 +98,12 @@ return self::getCreator()->createAttribute($attributeName, $args); } + /** + * Check if the value can be rendered by the default renderer + * + * @param mixed $value + * @return boolean True when the object can be rendered + */ public static function canRender($value) { return self::getRenderer()->canRender($value); @@ -235,16 +241,19 @@ return self::getCreator()->create('raw', array($content)); } + /** + * Renders the $content so that it can be used as output for the $view, + * including output escaping and encoding correction. + * + * @param Zend_View_Abstract $view + * @param mixed $content Anything number, string, array, Lazy, HtmlInterface, object with __toString + * @return string Output to echo to the user + */ public static function renderAny(Zend_View_Abstract $view, $content) { return self::getRenderer()->renderAny($view, $content); } - public static function renderArray(Zend_View_Abstract $view, array $content) - { - return self::getRenderer()->renderArray($view, $content); - } - public static function renderNew(Zend_View_Abstract $view, $tagName, $arg_array = null) { $args = array_slice(func_get_args(), 2); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |