Documentation is available at TypeAdapter.php
- <?php
- # NOTE FOR TYPE ADAPTERS IN PHP:
- # Copyright (c) 2002-2005 Cunningham & Cunningham, Inc.
- # Released under the terms of the GNU General Public License version 2 or later.
- #
- # PHP5 translation by Luis A. Floreani <luis.floreani@gmail.com>
- class TypeAdapter {
- public /*Object*/
- $target;
- public $fixture;
- public $field;
- public $method;
- public /*Class*/
- $type = null;
- /**
- * @param Fixture target
- * @param string name
- */
- public static function onMethod( $fixture, $name ) {
- $type = $fixture->getType( $name );
- $adapter = self::on( $fixture, $type );
- $adapter->method = $name;
- return $adapter;
- }
- /**
- * @param Fixture target
- * @param string name
- */
- public static function onField($fixture, $name) {
- $type = $fixture->getType( $name );
- $adapter = self::on( $fixture, $type );
- $adapter->field = $name;
- return $adapter;
- }
- public static function on( $fixture, $type ) {
- $a = self::adapterFor( $type );
- $a->init( $fixture, $type );
- $a->target = $fixture;
- return $a;
- }
- public static function adapterFor($type) {
- if (self :: is_bool($type))
- return new BooleanAdapter();
- if (self :: is_int($type))
- return new IntegerAdapter();
- if (self :: is_double($type))
- return new DoubleAdapter();
- if (self :: is_string($type))
- return new StringAdapter();
- return new TypeAdapter();
- }
- public static function is_bool($type) {
- return $type == "boolean";
- }
- public static function is_int($type) {
- return $type == "integer";
- }
- public static function is_double($type) {
- return $type == "double";
- }
- public static function is_string($type) {
- return $type == "string";
- }
- /*
- public static function getReturnType($fixture, $name) {
- $r = new ReflectionClass($fixture);
- return self::getType($fixture, $name);
- // $method = $r->getMethod($name);
- // $val = $method->invoke($fixture);
- //
- // $type = gettype($val);
- //
- // if ($type == "object")
- // return get_class($val);
- //
- // return $type;
- }
- public static function getFieldType($fixture, $name) {
- $r = new ReflectionClass($fixture);
- return self::getType($fixture, $name);
- ;
- // $prop = $r->getProperty($name);
- // $val = $prop->getValue($fixture);
- // return gettype($val);
- }
- */
- /*
- public static function getType( $fixture, $name ) {
- $r = new ReflectionClass($fixture);
- $properties = $r->getProperties();
- foreach ($properties as $property) {
- if ($property->getName() == "typeDict") {
- $values = $property->getValue($fixture);
- if (isset($values[$name]))
- return $values[$name];
- else
- throw new Exception($name . " does not exist in typeDict");
- }
- }
- throw new Exception("There is no typeDict in your fixture!");
- }
- */
- public function init($fixture, $type) {
- $this->fixture = $fixture;
- $this->type = $type;
- }
- public function set($value) {
- $r = new ReflectionClass($this->target);
- $prop = $r->getProperty($this->field);
- $prop->setValue($this->target, $value);
- }
- public function get() {
- if ($this->field != null) {
- if ($this->field instanceof self)
- return $this->field->get($this->target);
- return "";
- }
- if ($this->method != null) {
- $sal = $this->invoke();
- return $sal;
- }
- }
- public function invoke() {
- $r = new ReflectionClass($this->target);
- $method = $r->getMethod($this->method);
- return $method->invoke($this->target);
- }
- /**
- * @param string s
- * @return Object
- */
- /* it is run just when TypeAdapter is not a subclass */
- public function parse($s) {
- return $this->fixture->parse($s, $this->type);
- }
- /**
- * @return boolean
- */
- public function equals($a, $b) {
- if ($a instanceof ScientificDouble)
- return $this->scientificEquals($a, $b);
- }
- public static function scientificEquals($a, $b) {
- return $a->equals($b->toString());
- }
- public function toString($o) {
- if ($o == null)
- return "null";
- if (is_object($o))
- return $o->toString();
- return strval($o);
- }
- }
- class BooleanAdapter extends TypeAdapter {
- public function equals($a, $b) {
- return $a == $b;
- }
- public function parse($s) {
- if ($s == "false")
- return false;
- else
- if ($s == "true")
- return true;
- return "not a boolean";
- }
- }
- class DoubleAdapter extends TypeAdapter {
- private $PRECISION = 0.000001;
- public function equals($a, $b) {
- return $this->doubleEquals($a, $b);
- }
- public function doubleEquals($a, $b) {
- if (abs($b - $a) < $this->PRECISION) {
- return true;
- }
- return false;
- }
- public function parse($s) {
- return (double) $s;
- }
- }
- class IntegerAdapter extends TypeAdapter {
- public function equals($a, $b) {
- return $a == $b;
- }
- public function parse($s) {
- return intval($s);
- }
- }
- class StringAdapter extends TypeAdapter {
- public function equals($a, $b) {
- return strcmp($a, $b) == 0;
- }
- public function parse($s) {
- return $s;
- }
- }
- ?>
Documentation generated on Sun, 02 Apr 2006 16:01:07 +0200 by phpDocumentor 1.3.0RC5