public function testGetTypeOf() { $map = array('int' => 1, 'string' => 'asdf', 'float' => 1.5, 'bool' => true, 'null' => null, 'map<wild, wild>' => array(), 'list<string>' => array('a', 'b'), 'list<int>' => array(1, 2, 3), 'map<string, int>' => array('x' => 3), 'map<int, list<string>>' => array(1 => array('x', 'y')), 'stdClass' => new stdClass(), 'list<Exception>' => array(new Exception(), new LogicException(), new RuntimeException()), 'map<string, stdClass>' => array('x' => new stdClass())); foreach ($map as $expect => $input) { $this->assertEqual($expect, PhutilTypeSpec::getTypeOf($input), print_r($input, true)); PhutilTypeSpec::newFromString($expect)->check($input); } }
public function __construct(PhutilTypeSpec $type, $value, $name = null, $err = null) { if ($name !== null) { $invalid = pht("Parameter '%s' has invalid type.", $name); } else { $invalid = pht("Parameter has invalid type."); } if ($type->getType() == 'regex') { if (is_string($value)) { $message = pht("Expected a regular expression, but '%s' is not valid: %s", $value, $err); } else { $message = pht("Expected a regular expression, but value is not valid: %s", $err); } } else { $message = pht("Expected type '%s', got type '%s'.", $type->toString(), PhutilTypeSpec::getTypeOf($value)); } parent::__construct($invalid . ' ' . $message); }