HTML_Template_Flexy
[ class tree: HTML_Template_Flexy ] [ index: HTML_Template_Flexy ] [ all elements ]

Source for file Factory.php

Documentation is available at Factory.php

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4: */
  3. // +----------------------------------------------------------------------+
  4. // | PHP Version 4                                                        |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2002 The PHP Group                                |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 2.02 of the PHP license,      |
  9. // | that is bundled with this package in the file LICENSE, and is        |
  10. // | available at through the world-wide-web at                           |
  11. // | http://www.php.net/license/2_02.txt.                                 |
  12. // | If you did not receive a copy of the PHP license and are unable to   |
  13. // | obtain it through the world-wide-web, please send a note to          |
  14. // | license@php.net so we can mail you a copy immediately.               |
  15. // +----------------------------------------------------------------------+
  16. // | Authors:  nobody <nobody@localhost>                                  |
  17. // +----------------------------------------------------------------------+
  18. //
  19. // $Id: Factory.php 334846 2014-09-12 04:50:56Z alan_k $
  20. //
  21. //  Factory tools for managing groups of HTML_Elements
  22. //
  23.  
  24. require_once 'HTML/Template/Flexy/Element.php';
  25.  
  26. class HTML_Template_Flexy_Factory {
  27.  
  28.  
  29.     /**
  30.     * fromArray - builds a set of elements from a key=>value array (eg. DO->toArray())
  31.     * the second parameter is an optional HTML_Element array to merge it into.
  32.     * 
  33.     * 
  34.     * @param   array   key(tag name) => value
  35.     * @param   optional array   key(tag name) => HTML_Element
  36.     *
  37.     * @return   array    Array of HTML_Elements
  38.     * @access   public
  39.     */
  40.   
  41.     static function fromArray($ar,$ret=array()) 
  42.     {
  43.         
  44.         foreach($ar as $k=>$v{
  45.             if (is_array($v)) {
  46.                 $ret = HTML_Template_Flexy_Factory::fromArrayPrefixed($k,$v,$ret);
  47.             }
  48.             
  49.             
  50.             if (!isset($ret[$k])) {
  51.                 $ret[$k= new HTML_Template_Flexy_Element();
  52.             }
  53.             $ret[$k]->setValue($v);
  54.         }
  55.         return $ret;
  56.     }
  57.     
  58.     /**
  59.     * fromArrayPrefixed - takes a multi dimensional array, and builds the 'xxx[sss][xx]' => value
  60.     * 
  61.     * @param   array   key(tag name) => value
  62.     * @param   array   key(tag name) => value
  63.     * @param   optional array   key(tag name) => HTML_Element
  64.     *
  65.     * @return   array    Array of HTML_Elements
  66.     * @access   public
  67.     */
  68.   
  69.     static  function fromArrayPrefixed($prefix$ar,$ret=array()) 
  70.     {
  71.       
  72.         foreach($ar as $k=>$v{
  73.             if (is_array($v)) {
  74.                 $ret = HTML_Template_Flexy_Factory::fromArrayPrefixed($prefix.'['.$k.']',$v,$ret);
  75.                 if (!isset($ret[$prefix.'['.$k.'][]'])) {
  76.                     $ret[$prefix.'['.$k.'][]'= new HTML_Template_Flexy_Element();
  77.                 }
  78.                 $ret[$prefix.'['.$k.'][]']->setValue($v);
  79.             }
  80.             
  81.             if (!isset($ret[$prefix.'['.$k.']'])) {
  82.                 $ret[$prefix.'['.$k.']'= new HTML_Template_Flexy_Element();
  83.             }
  84.             $ret[$prefix.'['.$k.']']->setValue($v);
  85.             
  86.             
  87.             
  88.         }
  89.         return $ret;
  90.     }
  91.     
  92.     
  93.     /**
  94.     * setErrors - sets the suffix of an element to a value..
  95.     *  
  96.     * HTML_Element_Factory::setErrors($elements,array('name','not long enough'));
  97.     *
  98.     * @depreciated  - this is really outside the scope of Factory - it should be
  99.     *                    seperated into a rendering toolkit of some kind.
  100.  
  101.     * @param   array    of HTML_Element's
  102.     * @param    array   key(tag name) => error
  103.     * @param    string sprintf error format..
  104.     *
  105.     * @return   array    Array of HTML_Elements
  106.     * @access   public
  107.     */
  108.   
  109.     static function &setErrors(&$ret,$set,$format='<span class="error">%s</span>'
  110.     {
  111.         if (empty($ret|| !is_array($ret)) {
  112.             $ret = array();
  113.         }
  114.         // check what you send this.. !!!
  115.         if (!is_array($set)) {
  116.             return HTML_Template_Flexy::staticRaiseError(
  117.                 'invalid arguments "$set" (should be an array) sent to HTML_Template_Flexy_Factory::setErrors'
  118.                 0HTML_TEMPLATE_FLEXY_ERROR_DIE);
  119.         }
  120.         foreach($set as $k=>$v{
  121.             if (!$v{
  122.                 continue;
  123.             }
  124.             if (!isset($ret[$k])) {
  125.                 $ret[$k= new HTML_Template_Flexy_Element;
  126.             }
  127.             $ret[$k]->suffix .= sprintf($format,$v);
  128.         }
  129.         return $ret;
  130.     }
  131.     
  132.     
  133.     /**
  134.     * setRequired - sets the prefix of an element to a value..
  135.     *  
  136.     * HTML_Element_Factory::setRequired($elements,array('name',true));
  137.     *
  138.     * @depreciated  - this is really outside the scope of Factory - it should be
  139.     *                seperated into a rendering toolkit
  140.     * 
  141.     * @param    array   of HTML_Element's
  142.     * @param    array  key(tag name) => error
  143.     * @param    string sprintf error format..
  144.     *
  145.     *
  146.     * @return   array    Array of HTML_Elements
  147.     * @access   public
  148.     */
  149.   
  150.     static function &setRequired(&$ret,$set,$format='<span class="required">*</span>'
  151.     {
  152.         
  153.         
  154.         if (empty($ret|| !is_array($ret)) {
  155.             $ret = array();
  156.         }
  157.         foreach($set as $k=>$v{
  158.             if (!$v{
  159.                 continue;
  160.             }
  161.             if (!isset($ret[$k])) {
  162.                 $ret[$k= new HTML_Template_Flexy_Element();
  163.             }
  164.             $ret[$k]->prefix .= sprintf($format,$v);
  165.         }
  166.         return $ret;
  167.     }
  168.     
  169.     
  170.     /**
  171.     * freeze - freeze's an element. - just copies the value to the override.
  172.     * this probably needs more thought.. - it would probably need to merge
  173.     * the full tag info with types, to be usefull..
  174.     *  
  175.     * $ar = HTML_Element_Factory::freeze($ar);
  176.     *
  177.     * @depreciated  - this is really outside the scope of Factory - it should be
  178.     *                seperated into a rendering toolkit
  179.     * 
  180.     *
  181.     * @param   array   (return by referencekey(tag name) => HTML_Element
  182.     *
  183.     * @return   array    Array of HTML_Elements
  184.     * @access   public
  185.     */
  186.     static function freeze(&$array{
  187.     
  188.         foreach($array as $k=>$v{
  189.             $array[$k]->override = $array[$k]->value;
  190.         }
  191.     }
  192.     
  193.  
  194. }

Documentation generated on Mon, 11 Mar 2019 15:59:56 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.