1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Psi\Component\ContentType\Standard\View; |
6
|
|
|
|
7
|
|
|
use Psi\Component\ContentType\View\ViewInterface; |
8
|
|
|
|
9
|
|
|
class ObjectView implements ViewInterface, \ArrayAccess, \Iterator |
10
|
|
|
{ |
11
|
|
|
private $viewClosures; |
12
|
|
|
|
13
|
|
|
public function __construct(array $viewClosures) |
14
|
|
|
{ |
15
|
|
|
$this->viewClosures = $viewClosures; |
16
|
|
|
} |
17
|
|
|
|
18
|
|
|
public function offsetGet($name) |
19
|
|
|
{ |
20
|
|
|
if (!isset($this->viewClosures[$name])) { |
21
|
|
|
throw new \InvalidArgumentException(sprintf( |
22
|
|
|
'Child view "%s" has not been set, known children: "%s"', |
23
|
|
|
$name, implode('", "', array_keys($this->viewClosures)) |
24
|
|
|
)); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
return $this->viewClosures[$name](); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
public function offsetExists($name) |
31
|
|
|
{ |
32
|
|
|
return isset($this->viewClosures[$name]); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function offsetSet($name, $value) |
36
|
|
|
{ |
37
|
|
|
throw new \BadMethodCallException( |
38
|
|
|
'Cannot modify an object view.' |
39
|
|
|
); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function offsetUnset($name) |
43
|
|
|
{ |
44
|
|
|
throw new \BadMethodCallException( |
45
|
|
|
'Cannot modify an object view.' |
46
|
|
|
); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function current() |
50
|
|
|
{ |
51
|
|
|
return $this->offsetGet(key($this->viewClosures)); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function key() |
55
|
|
|
{ |
56
|
|
|
return key($this->viewClosures); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function next() |
60
|
|
|
{ |
61
|
|
|
return next($this->viewClosures); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function rewind() |
65
|
|
|
{ |
66
|
|
|
return reset($this->viewClosures); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function valid() |
70
|
|
|
{ |
71
|
|
|
return key($this->viewClosures) !== null; |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|