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

Source for file Assign.php

Documentation is available at Assign.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: Assign.php 334846 2014-09-12 04:50:56Z alan_k $
  20. //
  21. //  Provider for Assign API ( Eg. $flexy->assign(...) )
  22. //
  23.  
  24. define('HTML_TEMPLATE_FLEXY_ASSIGN_ERROR_INVALIDARGS'-100);
  25.  
  26. class HTML_Template_Flexy_Assign {
  27.     
  28.     /**
  29.     * The variables stored in the Assigner
  30.     *
  31.     * @var array 
  32.     * @access public
  33.     */
  34.     var $variables = array();
  35.     /**
  36.     * The references stored in the Assigner
  37.     *
  38.     * @var array 
  39.     * @access public
  40.     */
  41.     var $references = array();
  42.  
  43.  
  44.     /**
  45.     * 
  46.     * Assigns a token-name and value to $this->_token_vars for use in a
  47.     * template.
  48.     * 
  49.     * There are three valid ways to assign values to a template.
  50.     * 
  51.     * Form 1: $args[0] is a string and $args[1] is mixed. This means
  52.     * $args[0] is a token name and $args[1] is the token value (which
  53.     * allows objects, arrays, strings, numbers, or anything else).
  54.     * $args[1] can be null, which means the corresponding token value in
  55.     * the template will also be null.
  56.     * 
  57.     * Form 2: $args[0] is an array and $args[1] is not set. Assign a
  58.     * series of tokens where the key is the token name, and the value is
  59.     * token value.
  60.     * 
  61.     * Form 3: $args[0] is an object and $args[1] is not set.  Assigns
  62.     * copies of all object variables (properties) to tokens; the token
  63.     * name and value is a copy of each object property and value.
  64.     * 
  65.     * @access public
  66.     * 
  67.     * @param string|array|object $args[0] This param can be a string, an
  68.     *  array, or an object.  If $args[0] is a string, it is the name of a
  69.     *  variable in the template.  If $args[0] is an array, it must be an
  70.     *  associative array of key-value pairs where the key is a variable
  71.     *  name in the template and the value is the value for that variable
  72.     *  in the template.  If $args[0] is an object, copies of its
  73.     *  properties will be assigned to the template.
  74.     * 
  75.     * @param mixed $args[1] If $args[0] is an array or object, $args[1]
  76.     *  should not be set.  Otherwise, a copy of $args[1] is assigned to a
  77.     *  template variable named after $args[0].
  78.     * 
  79.     * @return bool|PEAR_ErrorBoolean true if all assignments were
  80.     *  committed, or a PEAR_Error object if there was an error.
  81.     * 
  82.     * @throws SAVANT_ERROR_ASSIGN Unknown reason for error, probably
  83.     *  because you passed $args[1] when $args[0] is an array or object.
  84.     * 
  85.     * @author Paul M. Jones <pmjones@ciaweb.net>
  86.     * @see assignRef()
  87.     * 
  88.     * @see assignObject()
  89.     * 
  90.     */
  91.     
  92.     function assign($args)
  93.     {    
  94.         // in Form 1, $args[0] is a string name and $args[1] is mixed.
  95.         // in Form 2, $args[0] is an associative array.
  96.         // in Form 3, $args[0] is an object.
  97.         
  98.         $count count($args);
  99.         
  100.         // -------------------------------------------------------------
  101.         //
  102.         // Now we assign variable copies.
  103.         //
  104.         
  105.         // form 1 (string name and mixed value)
  106.         // don't check isset() on $args[1] becuase a 'null' is not set,
  107.         // and we might want to pass a null.
  108.         if (is_string($args[0]&& $count > 1{
  109.             if (isset($this->references[$args[0]])) {
  110.                 unset($this->references[$args[0]]);
  111.             }
  112.             // keep a copy in the token vars array
  113.             $this->variables[$args[0]] $args[1];
  114.             
  115.             // done!
  116.             return true;
  117.         }
  118.         
  119.         // form 2 (assoc array)
  120.         if (is_array($args[0]&& $count == 1{
  121.             
  122.             foreach ($args[0as $key=>$val{
  123.                 $this->assign(array($key$val));
  124.             }
  125.             
  126.             // done!
  127.             return true;
  128.         }
  129.         
  130.         // form 3 (object props)
  131.         if (is_object($args[0]&& $count == 1{
  132.             
  133.             // get the object properties
  134.             $data get_object_vars($args[0]);
  135.             foreach ($data as $key=>$val{
  136.                 $this->assign(array($key$val));
  137.             }
  138.             
  139.             // done!
  140.             return true;
  141.         }
  142.         
  143.         
  144.         // -------------------------------------------------------------
  145.         //
  146.         // Final error catch.  We should not have gotten to this point.
  147.         //
  148.         
  149.         return HTML_Template_Flexy::staticRaiseError(
  150.             "invalid type sent to assign, "print_r($args,true),
  151.             HTML_TEMPLATE_FLEXY_ASSIGN_ERROR_INVALIDARGS             
  152.         );
  153.     }
  154.      
  155.  
  156.     /**
  157.     * 
  158.     * Assign a token by reference.  This allows you change variable
  159.     * values within the template and have those changes reflected back
  160.     * at the calling logic script.  Works as with form 2 of assign().
  161.     * 
  162.     * @access public
  163.     * 
  164.     * @param string $name The template token-name for the reference.
  165.     * 
  166.     * @param mixed &$ref The variable passed by-reference.
  167.     * 
  168.     * @return bool|PEAR_ErrorBoolean true on success, or a PEAR_Error
  169.     *  on failure.
  170.     * 
  171.     * @throws SAVANT_ERROR_ASSIGN_REF Unknown reason for error.
  172.     * 
  173.     * @see assign()
  174.     * @author Paul M. Jones <pmjones@ciaweb.net>
  175.     * @see assignObject()
  176.     * 
  177.     */
  178.     
  179.     function assignRef($name&$ref)
  180.     {
  181.         // look for the proper case: name and variable
  182.         if (is_string($name&& isset($ref)) {
  183.             if (isset($this->variables[$name])) {
  184.                 unset($this->variables[$name]);
  185.             }
  186.             //
  187.             // assign the token as a reference
  188.             $this->references[$name=$ref;
  189.              
  190.             // done!
  191.             return true;
  192.         }
  193.         
  194.         // final error catch
  195.         return HTML_Template_Flexy::staticRaiseError(
  196.             "invalid type sent to assignRef, "print_r($name,true),
  197.  
  198.         );
  199.     }
  200.     
  201.  
  202. }

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