public function array_of_issues() { $issue1 = new net·xp_framework·unittest·webservices·rest·IssueWithField(1, 'test1'); $issue2 = new net·xp_framework·unittest·webservices·rest·IssueWithField(2, 'test2'); $this->assertEquals(array($issue1, $issue2), $this->fixture->convert(ArrayType::forName($issue1->getClassName() . '[]'), array(array('issue_id' => 1, 'title' => 'test1'), array('issue_id' => 2, 'title' => 'test2')))); }
public function cast_raises_exceptions_for_non_arrays($value) { ArrayType::forName('var[]')->cast($value); }
public function arrayOfString() { $this->assertEquals(ArrayType::forName('string[]'), Type::forName('string[]')); }
public function intArray() { $this->assertEquals(ArrayType::forName('var[]'), typeof(array(1, 2, 3))); }
public function varArrayAssignableFromIntArray() { $this->assertFalse(ArrayType::forName('var[]')->isAssignableFrom('int[]')); }
public function arrayComponentType() { $this->assertEquals(ArrayType::forName('int[]'), MapType::forName('[:int[]]')->componentType()); }
public function varArgsArrayType() { $this->assertEquals(ArrayType::forName('string[]'), $this->methodParameter('printf', 1)->getType()); }
function typeof($arg) { if ($arg instanceof Generic) { return $arg->getClass(); } else { if (NULL === $arg) { return Type::$VOID; } else { if (is_array($arg)) { return 0 === key($arg) ? ArrayType::forName('var[]') : MapType::forName('[:var]'); } else { return Type::forName(gettype($arg)); } } } }
public function stringArrayDeprecatedSyntax() { $this->assertEquals(ArrayType::forName('string[]'), Type::forName('array<string>')); }
/** * Gets a type for a given name * * Checks for: * <ul> * <li>Primitive types (string, integer, double, boolean, array)</li> * <li>Array notations (string[] or string*)</li> * <li>Resources</li> * <li>Any type (var or *)</li> * <li>Generic notations (util.collections.HashTable<lang.types.String, lang.Generic>)</li> * <li>Anything else will be passed to XPClass::forName()</li> * </ul> * * @param string name * @return lang.Type */ public static function forName($name) { static $deprecated = array('char' => 'string', 'integer' => 'int', 'boolean' => 'bool', 'float' => 'double', 'mixed' => 'var', '*' => 'var', 'array' => 'var[]', 'resource' => 'var'); static $primitives = array('string' => TRUE, 'int' => TRUE, 'double' => TRUE, 'bool' => TRUE); // Map deprecated type names $type = isset($deprecated[$name]) ? $deprecated[$name] : $name; // Map well-known primitives, var and void, handle rest syntactically: // * T[] is an array // * [:T] is a map // * T* is a vararg // * T<K, V> is a generic // * Anything else is a qualified or unqualified class name if (isset($primitives[$type])) { return Primitive::forName($type); } else { if ('var' === $type) { return self::$VAR; } else { if ('void' === $type) { return self::$VOID; return $type; } else { if ('[]' === substr($type, -2)) { return ArrayType::forName($type); } else { if ('[:' === substr($type, 0, 2)) { return MapType::forName($type); } else { if ('*' === substr($type, -1)) { return ArrayType::forName(substr($type, 0, -1) . '[]'); } else { if (FALSE === ($p = strpos($type, '<'))) { return strstr($type, '.') ? XPClass::forName($type) : new XPClass($type); } } } } } } } // Generics // * D<K, V> is a generic type definition D with K and V componenty // * Deprecated: array<T> is T[], array<K, V> is [:T] $base = substr($type, 0, $p); $components = self::forNames(substr($type, $p + 1, -1)); if ('array' !== $base) { return cast(self::forName($base), 'lang.XPClass')->newGenericType($components); } $s = sizeof($components); if (2 === $s) { return MapType::forName('[:' . $components[1]->name . ']'); } else { if (1 === $s) { return ArrayType::forName($components[0]->name . '[]'); } } throw new IllegalArgumentException('Unparseable name ' . $name); }