Ampache PHP Cross Reference Groupware Applications

Source: /modules/Dropbox/Checker.php - 94 lines - 3296 bytes - Summary - Text - Print

   1  <?php
   2  namespace Dropbox;
   3  
   4  /**
   5   * Helper functions to validate arguments.
   6   *
   7   * @internal
   8   */
   9  class Checker
  10  {
  11      static function throwError($argName, $argValue, $expectedTypeName)
  12      {
  13          if ($argValue === null) throw new \InvalidArgumentException("'$argName' must not be null");
  14  
  15          if (is_object($argValue)) {
  16              // Class type.
  17              $argTypeName = get_class($argValue);
  18          } else {
  19              // Built-in type.
  20              $argTypeName = gettype($argValue);
  21          }
  22          throw new \InvalidArgumentException("'$argName' has bad type; expecting $expectedTypeName, got $argTypeName");
  23      }
  24  
  25      static function argResource($argName, $argValue)
  26      {
  27          if (!is_resource($argValue)) self::throwError($argName, $argValue, "resource");
  28      }
  29  
  30      static function argCallable($argName, $argValue)
  31      {
  32          if (!is_callable($argValue)) self::throwError($argName, $argValue, "callable");
  33      }
  34  
  35      static function argBool($argName, $argValue)
  36      {
  37          if (!is_bool($argValue)) self::throwError($argName, $argValue, "boolean");
  38      }
  39  
  40      static function argArray($argName, $argValue)
  41      {
  42          if (!is_array($argValue)) self::throwError($argName, $argValue, "array");
  43      }
  44  
  45      static function argString($argName, $argValue)
  46      {
  47          if (!is_string($argValue)) self::throwError($argName, $argValue, "string");
  48      }
  49  
  50      static function argStringOrNull($argName, $argValue)
  51      {
  52          if ($argValue === null) return;
  53          if (!is_string($argValue)) self::throwError($argName, $argValue, "string");
  54      }
  55  
  56      static function argStringNonEmpty($argName, $argValue)
  57      {
  58          if (!is_string($argValue)) self::throwError($argName, $argValue, "string");
  59          if (strlen($argValue) === 0) throw new \InvalidArgumentException("'$argName' must be non-empty");
  60      }
  61  
  62      static function argStringNonEmptyOrNull($argName, $argValue)
  63      {
  64          if ($argValue === null) return;
  65          if (!is_string($argValue)) self::throwError($argName, $argValue, "string");
  66          if (strlen($argValue) === 0) throw new \InvalidArgumentException("'$argName' must be non-empty");
  67      }
  68  
  69      static function argNat($argName, $argValue)
  70      {
  71          if (!is_int($argValue)) self::throwError($argName, $argValue, "int");
  72          if ($argValue < 0) throw new \InvalidArgumentException("'$argName' must be non-negative (you passed in $argValue)");
  73      }
  74  
  75      static function argNatOrNull($argName, $argValue)
  76      {
  77          if ($argValue === null) return;
  78          if (!is_int($argValue)) self::throwError($argName, $argValue, "int");
  79          if ($argValue < 0) throw new \InvalidArgumentException("'$argName' must be non-negative (you passed in $argValue)");
  80      }
  81  
  82      static function argIntPositive($argName, $argValue)
  83      {
  84          if (!is_int($argValue)) self::throwError($argName, $argValue, "int");
  85          if ($argValue < 1) throw new \InvalidArgumentException("'$argName' must be positive (you passed in $argValue)");
  86      }
  87  
  88      static function argIntPositiveOrNull($argName, $argValue)
  89      {
  90          if ($argValue === null) return;
  91          if (!is_int($argValue)) self::throwError($argName, $argValue, "int");
  92          if ($argValue < 1) throw new \InvalidArgumentException("'$argName' must be positive (you passed in $argValue)");
  93      }
  94  }

title

Description

title

Description

title

Description

title

title

Body