Exception
5 years ago
WebAuthException
5 years ago
certs
5 years ago
AppInfo.php
5 years ago
AppInfoLoadException.php
5 years ago
ArrayEntryStore.php
5 years ago
AuthBase.php
5 years ago
AuthInfo.php
5 years ago
AuthInfoLoadException.php
5 years ago
Checker.php
5 years ago
Client.php
5 years ago
Curl.php
5 years ago
CurlStreamRelay.php
5 years ago
DeserializeException.php
5 years ago
DropboxMetadataHeaderCatcher.php
5 years ago
Exception.php
5 years ago
Host.php
5 years ago
HttpResponse.php
5 years ago
OAuth1AccessToken.php
5 years ago
OAuth1Upgrader.php
5 years ago
Path.php
5 years ago
RequestUtil.php
5 years ago
RootCertificates.php
5 years ago
SSLTester.php
5 years ago
Security.php
5 years ago
StreamReadException.php
5 years ago
Util.php
5 years ago
ValueStore.php
5 years ago
WebAuth.php
5 years ago
WebAuthBase.php
5 years ago
WebAuthNoRedirect.php
5 years ago
WriteMode.php
5 years ago
autoload.php
5 years ago
strict.php
5 years ago
Checker.php
96 lines
| 1 | <?php |
| 2 | namespace Dropbox; |
| 3 | |
| 4 | /** |
| 5 | * Helper functions to validate arguments. |
| 6 | * |
| 7 | * @internal |
| 8 | */ |
| 9 | class Checker |
| 10 | { |
| 11 | static function throwError($argName, $argValue, $expectedTypeName) |
| 12 | { |
| 13 | if ($argValue === null) throw new \InvalidArgumentException("'$argName' must not be null"); |
| 14 | |
| 15 | if (is_object($argValue)) { |
| 16 | // Class type. |
| 17 | $argTypeName = get_class($argValue); |
| 18 | } else { |
| 19 | // Built-in type. |
| 20 | $argTypeName = gettype($argValue); |
| 21 | } |
| 22 | |
| 23 | throw new \InvalidArgumentException("'$argName' has bad type; expecting $expectedTypeName, got $argTypeName"); |
| 24 | } |
| 25 | |
| 26 | static function argResource($argName, $argValue) |
| 27 | { |
| 28 | if (!is_resource($argValue)) self::throwError($argName, $argValue, "resource"); |
| 29 | } |
| 30 | |
| 31 | static function argCallable($argName, $argValue) |
| 32 | { |
| 33 | if (!is_callable($argValue)) self::throwError($argName, $argValue, "callable"); |
| 34 | } |
| 35 | |
| 36 | static function argBool($argName, $argValue) |
| 37 | { |
| 38 | if (!is_bool($argValue)) self::throwError($argName, $argValue, "boolean"); |
| 39 | } |
| 40 | |
| 41 | static function argArray($argName, $argValue) |
| 42 | { |
| 43 | if (!is_array($argValue)) self::throwError($argName, $argValue, "array"); |
| 44 | } |
| 45 | |
| 46 | static function argString($argName, $argValue) |
| 47 | { |
| 48 | if (!is_string($argValue)) self::throwError($argName, $argValue, "string"); |
| 49 | } |
| 50 | |
| 51 | static function argStringOrNull($argName, $argValue) |
| 52 | { |
| 53 | if ($argValue === null) return; |
| 54 | if (!is_string($argValue)) self::throwError($argName, $argValue, "string"); |
| 55 | } |
| 56 | |
| 57 | static function argStringNonEmpty($argName, $argValue) |
| 58 | { |
| 59 | if (!is_string($argValue)) self::throwError($argName, $argValue, "string"); |
| 60 | if (strlen($argValue) === 0) throw new \InvalidArgumentException("'$argName' must be non-empty"); |
| 61 | } |
| 62 | |
| 63 | static function argStringNonEmptyOrNull($argName, $argValue) |
| 64 | { |
| 65 | if ($argValue === null) return; |
| 66 | if (!is_string($argValue)) self::throwError($argName, $argValue, "string"); |
| 67 | if (strlen($argValue) === 0) throw new \InvalidArgumentException("'$argName' must be non-empty"); |
| 68 | } |
| 69 | |
| 70 | static function argNat($argName, $argValue) |
| 71 | { |
| 72 | if (!is_int($argValue)) self::throwError($argName, $argValue, "int"); |
| 73 | if ($argValue < 0) throw new \InvalidArgumentException("'$argName' must be non-negative (you passed in $argValue)"); |
| 74 | } |
| 75 | |
| 76 | static function argNatOrNull($argName, $argValue) |
| 77 | { |
| 78 | if ($argValue === null) return; |
| 79 | if (!is_int($argValue)) self::throwError($argName, $argValue, "int"); |
| 80 | if ($argValue < 0) throw new \InvalidArgumentException("'$argName' must be non-negative (you passed in $argValue)"); |
| 81 | } |
| 82 | |
| 83 | static function argIntPositive($argName, $argValue) |
| 84 | { |
| 85 | if (!is_int($argValue)) self::throwError($argName, $argValue, "int"); |
| 86 | if ($argValue < 1) throw new \InvalidArgumentException("'$argName' must be positive (you passed in $argValue)"); |
| 87 | } |
| 88 | |
| 89 | static function argIntPositiveOrNull($argName, $argValue) |
| 90 | { |
| 91 | if ($argValue === null) return; |
| 92 | if (!is_int($argValue)) self::throwError($argName, $argValue, "int"); |
| 93 | if ($argValue < 1) throw new \InvalidArgumentException("'$argName' must be positive (you passed in $argValue)"); |
| 94 | } |
| 95 | } |
| 96 |