Version 1
[yaffs-website] / vendor / phpunit / phpunit / src / Framework / Constraint / IsType.php
1 <?php
2 /*
3  * This file is part of PHPUnit.
4  *
5  * (c) Sebastian Bergmann <sebastian@phpunit.de>
6  *
7  * For the full copyright and license information, please view the LICENSE
8  * file that was distributed with this source code.
9  */
10
11 /**
12  * Constraint that asserts that the value it is evaluated for is of a
13  * specified type.
14  *
15  * The expected value is passed in the constructor.
16  *
17  * @since Class available since Release 3.0.0
18  */
19 class PHPUnit_Framework_Constraint_IsType extends PHPUnit_Framework_Constraint
20 {
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';
32
33     /**
34      * @var array
35      */
36     protected $types = array(
37         'array'    => true,
38         'boolean'  => true,
39         'bool'     => true,
40         'double'   => true,
41         'float'    => true,
42         'integer'  => true,
43         'int'      => true,
44         'null'     => true,
45         'numeric'  => true,
46         'object'   => true,
47         'real'     => true,
48         'resource' => true,
49         'string'   => true,
50         'scalar'   => true,
51         'callable' => true
52     );
53
54     /**
55      * @var string
56      */
57     protected $type;
58
59     /**
60      * @param string $type
61      *
62      * @throws PHPUnit_Framework_Exception
63      */
64     public function __construct($type)
65     {
66         parent::__construct();
67
68         if (!isset($this->types[$type])) {
69             throw new PHPUnit_Framework_Exception(
70                 sprintf(
71                     'Type specified for PHPUnit_Framework_Constraint_IsType <%s> ' .
72                     'is not a valid type.',
73                     $type
74                 )
75             );
76         }
77
78         $this->type = $type;
79     }
80
81     /**
82      * Evaluates the constraint for parameter $other. Returns true if the
83      * constraint is met, false otherwise.
84      *
85      * @param mixed $other Value or object to evaluate.
86      *
87      * @return bool
88      */
89     protected function matches($other)
90     {
91         switch ($this->type) {
92             case 'numeric':
93                 return is_numeric($other);
94
95             case 'integer':
96             case 'int':
97                 return is_integer($other);
98
99             case 'double':
100             case 'float':
101             case 'real':
102                 return is_float($other);
103
104             case 'string':
105                 return is_string($other);
106
107             case 'boolean':
108             case 'bool':
109                 return is_bool($other);
110
111             case 'null':
112                 return is_null($other);
113
114             case 'array':
115                 return is_array($other);
116
117             case 'object':
118                 return is_object($other);
119
120             case 'resource':
121                 return is_resource($other) || is_string(@get_resource_type($other));
122
123             case 'scalar':
124                 return is_scalar($other);
125
126             case 'callable':
127                 return is_callable($other);
128         }
129     }
130
131     /**
132      * Returns a string representation of the constraint.
133      *
134      * @return string
135      */
136     public function toString()
137     {
138         return sprintf(
139             'is of type "%s"',
140             $this->type
141         );
142     }
143 }