1<?php
2/**
3 * Zend Framework (http://framework.zend.com/)
4 *
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
8 */
9
10namespace Zend\Stdlib\Guard;
11
12use Traversable;
13
14/**
15 * Static guard helper class
16 *
17 * Bridges the gap for allowing refactoring until traits can be used by default.
18 *
19 * @deprecated
20 */
21abstract class GuardUtils
22{
23    const DEFAULT_EXCEPTION_CLASS = 'Zend\Stdlib\Exception\InvalidArgumentException';
24
25    /**
26     * Verifies that the data is an array or Traversable
27     *
28     * @param  mixed  $data           the data to verify
29     * @param  string $dataName       the data name
30     * @param  string $exceptionClass FQCN for the exception
31     * @throws \Exception
32     */
33    public static function guardForArrayOrTraversable(
34        $data,
35        $dataName = 'Argument',
36        $exceptionClass = self::DEFAULT_EXCEPTION_CLASS
37    ) {
38        if (!is_array($data) && !($data instanceof Traversable)) {
39            $message = sprintf(
40                '%s must be an array or Traversable, [%s] given',
41                $dataName,
42                is_object($data) ? get_class($data) : gettype($data)
43            );
44            throw new $exceptionClass($message);
45        }
46    }
47
48    /**
49     * Verify that the data is not empty
50     *
51     * @param  mixed  $data           the data to verify
52     * @param  string $dataName       the data name
53     * @param  string $exceptionClass FQCN for the exception
54     * @throws \Exception
55     */
56    public static function guardAgainstEmpty(
57        $data,
58        $dataName = 'Argument',
59        $exceptionClass = self::DEFAULT_EXCEPTION_CLASS
60    ) {
61        if (empty($data)) {
62            $message = sprintf('%s cannot be empty', $dataName);
63            throw new $exceptionClass($message);
64        }
65    }
66
67    /**
68     * Verify that the data is not null
69     *
70     * @param  mixed  $data           the data to verify
71     * @param  string $dataName       the data name
72     * @param  string $exceptionClass FQCN for the exception
73     * @throws \Exception
74     */
75    public static function guardAgainstNull(
76        $data,
77        $dataName = 'Argument',
78        $exceptionClass = self::DEFAULT_EXCEPTION_CLASS
79    ) {
80        if (null === $data) {
81            $message = sprintf('%s cannot be null', $dataName);
82            throw new $exceptionClass($message);
83        }
84    }
85}
86