Source for file TypeAdapter.php

Documentation is available at TypeAdapter.php

  1. <?php
  2.  
  3.  
  4.  
  5. # NOTE FOR TYPE ADAPTERS IN PHP:
  6.  
  7. # Copyright (c) 2002-2005 Cunningham & Cunningham, Inc.
  8. # Released under the terms of the GNU General Public License version 2 or later.
  9. #
  10. # PHP5 translation by Luis A. Floreani <luis.floreani@gmail.com>
  11.  
  12. class TypeAdapter {
  13.  
  14. public /*Object*/
  15. $target;
  16. public $fixture;
  17. public $field;
  18. public $method;
  19. public /*Class*/
  20. $type = null;
  21.  
  22. /**
  23. * @param Fixture target
  24. * @param string name
  25. */
  26.  
  27. public static function onMethod( $fixture, $name ) {
  28. $type = $fixture->getType( $name );
  29. $adapter = self::on( $fixture, $type );
  30. $adapter->method = $name;
  31. return $adapter;
  32. }
  33.  
  34. /**
  35. * @param Fixture target
  36. * @param string name
  37. */
  38.  
  39. public static function onField($fixture, $name) {
  40. $type = $fixture->getType( $name );
  41. $adapter = self::on( $fixture, $type );
  42. $adapter->field = $name;
  43. return $adapter;
  44. }
  45.  
  46. public static function on( $fixture, $type ) {
  47. $a = self::adapterFor( $type );
  48. $a->init( $fixture, $type );
  49. $a->target = $fixture;
  50. return $a;
  51. }
  52.  
  53. public static function adapterFor($type) {
  54. if (self :: is_bool($type))
  55. return new BooleanAdapter();
  56. if (self :: is_int($type))
  57. return new IntegerAdapter();
  58. if (self :: is_double($type))
  59. return new DoubleAdapter();
  60. if (self :: is_string($type))
  61. return new StringAdapter();
  62.  
  63. return new TypeAdapter();
  64. }
  65. public static function is_bool($type) {
  66. return $type == "boolean";
  67. }
  68. public static function is_int($type) {
  69. return $type == "integer";
  70. }
  71. public static function is_double($type) {
  72. return $type == "double";
  73. }
  74. public static function is_string($type) {
  75. return $type == "string";
  76. }
  77.  
  78. /*
  79. public static function getReturnType($fixture, $name) {
  80. $r = new ReflectionClass($fixture);
  81.  
  82. return self::getType($fixture, $name);
  83.  
  84. // $method = $r->getMethod($name);
  85. // $val = $method->invoke($fixture);
  86. //
  87. // $type = gettype($val);
  88. //
  89. // if ($type == "object")
  90. // return get_class($val);
  91. //
  92. // return $type;
  93.  
  94. }
  95.  
  96. public static function getFieldType($fixture, $name) {
  97. $r = new ReflectionClass($fixture);
  98.  
  99. return self::getType($fixture, $name);
  100. ;
  101.  
  102. // $prop = $r->getProperty($name);
  103. // $val = $prop->getValue($fixture);
  104. // return gettype($val);
  105.  
  106. }
  107. */
  108. /*
  109. public static function getType( $fixture, $name ) {
  110. $r = new ReflectionClass($fixture);
  111.  
  112. $properties = $r->getProperties();
  113. foreach ($properties as $property) {
  114. if ($property->getName() == "typeDict") {
  115. $values = $property->getValue($fixture);
  116. if (isset($values[$name]))
  117. return $values[$name];
  118. else
  119. throw new Exception($name . " does not exist in typeDict");
  120. }
  121. }
  122. throw new Exception("There is no typeDict in your fixture!");
  123. }
  124. */
  125.  
  126. public function init($fixture, $type) {
  127. $this->fixture = $fixture;
  128. $this->type = $type;
  129. }
  130.  
  131. public function set($value) {
  132. $r = new ReflectionClass($this->target);
  133. $prop = $r->getProperty($this->field);
  134. $prop->setValue($this->target, $value);
  135. }
  136.  
  137. public function get() {
  138. if ($this->field != null) {
  139. if ($this->field instanceof self)
  140. return $this->field->get($this->target);
  141. return "";
  142. }
  143.  
  144. if ($this->method != null) {
  145. $sal = $this->invoke();
  146. return $sal;
  147. }
  148. }
  149.  
  150. public function invoke() {
  151. $r = new ReflectionClass($this->target);
  152. $method = $r->getMethod($this->method);
  153. return $method->invoke($this->target);
  154. }
  155.  
  156. /**
  157. * @param string s
  158. * @return Object
  159. */
  160.  
  161. /* it is run just when TypeAdapter is not a subclass */
  162. public function parse($s) {
  163. return $this->fixture->parse($s, $this->type);
  164. }
  165.  
  166. /**
  167. * @return boolean
  168. */
  169.  
  170. public function equals($a, $b) {
  171. if ($a instanceof ScientificDouble)
  172. return $this->scientificEquals($a, $b);
  173. }
  174.  
  175. public static function scientificEquals($a, $b) {
  176. return $a->equals($b->toString());
  177. }
  178.  
  179. public function toString($o) {
  180. if ($o == null)
  181. return "null";
  182. if (is_object($o))
  183. return $o->toString();
  184.  
  185. return strval($o);
  186. }
  187. }
  188.  
  189. class BooleanAdapter extends TypeAdapter {
  190. public function equals($a, $b) {
  191. return $a == $b;
  192. }
  193.  
  194. public function parse($s) {
  195. if ($s == "false")
  196. return false;
  197. else
  198. if ($s == "true")
  199. return true;
  200. return "not a boolean";
  201. }
  202. }
  203.  
  204. class DoubleAdapter extends TypeAdapter {
  205.  
  206. private $PRECISION = 0.000001;
  207.  
  208. public function equals($a, $b) {
  209. return $this->doubleEquals($a, $b);
  210. }
  211.  
  212. public function doubleEquals($a, $b) {
  213. if (abs($b - $a) < $this->PRECISION) {
  214. return true;
  215. }
  216. return false;
  217. }
  218.  
  219. public function parse($s) {
  220. return (double) $s;
  221. }
  222. }
  223.  
  224. class IntegerAdapter extends TypeAdapter {
  225. public function equals($a, $b) {
  226. return $a == $b;
  227. }
  228.  
  229. public function parse($s) {
  230. return intval($s);
  231. }
  232. }
  233.  
  234. class StringAdapter extends TypeAdapter {
  235. public function equals($a, $b) {
  236. return strcmp($a, $b) == 0;
  237. }
  238.  
  239. public function parse($s) {
  240. return $s;
  241. }
  242. }
  243. ?>

Documentation generated on Sun, 02 Apr 2006 16:01:07 +0200 by phpDocumentor 1.3.0RC5