1<?php
2
3/*
4 * This file is part of the Symfony package.
5 *
6 * (c) Fabien Potencier <fabien@symfony.com>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12namespace Symfony\Component\Process;
13
14use Symfony\Component\Process\Exception\InvalidArgumentException;
15
16/**
17 * ProcessUtils is a bunch of utility methods.
18 *
19 * This class contains static methods only and is not meant to be instantiated.
20 *
21 * @author Martin Hasoň <martin.hason@gmail.com>
22 */
23class ProcessUtils
24{
25    /**
26     * This class should not be instantiated.
27     */
28    private function __construct()
29    {
30    }
31
32    /**
33     * Validates and normalizes a Process input.
34     *
35     * @param string $caller The name of method call that validates the input
36     * @param mixed  $input  The input to validate
37     *
38     * @return mixed
39     *
40     * @throws InvalidArgumentException In case the input is not valid
41     */
42    public static function validateInput(string $caller, $input)
43    {
44        if (null !== $input) {
45            if (\is_resource($input)) {
46                return $input;
47            }
48            if (\is_string($input)) {
49                return $input;
50            }
51            if (is_scalar($input)) {
52                return (string) $input;
53            }
54            if ($input instanceof Process) {
55                return $input->getIterator($input::ITER_SKIP_ERR);
56            }
57            if ($input instanceof \Iterator) {
58                return $input;
59            }
60            if ($input instanceof \Traversable) {
61                return new \IteratorIterator($input);
62            }
63
64            throw new InvalidArgumentException(sprintf('"%s" only accepts strings, Traversable objects or stream resources.', $caller));
65        }
66
67        return $input;
68    }
69}
70