1 | <?php |
||
22 | class HelperSet implements \IteratorAggregate |
||
23 | { |
||
24 | private $helpers = array(); |
||
25 | private $command; |
||
26 | |||
27 | /** |
||
28 | * Constructor. |
||
29 | * |
||
30 | * @param Helper[] $helpers An array of helper. |
||
31 | */ |
||
32 | public function __construct(array $helpers = array()) |
||
33 | { |
||
34 | foreach ($helpers as $alias => $helper) { |
||
35 | $this->set($helper, is_int($alias) ? null : $alias); |
||
36 | } |
||
37 | } |
||
38 | |||
39 | /** |
||
40 | * Sets a helper. |
||
41 | * |
||
42 | * @param HelperInterface $helper The helper instance |
||
43 | * @param string $alias An alias |
||
44 | */ |
||
45 | public function set(HelperInterface $helper, $alias = null) |
||
46 | { |
||
47 | $this->helpers[$helper->getName()] = $helper; |
||
48 | if (null !== $alias) { |
||
49 | $this->helpers[$alias] = $helper; |
||
50 | } |
||
51 | |||
52 | $helper->setHelperSet($this); |
||
53 | } |
||
54 | |||
55 | /** |
||
56 | * Returns true if the helper if defined. |
||
57 | * |
||
58 | * @param string $name The helper name |
||
59 | * |
||
60 | * @return bool true if the helper is defined, false otherwise |
||
61 | */ |
||
62 | public function has($name) |
||
66 | |||
67 | /** |
||
68 | * Gets a helper value. |
||
69 | * |
||
70 | * @param string $name The helper name |
||
71 | * |
||
72 | * @return HelperInterface The helper instance |
||
73 | * |
||
74 | * @throws InvalidArgumentException if the helper is not defined |
||
75 | */ |
||
76 | public function get($name) |
||
84 | |||
85 | /** |
||
86 | * Sets the command associated with this helper set. |
||
87 | * |
||
88 | * @param Command $command A Command instance |
||
89 | */ |
||
90 | public function setCommand(Command $command = null) |
||
94 | |||
95 | /** |
||
96 | * Gets the command associated with this helper set. |
||
97 | * |
||
98 | * @return Command A Command instance |
||
99 | */ |
||
100 | public function getCommand() |
||
104 | |||
105 | public function getIterator() |
||
106 | { |
||
107 | return new \ArrayIterator($this->helpers); |
||
108 | } |
||
109 | } |
||
110 |