1: <?php
2: /**
3: * PHP version 5
4: *
5: * @package generalDriver
6: * @author Christian Schiffler <c.schiffler@cyberspectrum.de>
7: * @author Stefan Heimes <stefan_heimes@hotmail.com>
8: * @author Tristan Lins <tristan.lins@bit3.de>
9: * @copyright The MetaModels team.
10: * @license LGPL.
11: * @filesource
12: */
13:
14: namespace DcGeneral\DataDefinition\Definition\View;
15:
16: use DcGeneral\Exception\DcGeneralInvalidArgumentException;
17:
18: /**
19: * Class CommandCollection.
20: *
21: * Implementation of a command collection.
22: *
23: * @package DcGeneral\DataDefinition\Definition\View
24: */
25: class CommandCollection implements CommandCollectionInterface
26: {
27: /**
28: * The commands contained within the collection.
29: *
30: * @var CommandInterface[]
31: */
32: protected $commands = array();
33:
34: /**
35: * {@inheritdoc}
36: */
37: public function clearCommands()
38: {
39: $this->commands = array();
40:
41: return $this;
42: }
43:
44: /**
45: * {@inheritdoc}
46: */
47: public function setCommands(array $commands)
48: {
49: $this->clearCommands();
50: $this->addCommands($commands);
51:
52: return $this;
53: }
54:
55: /**
56: * {@inheritdoc}
57: */
58: public function addCommands(array $commands)
59: {
60: foreach ($commands as $command)
61: {
62: $this->addCommand($command);
63: }
64:
65: return $this;
66: }
67:
68: /**
69: * {@inheritdoc}
70: */
71: public function removeCommands(array $commands)
72: {
73: foreach ($commands as $command)
74: {
75: $this->removeCommand($command);
76: }
77:
78: return $this;
79: }
80:
81: /**
82: * {@inheritdoc}
83: */
84: public function hasCommand(CommandInterface $command)
85: {
86: $hash = spl_object_hash($command);
87:
88: return isset($this->commands[$hash]);
89: }
90:
91: /**
92: * {@inheritdoc}
93: */
94: public function hasCommandNamed($name)
95: {
96: foreach ($this->commands as $command)
97: {
98: if ($command->getName() == $name)
99: {
100: return true;
101: }
102: }
103:
104: return false;
105: }
106:
107: /**
108: * {@inheritdoc}
109: */
110: public function addCommand(CommandInterface $command)
111: {
112: $hash = spl_object_hash($command);
113:
114: $this->commands[$hash] = $command;
115:
116: return $this;
117: }
118:
119: /**
120: * {@inheritdoc}
121: */
122: public function removeCommand(CommandInterface $command)
123: {
124: $hash = spl_object_hash($command);
125: unset($this->commands[$hash]);
126:
127: return $this;
128: }
129:
130: /**
131: * {@inheritdoc}
132: *
133: * @throws DcGeneralInvalidArgumentException when the requested command could not be found.
134: */
135: public function removeCommandNamed($name)
136: {
137: foreach ($this->commands as $command)
138: {
139: if ($command->getName() == $name)
140: {
141: $this->removeCommand($command);
142:
143: return $this;
144: }
145: }
146:
147: throw new DcGeneralInvalidArgumentException('Command with name ' . $name . ' not found');
148: }
149:
150: /**
151: * {@inheritdoc}
152: *
153: * @throws DcGeneralInvalidArgumentException when the requested command could not be found.
154: */
155: public function getCommandNamed($name)
156: {
157: foreach ($this->commands as $command)
158: {
159: if ($command->getName() == $name)
160: {
161: return $command;
162: }
163: }
164:
165: throw new DcGeneralInvalidArgumentException('Command with name ' . $name . ' not found');
166: }
167:
168: /**
169: * {@inheritdoc}
170: */
171: public function getCommands()
172: {
173: return $this->commands;
174: }
175: }
176: