Nette\Utils\ObjectMixin::checkType PHP Method

checkType() public static method

Finds whether a variable is of expected type and do non-data-loss conversion.
public static checkType ( &$val, $type ) : boolean
return boolean
    public static function checkType(&$val, $type)
    {
        if (strpos($type, '|') !== FALSE) {
            $found = NULL;
            foreach (explode('|', $type) as $type) {
                $tmp = $val;
                if (self::checkType($tmp, $type)) {
                    if ($val === $tmp) {
                        return TRUE;
                    }
                    $found[] = $tmp;
                }
            }
            if ($found) {
                $val = $found[0];
                return TRUE;
            }
            return FALSE;
        } elseif (substr($type, -2) === '[]') {
            if (!is_array($val)) {
                return FALSE;
            }
            $type = substr($type, 0, -2);
            $res = [];
            foreach ($val as $k => $v) {
                if (!self::checkType($v, $type)) {
                    return FALSE;
                }
                $res[$k] = $v;
            }
            $val = $res;
            return TRUE;
        }
        switch (strtolower($type)) {
            case NULL:
            case 'mixed':
                return TRUE;
            case 'bool':
            case 'boolean':
                return ($val === NULL || is_scalar($val)) && settype($val, 'bool');
            case 'string':
                return ($val === NULL || is_scalar($val) || is_object($val) && method_exists($val, '__toString')) && settype($val, 'string');
            case 'int':
            case 'integer':
                return ($val === NULL || is_bool($val) || is_numeric($val)) && (double) (int) $val === (double) $val && settype($val, 'int');
            case 'float':
                return ($val === NULL || is_bool($val) || is_numeric($val)) && settype($val, 'float');
            case 'scalar':
            case 'array':
            case 'object':
            case 'callable':
            case 'resource':
            case 'null':
                return call_user_func("is_{$type}", $val);
            default:
                return $val instanceof $type;
        }
    }

Usage Example

Beispiel #1
0
 /**
  * @return mixed
  * @throws MemberAccessException
  */
 public function __call($name, $args)
 {
     $class = get_class($this);
     $isProp = ObjectMixin::hasProperty($class, $name);
     if ($name === '') {
         throw new MemberAccessException("Call to class '{$class}' method without name.");
     } elseif ($isProp === 'event') {
         // calling event handlers
         if (is_array($this->{$name}) || $this->{$name} instanceof \Traversable) {
             foreach ($this->{$name} as $handler) {
                 Callback::invokeArgs($handler, $args);
             }
         } elseif ($this->{$name} !== NULL) {
             throw new UnexpectedValueException("Property {$class}::\${$name} must be array or NULL, " . gettype($this->{$name}) . ' given.');
         }
     } elseif ($isProp && $this->{$name} instanceof \Closure) {
         // closure in property
         trigger_error("Invoking closure in property via \$obj->{$name}() is deprecated" . ObjectMixin::getSource(), E_USER_DEPRECATED);
         return call_user_func_array($this->{$name}, $args);
     } elseif (($methods =& ObjectMixin::getMethods($class)) && isset($methods[$name]) && is_array($methods[$name])) {
         // magic @methods
         trigger_error("Magic methods such as {$class}::{$name}() are deprecated" . ObjectMixin::getSource(), E_USER_DEPRECATED);
         list($op, $rp, $type) = $methods[$name];
         if (count($args) !== ($op === 'get' ? 0 : 1)) {
             throw new InvalidArgumentException("{$class}::{$name}() expects " . ($op === 'get' ? 'no' : '1') . ' argument, ' . count($args) . ' given.');
         } elseif ($type && $args && !ObjectMixin::checkType($args[0], $type)) {
             throw new InvalidArgumentException("Argument passed to {$class}::{$name}() must be {$type}, " . gettype($args[0]) . ' given.');
         }
         if ($op === 'get') {
             return $rp->getValue($this);
         } elseif ($op === 'set') {
             $rp->setValue($this, $args[0]);
         } elseif ($op === 'add') {
             $val = $rp->getValue($this);
             $val[] = $args[0];
             $rp->setValue($this, $val);
         }
         return $this;
     } elseif ($cb = ObjectMixin::getExtensionMethod($class, $name)) {
         // extension methods
         trigger_error("Extension methods such as {$class}::{$name}() are deprecated" . ObjectMixin::getSource(), E_USER_DEPRECATED);
         return Callback::invoke($cb, $this, ...$args);
     } else {
         ObjectMixin::strictCall($class, $name);
     }
 }