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\OptionsResolver;
13
14use Symfony\Component\OptionsResolver\Exception\AccessException;
15
16final class OptionConfigurator
17{
18    private $name;
19    private $resolver;
20
21    public function __construct(string $name, OptionsResolver $resolver)
22    {
23        $this->name = $name;
24        $this->resolver = $resolver;
25        $this->resolver->setDefined($name);
26    }
27
28    /**
29     * Adds allowed types for this option.
30     *
31     * @return $this
32     *
33     * @throws AccessException If called from a lazy option or normalizer
34     */
35    public function allowedTypes(string ...$types): self
36    {
37        $this->resolver->setAllowedTypes($this->name, $types);
38
39        return $this;
40    }
41
42    /**
43     * Sets allowed values for this option.
44     *
45     * @param mixed ...$values One or more acceptable values/closures
46     *
47     * @return $this
48     *
49     * @throws AccessException If called from a lazy option or normalizer
50     */
51    public function allowedValues(...$values): self
52    {
53        $this->resolver->setAllowedValues($this->name, $values);
54
55        return $this;
56    }
57
58    /**
59     * Sets the default value for this option.
60     *
61     * @param mixed $value The default value of the option
62     *
63     * @return $this
64     *
65     * @throws AccessException If called from a lazy option or normalizer
66     */
67    public function default($value): self
68    {
69        $this->resolver->setDefault($this->name, $value);
70
71        return $this;
72    }
73
74    /**
75     * Defines an option configurator with the given name.
76     */
77    public function define(string $option): self
78    {
79        return $this->resolver->define($option);
80    }
81
82    /**
83     * Marks this option as deprecated.
84     *
85     * @param string          $package The name of the composer package that is triggering the deprecation
86     * @param string          $version The version of the package that introduced the deprecation
87     * @param string|\Closure $message The deprecation message to use
88     *
89     * @return $this
90     */
91    public function deprecated(string $package, string $version, $message = 'The option "%name%" is deprecated.'): self
92    {
93        $this->resolver->setDeprecated($this->name, $package, $version, $message);
94
95        return $this;
96    }
97
98    /**
99     * Sets the normalizer for this option.
100     *
101     * @return $this
102     *
103     * @throws AccessException If called from a lazy option or normalizer
104     */
105    public function normalize(\Closure $normalizer): self
106    {
107        $this->resolver->setNormalizer($this->name, $normalizer);
108
109        return $this;
110    }
111
112    /**
113     * Marks this option as required.
114     *
115     * @return $this
116     *
117     * @throws AccessException If called from a lazy option or normalizer
118     */
119    public function required(): self
120    {
121        $this->resolver->setRequired($this->name);
122
123        return $this;
124    }
125
126    /**
127     * Sets an info message for an option.
128     *
129     * @return $this
130     *
131     * @throws AccessException If called from a lazy option or normalizer
132     */
133    public function info(string $info): self
134    {
135        $this->resolver->setInfo($this->name, $info);
136
137        return $this;
138    }
139}
140