PHP Classes

File: assertType.php

Recommend this page to a friend!
  Classes of Nathaniel Sherwood   Encase PHP Functional Programming   assertType.php   Download  
File: assertType.php
Role: Auxiliary script
Content type: text/plain
Description: Auxiliary script
Class: Encase PHP Functional Programming
Make functions work as if they are class functions
Author: By
Last change:
Date: 5 years ago
Size: 764 bytes
 

Contents

Class file image Download
<?php
namespace Encase\Functional;

use
Encase\Functional\Exceptions\InvalidTypeError;

/**
 * Asserts the value is the given type or one of an array of types.
 * Returns the matched type of the value or throws an exception on no match.
 *
 * @see \Encase\Functional\isType
 *
 * @param mixed $value Value to be checked.
 * @param string|string[] $type Type or an array of matched types.
 * @param string|null $paramName Parameter name to reference in exceptions.
 * @return string
 * @throws \Encase\Functional\Exceptions\InvalidTypeError
 */
function assertType($value, $type, string $paramName = null): string
{
   
$match = isType($value, $type);

    if (
$match === false) {
        throw
InvalidTypeError::make($type, $value, $paramName, 2);
    }

    return
$match;
}