summaryrefslogtreecommitdiff
path: root/vendor/symfony/console/Helper/HelperSet.php
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/symfony/console/Helper/HelperSet.php')
-rw-r--r--vendor/symfony/console/Helper/HelperSet.php74
1 files changed, 74 insertions, 0 deletions
diff --git a/vendor/symfony/console/Helper/HelperSet.php b/vendor/symfony/console/Helper/HelperSet.php
new file mode 100644
index 0000000..30df9f9
--- /dev/null
+++ b/vendor/symfony/console/Helper/HelperSet.php
@@ -0,0 +1,74 @@
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\Helper;
13
14use Symfony\Component\Console\Exception\InvalidArgumentException;
15
16/**
17 * HelperSet represents a set of helpers to be used with a command.
18 *
19 * @author Fabien Potencier <fabien@symfony.com>
20 *
21 * @implements \IteratorAggregate<string, HelperInterface>
22 */
23class HelperSet implements \IteratorAggregate
24{
25 /** @var array<string, HelperInterface> */
26 private array $helpers = [];
27
28 /**
29 * @param HelperInterface[] $helpers
30 */
31 public function __construct(array $helpers = [])
32 {
33 foreach ($helpers as $alias => $helper) {
34 $this->set($helper, \is_int($alias) ? null : $alias);
35 }
36 }
37
38 public function set(HelperInterface $helper, ?string $alias = null): void
39 {
40 $this->helpers[$helper->getName()] = $helper;
41 if (null !== $alias) {
42 $this->helpers[$alias] = $helper;
43 }
44
45 $helper->setHelperSet($this);
46 }
47
48 /**
49 * Returns true if the helper if defined.
50 */
51 public function has(string $name): bool
52 {
53 return isset($this->helpers[$name]);
54 }
55
56 /**
57 * Gets a helper value.
58 *
59 * @throws InvalidArgumentException if the helper is not defined
60 */
61 public function get(string $name): HelperInterface
62 {
63 if (!$this->has($name)) {
64 throw new InvalidArgumentException(sprintf('The helper "%s" is not defined.', $name));
65 }
66
67 return $this->helpers[$name];
68 }
69
70 public function getIterator(): \Traversable
71 {
72 return new \ArrayIterator($this->helpers);
73 }
74}