source: pro-violet-viettel/sourcecode/application/libraries/Doctrine/Symfony/Component/Console/Input/ArrayInput.php @ 345

Last change on this file since 345 was 345, checked in by quyenla, 11 years ago

collaborator page

File size: 5.3 KB
Line 
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\Console\Input;
13
14/**
15 * ArrayInput represents an input provided as an array.
16 *
17 * Usage:
18 *
19 *     $input = new ArrayInput(array('name' => 'foo', '--bar' => 'foobar'));
20 *
21 * @author Fabien Potencier <fabien@symfony.com>
22 *
23 * @api
24 */
25class ArrayInput extends Input
26{
27    private $parameters;
28
29    /**
30     * Constructor.
31     *
32     * @param array           $parameters An array of parameters
33     * @param InputDefinition $definition A InputDefinition instance
34     *
35     * @api
36     */
37    public function __construct(array $parameters, InputDefinition $definition = null)
38    {
39        $this->parameters = $parameters;
40
41        parent::__construct($definition);
42    }
43
44    /**
45     * Returns the first argument from the raw parameters (not parsed).
46     *
47     * @return string The value of the first argument or null otherwise
48     */
49    public function getFirstArgument()
50    {
51        foreach ($this->parameters as $key => $value) {
52            if ($key && '-' === $key[0]) {
53                continue;
54            }
55
56            return $value;
57        }
58    }
59
60    /**
61     * Returns true if the raw parameters (not parsed) contains a value.
62     *
63     * This method is to be used to introspect the input parameters
64     * before it has been validated. It must be used carefully.
65     *
66     * @param string|array $values The values to look for in the raw parameters (can be an array)
67     *
68     * @return Boolean true if the value is contained in the raw parameters
69     */
70    public function hasParameterOption($values)
71    {
72        $values = (array) $values;
73
74        foreach ($this->parameters as $k => $v) {
75            if (!is_int($k)) {
76                $v = $k;
77            }
78
79            if (in_array($v, $values)) {
80                return true;
81            }
82        }
83
84        return false;
85    }
86
87    /**
88     * Returns the value of a raw option (not parsed).
89     *
90     * This method is to be used to introspect the input parameters
91     * before it has been validated. It must be used carefully.
92     *
93     * @param string|array $values The value(s) to look for in the raw parameters (can be an array)
94     * @param mixed $default The default value to return if no result is found
95     *
96     * @return mixed The option value
97     */
98    public function getParameterOption($values, $default = false)
99    {
100        $values = (array) $values;
101
102        foreach ($this->parameters as $k => $v) {
103            if (is_int($k) && in_array($v, $values)) {
104                return true;
105            } elseif (in_array($k, $values)) {
106                return $v;
107            }
108        }
109
110        return $default;
111    }
112
113    /**
114     * Processes command line arguments.
115     */
116    protected function parse()
117    {
118        foreach ($this->parameters as $key => $value) {
119            if ('--' === substr($key, 0, 2)) {
120                $this->addLongOption(substr($key, 2), $value);
121            } elseif ('-' === $key[0]) {
122                $this->addShortOption(substr($key, 1), $value);
123            } else {
124                $this->addArgument($key, $value);
125            }
126        }
127    }
128
129    /**
130     * Adds a short option value.
131     *
132     * @param string $shortcut The short option key
133     * @param mixed  $value    The value for the option
134     *
135     * @throws \RuntimeException When option given doesn't exist
136     */
137    private function addShortOption($shortcut, $value)
138    {
139        if (!$this->definition->hasShortcut($shortcut)) {
140            throw new \InvalidArgumentException(sprintf('The "-%s" option does not exist.', $shortcut));
141        }
142
143        $this->addLongOption($this->definition->getOptionForShortcut($shortcut)->getName(), $value);
144    }
145
146    /**
147     * Adds a long option value.
148     *
149     * @param string $name  The long option key
150     * @param mixed  $value The value for the option
151     *
152     * @throws \InvalidArgumentException When option given doesn't exist
153     * @throws \InvalidArgumentException When a required value is missing
154     */
155    private function addLongOption($name, $value)
156    {
157        if (!$this->definition->hasOption($name)) {
158            throw new \InvalidArgumentException(sprintf('The "--%s" option does not exist.', $name));
159        }
160
161        $option = $this->definition->getOption($name);
162
163        if (null === $value) {
164            if ($option->isValueRequired()) {
165                throw new \InvalidArgumentException(sprintf('The "--%s" option requires a value.', $name));
166            }
167
168            $value = $option->isValueOptional() ? $option->getDefault() : true;
169        }
170
171        $this->options[$name] = $value;
172    }
173
174    /**
175     * Adds an argument value.
176     *
177     * @param string $name  The argument name
178     * @param mixed  $value The value for the argument
179     *
180     * @throws \InvalidArgumentException When argument given doesn't exist
181     */
182    private function addArgument($name, $value)
183    {
184        if (!$this->definition->hasArgument($name)) {
185            throw new \InvalidArgumentException(sprintf('The "%s" argument does not exist.', $name));
186        }
187
188        $this->arguments[$name] = $value;
189    }
190}
Note: See TracBrowser for help on using the repository browser.