Skip to content

Instantly share code, notes, and snippets.

@rightfold
Created March 2, 2014 12:36
Show Gist options
  • Save rightfold/e8fc0e9f8db3697c048d to your computer and use it in GitHub Desktop.
Save rightfold/e8fc0e9f8db3697c048d to your computer and use it in GitHub Desktop.
<?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