3 * This file is part of PHPUnit.
5 * (c) Sebastian Bergmann <sebastian@phpunit.de>
7 * For the full copyright and license information, please view the LICENSE
8 * file that was distributed with this source code.
12 * Constraint that asserts that the value it is evaluated for is of a
15 * The expected value is passed in the constructor.
17 * @since Class available since Release 3.0.0
19 class PHPUnit_Framework_Constraint_IsType extends PHPUnit_Framework_Constraint
21 const TYPE_ARRAY = 'array';
22 const TYPE_BOOL = 'bool';
23 const TYPE_FLOAT = 'float';
24 const TYPE_INT = 'int';
25 const TYPE_NULL = 'null';
26 const TYPE_NUMERIC = 'numeric';
27 const TYPE_OBJECT = 'object';
28 const TYPE_RESOURCE = 'resource';
29 const TYPE_STRING = 'string';
30 const TYPE_SCALAR = 'scalar';
31 const TYPE_CALLABLE = 'callable';
36 protected $types = array(
62 * @throws PHPUnit_Framework_Exception
64 public function __construct($type)
66 parent::__construct();
68 if (!isset($this->types[$type])) {
69 throw new PHPUnit_Framework_Exception(
71 'Type specified for PHPUnit_Framework_Constraint_IsType <%s> ' .
72 'is not a valid type.',
82 * Evaluates the constraint for parameter $other. Returns true if the
83 * constraint is met, false otherwise.
85 * @param mixed $other Value or object to evaluate.
89 protected function matches($other)
91 switch ($this->type) {
93 return is_numeric($other);
97 return is_integer($other);
102 return is_float($other);
105 return is_string($other);
109 return is_bool($other);
112 return is_null($other);
115 return is_array($other);
118 return is_object($other);
121 return is_resource($other) || is_string(@get_resource_type($other));
124 return is_scalar($other);
127 return is_callable($other);
132 * Returns a string representation of the constraint.
136 public function toString()