fbab8f1073584820ce0f46ac6b0312bbd1dabced
[GitHub/WoltLab/WCF.git] /
1 <?php
2
3 /**
4 * @see https://github.com/laminas/laminas-stdlib for the canonical source repository
5 * @copyright https://github.com/laminas/laminas-stdlib/blob/master/COPYRIGHT.md
6 * @license https://github.com/laminas/laminas-stdlib/blob/master/LICENSE.md New BSD License
7 */
8
9 namespace Laminas\Stdlib\Guard;
10
11 use Traversable;
12
13 /**
14 * Provide a guard method for array or Traversable data
15 */
16 trait ArrayOrTraversableGuardTrait
17 {
18 /**
19 * Verifies that the data is an array or Traversable
20 *
21 * @param mixed $data the data to verify
22 * @param string $dataName the data name
23 * @param string $exceptionClass FQCN for the exception
24 * @throws \Exception
25 */
26 protected function guardForArrayOrTraversable(
27 $data,
28 $dataName = 'Argument',
29 $exceptionClass = 'Laminas\Stdlib\Exception\InvalidArgumentException'
30 ) {
31 if (! is_array($data) && ! ($data instanceof Traversable)) {
32 $message = sprintf(
33 "%s must be an array or Traversable, [%s] given",
34 $dataName,
35 is_object($data) ? get_class($data) : gettype($data)
36 );
37 throw new $exceptionClass($message);
38 }
39 }
40 }