You are here

public static function ParagonIE_Sodium_Core_Util::declareScalarType in Automatic Updates 8

Same name and namespace in other branches
  1. 7 vendor/paragonie/sodium_compat/src/Core/Util.php \ParagonIE_Sodium_Core_Util::declareScalarType()

If a variable does not match a given type, throw a TypeError.

Parameters

mixed $mixedVar:

string $type:

int $argumentIndex:

Return value

void

Throws

TypeError

SodiumException

66 calls to ParagonIE_Sodium_Core_Util::declareScalarType()
ParagonIE_Sodium_Compat::base642bin in vendor/paragonie/sodium_compat/src/Compat.php
ParagonIE_Sodium_Compat::bin2base64 in vendor/paragonie/sodium_compat/src/Compat.php
ParagonIE_Sodium_Compat::bin2hex in vendor/paragonie/sodium_compat/src/Compat.php
Cache-timing-safe implementation of bin2hex().
ParagonIE_Sodium_Compat::compare in vendor/paragonie/sodium_compat/src/Compat.php
Compare two strings, in constant-time. Compared to memcmp(), compare() is more useful for sorting.
ParagonIE_Sodium_Compat::crypto_aead_aes256gcm_decrypt in vendor/paragonie/sodium_compat/src/Compat.php
Authenticated Encryption with Associated Data: Decryption

... See full list

File

vendor/paragonie/sodium_compat/src/Core/Util.php, line 183

Class

ParagonIE_Sodium_Core_Util
Class ParagonIE_Sodium_Core_Util

Code

public static function declareScalarType(&$mixedVar = null, $type = 'void', $argumentIndex = 0) {
  if (func_num_args() === 0) {

    /* Tautology, by default */
    return;
  }
  if (func_num_args() === 1) {
    throw new TypeError('Declared void, but passed a variable');
  }
  $realType = strtolower(gettype($mixedVar));
  $type = strtolower($type);
  switch ($type) {
    case 'null':
      if ($mixedVar !== null) {
        throw new TypeError('Argument ' . $argumentIndex . ' must be null, ' . $realType . ' given.');
      }
      break;
    case 'integer':
    case 'int':
      $allow = array(
        'int',
        'integer',
      );
      if (!in_array($type, $allow)) {
        throw new TypeError('Argument ' . $argumentIndex . ' must be an integer, ' . $realType . ' given.');
      }
      $mixedVar = (int) $mixedVar;
      break;
    case 'boolean':
    case 'bool':
      $allow = array(
        'bool',
        'boolean',
      );
      if (!in_array($type, $allow)) {
        throw new TypeError('Argument ' . $argumentIndex . ' must be a boolean, ' . $realType . ' given.');
      }
      $mixedVar = (bool) $mixedVar;
      break;
    case 'string':
      if (!is_string($mixedVar)) {
        throw new TypeError('Argument ' . $argumentIndex . ' must be a string, ' . $realType . ' given.');
      }
      $mixedVar = (string) $mixedVar;
      break;
    case 'decimal':
    case 'double':
    case 'float':
      $allow = array(
        'decimal',
        'double',
        'float',
      );
      if (!in_array($type, $allow)) {
        throw new TypeError('Argument ' . $argumentIndex . ' must be a float, ' . $realType . ' given.');
      }
      $mixedVar = (double) $mixedVar;
      break;
    case 'object':
      if (!is_object($mixedVar)) {
        throw new TypeError('Argument ' . $argumentIndex . ' must be an object, ' . $realType . ' given.');
      }
      break;
    case 'array':
      if (!is_array($mixedVar)) {
        if (is_object($mixedVar)) {
          if ($mixedVar instanceof ArrayAccess) {
            return;
          }
        }
        throw new TypeError('Argument ' . $argumentIndex . ' must be an array, ' . $realType . ' given.');
      }
      break;
    default:
      throw new SodiumException('Unknown type (' . $realType . ') does not match expect type (' . $type . ')');
  }
}