3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
10 namespace Zend\Stdlib\Guard
;
15 * Static guard helper class
17 * Bridges the gap for allowing refactoring until traits can be used by default.
21 abstract class GuardUtils
23 const DEFAULT_EXCEPTION_CLASS
= 'Zend\Stdlib\Exception\InvalidArgumentException';
26 * Verifies that the data is an array or Traversable
28 * @param mixed $data the data to verify
29 * @param string $dataName the data name
30 * @param string $exceptionClass FQCN for the exception
33 public static function guardForArrayOrTraversable(
35 $dataName = 'Argument',
36 $exceptionClass = self
::DEFAULT_EXCEPTION_CLASS
38 if (!is_array($data) && !($data instanceof Traversable
)) {
40 '%s must be an array or Traversable, [%s] given',
42 is_object($data) ?
get_class($data) : gettype($data)
44 throw new $exceptionClass($message);
49 * Verify that the data is not empty
51 * @param mixed $data the data to verify
52 * @param string $dataName the data name
53 * @param string $exceptionClass FQCN for the exception
56 public static function guardAgainstEmpty(
58 $dataName = 'Argument',
59 $exceptionClass = self
::DEFAULT_EXCEPTION_CLASS
62 $message = sprintf('%s cannot be empty', $dataName);
63 throw new $exceptionClass($message);
68 * Verify that the data is not null
70 * @param mixed $data the data to verify
71 * @param string $dataName the data name
72 * @param string $exceptionClass FQCN for the exception
75 public static function guardAgainstNull(
77 $dataName = 'Argument',
78 $exceptionClass = self
::DEFAULT_EXCEPTION_CLASS
81 $message = sprintf('%s cannot be null', $dataName);
82 throw new $exceptionClass($message);