1<?php 2 3/* 4 * This file is part of the Prophecy. 5 * (c) Konstantin Kudryashov <ever.zet@gmail.com> 6 * Marcello Duarte <marcello.duarte@gmail.com> 7 * 8 * For the full copyright and license information, please view the LICENSE 9 * file that was distributed with this source code. 10 */ 11 12namespace Prophecy; 13 14use Prophecy\Argument\Token; 15 16/** 17 * Argument tokens shortcuts. 18 * 19 * @author Konstantin Kudryashov <ever.zet@gmail.com> 20 */ 21class Argument 22{ 23 /** 24 * Checks that argument is exact value or object. 25 * 26 * @param mixed $value 27 * 28 * @return Token\ExactValueToken 29 */ 30 public static function exact($value) 31 { 32 return new Token\ExactValueToken($value); 33 } 34 35 /** 36 * Checks that argument is of specific type or instance of specific class. 37 * 38 * @param string $type Type name (`integer`, `string`) or full class name 39 * 40 * @return Token\TypeToken 41 */ 42 public static function type($type) 43 { 44 return new Token\TypeToken($type); 45 } 46 47 /** 48 * Checks that argument object has specific state. 49 * 50 * @param string $methodName 51 * @param mixed $value 52 * 53 * @return Token\ObjectStateToken 54 */ 55 public static function which($methodName, $value) 56 { 57 return new Token\ObjectStateToken($methodName, $value); 58 } 59 60 /** 61 * Checks that argument matches provided callback. 62 * 63 * @param callable $callback 64 * 65 * @return Token\CallbackToken 66 */ 67 public static function that($callback) 68 { 69 return new Token\CallbackToken($callback); 70 } 71 72 /** 73 * Matches any single value. 74 * 75 * @return Token\AnyValueToken 76 */ 77 public static function any() 78 { 79 return new Token\AnyValueToken; 80 } 81 82 /** 83 * Matches all values to the rest of the signature. 84 * 85 * @return Token\AnyValuesToken 86 */ 87 public static function cetera() 88 { 89 return new Token\AnyValuesToken; 90 } 91 92 /** 93 * Checks that argument matches all tokens 94 * 95 * @param mixed ... a list of tokens 96 * 97 * @return Token\LogicalAndToken 98 */ 99 public static function allOf() 100 { 101 return new Token\LogicalAndToken(func_get_args()); 102 } 103 104 /** 105 * Checks that argument array or countable object has exact number of elements. 106 * 107 * @param integer $value array elements count 108 * 109 * @return Token\ArrayCountToken 110 */ 111 public static function size($value) 112 { 113 return new Token\ArrayCountToken($value); 114 } 115 116 /** 117 * Checks that argument array contains (key, value) pair 118 * 119 * @param mixed $key exact value or token 120 * @param mixed $value exact value or token 121 * 122 * @return Token\ArrayEntryToken 123 */ 124 public static function withEntry($key, $value) 125 { 126 return new Token\ArrayEntryToken($key, $value); 127 } 128 129 /** 130 * Checks that arguments array entries all match value 131 * 132 * @param mixed $value 133 * 134 * @return Token\ArrayEveryEntryToken 135 */ 136 public static function withEveryEntry($value) 137 { 138 return new Token\ArrayEveryEntryToken($value); 139 } 140 141 /** 142 * Checks that argument array contains value 143 * 144 * @param mixed $value 145 * 146 * @return Token\ArrayEntryToken 147 */ 148 public static function containing($value) 149 { 150 return new Token\ArrayEntryToken(self::any(), $value); 151 } 152 153 /** 154 * Checks that argument array has key 155 * 156 * @param mixed $key exact value or token 157 * 158 * @return Token\ArrayEntryToken 159 */ 160 public static function withKey($key) 161 { 162 return new Token\ArrayEntryToken($key, self::any()); 163 } 164 165 /** 166 * Checks that argument does not match the value|token. 167 * 168 * @param mixed $value either exact value or argument token 169 * 170 * @return Token\LogicalNotToken 171 */ 172 public static function not($value) 173 { 174 return new Token\LogicalNotToken($value); 175 } 176 177 /** 178 * @param string $value 179 * 180 * @return Token\StringContainsToken 181 */ 182 public static function containingString($value) 183 { 184 return new Token\StringContainsToken($value); 185 } 186 187 /** 188 * Checks that argument is identical value. 189 * 190 * @param mixed $value 191 * 192 * @return Token\IdenticalValueToken 193 */ 194 public static function is($value) 195 { 196 return new Token\IdenticalValueToken($value); 197 } 198 199 /** 200 * Check that argument is same value when rounding to the 201 * given precision. 202 * 203 * @param float $value 204 * @param float $precision 205 * 206 * @return Token\ApproximateValueToken 207 */ 208 public static function approximate($value, $precision = 0) 209 { 210 return new Token\ApproximateValueToken($value, $precision); 211 } 212} 213