PageRenderTime 40ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/php-phpunit-PHPUnit-3.6.11/PHPUnit-3.6.11/PHPUnit/Framework/Constraint/IsType.php

#
PHP | 191 lines | 91 code | 18 blank | 82 comment | 2 complexity | 7f498b0203ec9dfe7fce356ad39058ae MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /**
  3. * PHPUnit
  4. *
  5. * Copyright (c) 2001-2012, Sebastian Bergmann <sebastian@phpunit.de>.
  6. * All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * * Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * * Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in
  17. * the documentation and/or other materials provided with the
  18. * distribution.
  19. *
  20. * * Neither the name of Sebastian Bergmann nor the names of his
  21. * contributors may be used to endorse or promote products derived
  22. * from this software without specific prior written permission.
  23. *
  24. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  25. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  26. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  27. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  28. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  29. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  30. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  31. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  32. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  33. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  34. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  35. * POSSIBILITY OF SUCH DAMAGE.
  36. *
  37. * @package PHPUnit
  38. * @subpackage Framework_Constraint
  39. * @author Sebastian Bergmann <sebastian@phpunit.de>
  40. * @author Bernhard Schussek <bschussek@2bepublished.at>
  41. * @copyright 2001-2012 Sebastian Bergmann <sebastian@phpunit.de>
  42. * @license http://www.opensource.org/licenses/bsd-license.php BSD License
  43. * @link http://www.phpunit.de/
  44. * @since File available since Release 3.0.0
  45. */
  46. /**
  47. * Constraint that asserts that the value it is evaluated for is of a
  48. * specified type.
  49. *
  50. * The expected value is passed in the constructor.
  51. *
  52. * @package PHPUnit
  53. * @subpackage Framework_Constraint
  54. * @author Sebastian Bergmann <sebastian@phpunit.de>
  55. * @author Bernhard Schussek <bschussek@2bepublished.at>
  56. * @copyright 2001-2012 Sebastian Bergmann <sebastian@phpunit.de>
  57. * @license http://www.opensource.org/licenses/bsd-license.php BSD License
  58. * @version Release: 3.6.11
  59. * @link http://www.phpunit.de/
  60. * @since Class available since Release 3.0.0
  61. */
  62. class PHPUnit_Framework_Constraint_IsType extends PHPUnit_Framework_Constraint
  63. {
  64. const TYPE_ARRAY = 'array';
  65. const TYPE_BOOL = 'bool';
  66. const TYPE_FLOAT = 'float';
  67. const TYPE_INT = 'int';
  68. const TYPE_NULL = 'null';
  69. const TYPE_NUMERIC = 'numeric';
  70. const TYPE_OBJECT = 'object';
  71. const TYPE_RESOURCE = 'resource';
  72. const TYPE_STRING = 'string';
  73. const TYPE_SCALAR = 'scalar';
  74. const TYPE_CALLABLE = 'callable';
  75. /**
  76. * @var array
  77. */
  78. protected $types = array(
  79. 'array' => TRUE,
  80. 'boolean' => TRUE,
  81. 'bool' => TRUE,
  82. 'float' => TRUE,
  83. 'integer' => TRUE,
  84. 'int' => TRUE,
  85. 'null' => TRUE,
  86. 'numeric' => TRUE,
  87. 'object' => TRUE,
  88. 'resource' => TRUE,
  89. 'string' => TRUE,
  90. 'scalar' => TRUE,
  91. 'callable' => TRUE
  92. );
  93. /**
  94. * @var string
  95. */
  96. protected $type;
  97. /**
  98. * @param string $type
  99. * @throws InvalidArgumentException
  100. */
  101. public function __construct($type)
  102. {
  103. if (!isset($this->types[$type])) {
  104. throw new InvalidArgumentException(
  105. sprintf(
  106. 'Type specified for PHPUnit_Framework_Constraint_IsType <%s> ' .
  107. 'is not a valid type.',
  108. $type
  109. )
  110. );
  111. }
  112. $this->type = $type;
  113. }
  114. /**
  115. * Evaluates the constraint for parameter $other. Returns TRUE if the
  116. * constraint is met, FALSE otherwise.
  117. *
  118. * @param mixed $other Value or object to evaluate.
  119. * @return bool
  120. */
  121. protected function matches($other)
  122. {
  123. switch ($this->type) {
  124. case 'numeric': {
  125. return is_numeric($other);
  126. }
  127. case 'integer':
  128. case 'int': {
  129. return is_integer($other);
  130. }
  131. case 'float': {
  132. return is_float($other);
  133. }
  134. case 'string': {
  135. return is_string($other);
  136. }
  137. case 'boolean':
  138. case 'bool': {
  139. return is_bool($other);
  140. }
  141. case 'null': {
  142. return is_null($other);
  143. }
  144. case 'array': {
  145. return is_array($other);
  146. }
  147. case 'object': {
  148. return is_object($other);
  149. }
  150. case 'resource': {
  151. return is_resource($other);
  152. }
  153. case 'scalar': {
  154. return is_scalar($other);
  155. }
  156. case 'callable': {
  157. return is_callable($other);
  158. }
  159. }
  160. }
  161. /**
  162. * Returns a string representation of the constraint.
  163. *
  164. * @return string
  165. */
  166. public function toString()
  167. {
  168. return sprintf(
  169. 'is of type "%s"',
  170. $this->type
  171. );
  172. }
  173. }