YaLinqo\Enumerable::ofType PHP Method

ofType() public method

Syntax: ofType (type)

The ofType method returns only elements of the specified type. To instead receive an error if an element cannot be cast, use {@link cast}.

public ofType ( string $type ) : Enumerable
$type string The type to filter the elements of the sequence on. Can be either class name or one of the predefined types: array, int (integer, long), callable (callable), float (real, double), null, string, object, numeric, scalar.
return Enumerable A sequence that contains elements from the input sequence of the specified type.
    public function ofType($type)
    {
        switch ($type) {
            case 'array':
                return $this->where(function ($v) {
                    return is_array($v);
                });
            case 'int':
            case 'integer':
            case 'long':
                return $this->where(function ($v) {
                    return is_int($v);
                });
            case 'callable':
            case 'callback':
                return $this->where(function ($v) {
                    return is_callable($v);
                });
            case 'float':
            case 'real':
            case 'double':
                return $this->where(function ($v) {
                    return is_float($v);
                });
            case 'null':
                return $this->where(function ($v) {
                    return is_null($v);
                });
            case 'numeric':
                return $this->where(function ($v) {
                    return is_numeric($v);
                });
            case 'object':
                return $this->where(function ($v) {
                    return is_object($v);
                });
            case 'scalar':
                return $this->where(function ($v) {
                    return is_scalar($v);
                });
            case 'string':
                return $this->where(function ($v) {
                    return is_string($v);
                });
            default:
                return $this->where(function ($v) use($type) {
                    return is_object($v) && get_class($v) === $type;
                });
        }
    }