Source for file TypeAdapter.php

Documentation is available at TypeAdapter.php

  1. <?php
  2. /**
  3. * FIT TypeAdapter
  4. *
  5. * $Id$
  6. *
  7. * @author Luis A. Floreani <luis.floreani@gmail.com>
  8. * @author gERD Schaufelberger <gerd@php-tools.net>
  9. * @package FIT
  10. * @subpackage FileRunner
  11. * @license LGPL http://www.gnu.org/copyleft/lesser.html
  12. * @copyright Copyright (c) 2002-2005 Cunningham & Cunningham, Inc.
  13. */
  14.  
  15.  
  16. /**
  17. * FIT TypeAdapter
  18. *
  19. * The type adapter makes it possible to "cast" from HTML input into PHP variables.
  20. * Even if PHP is sort of type-free and does automatic casts, this class makes it
  21. * possible to check and validate variables including their types.
  22. *
  23. * @version 0.1.0
  24. * @package FIT
  25. * @subpackage FileRunner
  26. */
  27. class PHPFIT_TypeAdapter
  28. {
  29. /**
  30. * target fixture object
  31. * @var object
  32. */
  33. public $target;
  34. /**
  35. * fixture object
  36. * @var object
  37. */
  38. public $fixture;
  39. /**
  40. * named field
  41. * @var string
  42. */
  43. public $field;
  44. /**
  45. * named method
  46. * @var string
  47. */
  48. public $method;
  49. /**
  50. * adapter class
  51. * @var string
  52. */
  53. public $type = null;
  54.  
  55. /**
  56. * Create adapter for fixture's method
  57. *
  58. * @param Fixture target
  59. * @param string variable name
  60. */
  61. public static function onMethod( $fixture, $name )
  62. {
  63. $type = $fixture->getType( $name, true );
  64. $adapter = self::on( $fixture, $type );
  65. $adapter->method = $name;
  66. return $adapter;
  67. }
  68.  
  69. /**
  70. * Create adapter for fixture's field
  71. *
  72. * @param Fixture target
  73. * @param string variable name
  74. */
  75. public static function onField( $fixture, $name )
  76. {
  77. $type = $fixture->getType( $name );
  78. $adapter = self::on( $fixture, $type );
  79. $adapter->field = $name;
  80. return $adapter;
  81. }
  82.  
  83. /**
  84. * Create generic adapter for fixture
  85. *
  86. * @param Fixture target
  87. * @param string type of variables
  88. */
  89. public static function on( $fixture, $type ) {
  90. $adapter = self::adapterFor( $type );
  91. $adapter->init( $fixture, $type );
  92. $adapter->target = $fixture;
  93. return $adapter;
  94. }
  95.  
  96. /**
  97. * auxiliary function to include requested adapter
  98. *
  99. * @param string $type
  100. * @return object an instance of PHPFIT_TypeAdapter
  101. */
  102. private static function loadAdapter( $name )
  103. {
  104. // already loaded
  105. if( class_exists( 'PHPFIT_TypeAdapter_' . $name ) ) {
  106. return true;
  107. }
  108. return include_once 'PHPFIT/TypeAdapter/' . $name . '.php';
  109. }
  110.  
  111. /**
  112. * load actual adaptor for a specified type
  113. *
  114. * @param string $type
  115. * @return object an instance of PHPFIT_TypeAdapter
  116. */
  117. public static function adapterFor( $type )
  118. {
  119. if( self::is_bool( $type ) ) {
  120. self::loadAdapter( 'Boolean' );
  121. return new PHPFIT_TypeAdapter_Boolean();
  122. }
  123. if( self::is_int( $type ) ) {
  124. self::loadAdapter( 'Integer' );
  125. return new PHPFIT_TypeAdapter_Integer();
  126. }
  127. if( self::is_double( $type ) ) {
  128. self::loadAdapter( 'Double' );
  129. return new PHPFIT_TypeAdapter_Double();
  130. }
  131. if( self::is_string( $type ) ) {
  132. self::loadAdapter( 'String' );
  133. return new PHPFIT_TypeAdapter_String();
  134. }
  135. return new PHPFIT_TypeAdapter();
  136. }
  137.  
  138. /**
  139. * check for adapter type: boolean
  140. *
  141. * @param string $type
  142. * @return true if type matches, false otherwise
  143. */
  144. public static function is_bool( $type )
  145. {
  146. if( $type == 'boolean' || $type == 'bool' ) {
  147. return true;
  148. }
  149. return false;
  150. }
  151. /**
  152. * check for adapter type: integer
  153. *
  154. * @param string $type
  155. * @return true if type matches, false otherwise
  156. */
  157. public static function is_int($type) {
  158. if( $type == 'integer' || $type == 'int' ) {
  159. return true;
  160. }
  161. return false;
  162. }
  163. /**
  164. * check for adapter type: double
  165. *
  166. * @param string $type
  167. * @return true if type matches, false otherwise
  168. */
  169. public static function is_double( $type )
  170. {
  171. return $type == 'double';
  172. }
  173. /**
  174. * check for adapter type: string
  175. *
  176. * @param string $type
  177. * @return true if type matches, false otherwise
  178. */
  179. public static function is_string($type)
  180. {
  181. return $type == 'string';
  182. }
  183.  
  184. public function init( $fixture, $type )
  185. {
  186. $this->fixture = $fixture;
  187. $this->type = $type;
  188. }
  189.  
  190. public function set( $value )
  191. {
  192. $r = new ReflectionClass($this->target);
  193. $prop = $r->getProperty($this->field);
  194. $prop->setValue($this->target, $value);
  195. }
  196.  
  197. public function get() {
  198. if ($this->field != null) {
  199. if ($this->field instanceof self)
  200. return $this->field->get($this->target);
  201. return "";
  202. }
  203.  
  204. if ($this->method != null) {
  205. $sal = $this->invoke();
  206. return $sal;
  207. }
  208. }
  209.  
  210. public function invoke() {
  211. $r = new ReflectionClass($this->target);
  212. $method = $r->getMethod($this->method);
  213. return $method->invoke($this->target);
  214. }
  215.  
  216. /**
  217. * @param string s
  218. * @return Object
  219. */
  220.  
  221. /* it is run just when TypeAdapter is not a subclass */
  222. public function parse($s) {
  223. return $this->fixture->parse($s, $this->type);
  224. }
  225.  
  226. /**
  227. * @return boolean
  228. */
  229.  
  230. public function equals( $a, $b ) {
  231. if ($a instanceof PHPFIT_ScientificDouble )
  232. return $this->scientificEquals( $a, $b );
  233. }
  234.  
  235. public static function scientificEquals( $a, $b ) {
  236. return $a->equals( $b->toString() );
  237. }
  238.  
  239. public function toString( $o )
  240. {
  241. if( $o == null ) {
  242. return 'null';
  243. }
  244. if( is_object( $o ) ) {
  245. return $o->toString();
  246. }
  247.  
  248. return strval( $o );
  249. }
  250. }
  251. ?>

Documentation generated on Sun, 02 Apr 2006 23:21:41 +0000 by phpDocumentor 1.3.0RC5