/lib/pear/HTML/Template/Flexy/Assign.php

https://bitbucket.org/blackriver/openx · PHP · 204 lines · 48 code · 28 blank · 128 comment · 12 complexity · a1a3ee8748c659d657b302a8c856ff8f MD5 · raw file

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