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,v 1.6 2003/11/25 06:35:43 alan_k Exp $
  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.     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.     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.     * @param   array   (return by referncekey(tag name) => HTML_Element
  99.     * @param    array   key(tag name) => error
  100.     * @param    string sprintf error format..
  101.     *
  102.     * @return   array    Array of HTML_Elements
  103.     * @access   public
  104.     */
  105.   
  106.     function setErrors($ret,$set,$format='<span class="error">%s</span>'{
  107.         // check what you send this.. !!!
  108.         if (!is_array($set)) {
  109.             PEAR::raiseError(
  110.                 'invalid arguments "$set" (should be an array) sent to HTML_Template_Flexy_Factory::setErrors'
  111.                 0PEAR_ERROR_DIE);
  112.         }
  113.         foreach($set as $k=>$v{
  114.             if (!$v{
  115.                 continue;
  116.             }
  117.             if (!isset($ret[$k])) {
  118.                 $ret[$k= new HTML_Template_Flexy_Element;
  119.             }
  120.             $ret[$k]->suffix .= sprintf($format,$v);
  121.         }
  122.         return $ret;
  123.     }
  124.     
  125.     
  126.     /**
  127.     * setRequired - sets the prefix of an element to a value..
  128.     *  
  129.     * HTML_Element_Factory::setRequired($elements,array('name',true));
  130.     * 
  131.     * @param   array   (return by referncekey(tag name) => HTML_Element
  132.     * @param    array   key(tag name) => error
  133.     * @param    string sprintf error format..
  134.     *
  135.     * @return   array    Array of HTML_Elements
  136.     * @access   public
  137.     */
  138.   
  139.     function setRequired($ret,$set,$format='<span class="required">*</span>'{
  140.         
  141.         
  142.         $ret = array();
  143.         foreach($set as $k=>$v{
  144.             if (!$v{
  145.                 continue;
  146.             }
  147.             if (!isset($ret[$k])) {
  148.                 $ret[$k= new HTML_Template_Flexy_Element();
  149.             }
  150.             $ret[$k]->prefix .= sprintf($format,$v);
  151.         }
  152.         return $ret;
  153.     }
  154.     
  155.     
  156.     /**
  157.     * freeze - freeze's an element. - just copies the value to the override.
  158.     * this probably needs more thought.. - it would probably need to merge
  159.     * the full tag info with types, to be usefull..
  160.     *  
  161.     * $ar = HTML_Element_Factory::freeze($ar);
  162.     *
  163.     * @param   array   (return by referncekey(tag name) => HTML_Element
  164.     *
  165.     * @return   array    Array of HTML_Elements
  166.     * @access   public
  167.     */
  168.     function freeze($array{
  169.     
  170.         foreach($array as $k=>$v{
  171.             $array[$k]->override = $array[$k]->value;
  172.         }
  173.     }
  174.     
  175.  
  176. }
  177.  
  178. ?>

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