-
-
Save rightfold/e8fc0e9f8db3697c048d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace MSS\Utility; | |
function _wantScalarType($function, $values) { | |
foreach ($values as $value) | |
if (!$function($value)) | |
throw new \InvalidArgumentException(); | |
} | |
function wantBool() { | |
_wantScalarType('is_bool', func_get_args()); | |
} | |
function wantInt() { | |
_wantScalarType('is_int', func_get_args()); | |
} | |
function wantFloat() { | |
_wantScalarType('is_float', func_get_args()); | |
} | |
function wantString() { | |
_wantScalarType('is_string', func_get_args()); | |
} | |
function wantArrayOf($type) { | |
$arrays = array_slice(func_get_args(), 1); | |
foreach ($arrays as $values) { | |
switch ($type) { | |
case 'bool': | |
call_user_func_array('MSS\Utility\wantBool', $values); | |
break; | |
case 'int': | |
call_user_func_array('MSS\Utility\wantInt', $values); | |
break; | |
case 'float': | |
call_user_func_array('MSS\Utility\wantFloat', $values); | |
break; | |
case 'string': | |
call_user_func_array('MSS\Utility\wantString', $values); | |
break; | |
default: | |
foreach ($values as $value) | |
if (!($value instanceof $type)) | |
throw new \InvalidArgumentException(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment