1 | <?php |
||
8 | class Group implements SerializerFactory, \ArrayAccess, \IteratorAggregate, \Countable { |
||
9 | |||
10 | private $_factories = []; |
||
11 | |||
12 | 9 | public function __construct(array $factories = []) { |
|
13 | 9 | foreach ($factories as $contentType => $factory) { |
|
14 | 6 | $this->offsetSet($contentType, $factory); |
|
15 | } |
||
16 | 9 | } |
|
17 | |||
18 | 3 | public function createSerializer(string $contentType, \DOMNode $node) { |
|
19 | 3 | $serializer = NULL; |
|
20 | 3 | if ($this->offsetExists($contentType)) { |
|
21 | 3 | $factory = $this->offsetGet($contentType); |
|
22 | 3 | if ($factory instanceof SerializerFactory) { |
|
23 | 2 | $serializer = $factory->createSerializer($contentType, $node); |
|
24 | 1 | } elseif (is_callable($factory)) { |
|
25 | 1 | $serializer = $factory($contentType, $node); |
|
26 | } |
||
27 | 3 | if (NULL !== $serializer && !method_exists($serializer, '__toString')) { |
|
28 | 1 | throw new Exceptions\InvalidSerializer($contentType, get_class($serializer)); |
|
29 | } |
||
30 | } |
||
31 | 2 | return $serializer; |
|
32 | } |
||
33 | |||
34 | 8 | private function normalizeContentType($contentType) { |
|
37 | |||
38 | 4 | public function offsetExists($contentType) { |
|
42 | |||
43 | 8 | public function offsetSet($contentType, $factory) { |
|
44 | 8 | $contentType = $this->normalizeContentType($contentType); |
|
45 | 8 | if (!($factory instanceOf SerializerFactory || is_callable($factory))) { |
|
46 | 1 | throw new Exceptions\InvalidArgument( |
|
47 | 1 | 'factory', 'FluentDOM\Serializer\Factory, callable' |
|
48 | ); |
||
49 | } |
||
50 | 7 | $this->_factories[$contentType] = $factory; |
|
51 | 7 | } |
|
52 | |||
53 | 5 | public function offsetGet($contentType) { |
|
57 | |||
58 | 1 | public function offsetUnset($contentType) { |
|
59 | 1 | $contentType = $this->normalizeContentType($contentType); |
|
60 | 1 | if (array_key_exists($contentType, $this->_factories)) { |
|
61 | 1 | unset($this->_factories[$contentType]); |
|
62 | } |
||
63 | 1 | } |
|
64 | |||
65 | 1 | public function getIterator() { |
|
68 | |||
69 | 2 | public function count() { |
|
72 | } |
||
73 | } |
||
74 |