1: <?php
2:
3: namespace Guzzle\Service\Resource;
4:
5: use Guzzle\Common\Exception\InvalidArgumentException;
6: use Guzzle\Service\Command\CommandInterface;
7:
8: /**
9: * Factory that utilizes multiple factories for creating iterators
10: */
11: class CompositeResourceIteratorFactory implements ResourceIteratorFactoryInterface
12: {
13: /**
14: * @var array Array of factories
15: */
16: protected $factories;
17:
18: /**
19: * @param array $factories Array of factories used to instantiate iterators
20: */
21: public function __construct(array $factories)
22: {
23: $this->factories = $factories;
24: }
25:
26: /**
27: * {@inheritdoc}
28: */
29: public function build(CommandInterface $command, array $options = array())
30: {
31: if (!($factory = $this->getFactory($command))) {
32: throw new InvalidArgumentException('Iterator was not found for ' . $command->getName());
33: }
34:
35: return $factory->build($command, $options);
36: }
37:
38: /**
39: * {@inheritdoc}
40: */
41: public function canBuild(CommandInterface $command)
42: {
43: return $this->getFactory($command) !== false;
44: }
45:
46: /**
47: * Add a factory to the composite factory
48: *
49: * @param ResourceIteratorFactoryInterface $factory Factory to add
50: *
51: * @return self
52: */
53: public function addFactory(ResourceIteratorFactoryInterface $factory)
54: {
55: $this->factories[] = $factory;
56:
57: return $this;
58: }
59:
60: /**
61: * Get the factory that matches the command object
62: *
63: * @param CommandInterface $command Command retrieving the iterator for
64: *
65: * @return ResourceIteratorFactoryInterface|bool
66: */
67: protected function getFactory(CommandInterface $command)
68: {
69: foreach ($this->factories as $factory) {
70: if ($factory->canBuild($command)) {
71: return $factory;
72: }
73: }
74:
75: return false;
76: }
77: }
78: