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 $template; |
12
|
|
|
private $viewClosures; |
13
|
|
|
|
14
|
|
|
public function __construct(string $template, array $viewClosures) |
15
|
|
|
{ |
16
|
|
|
$this->template = $template; |
17
|
|
|
$this->viewClosures = $viewClosures; |
18
|
|
|
} |
19
|
|
|
|
20
|
|
View Code Duplication |
public function offsetGet($name) |
|
|
|
|
21
|
|
|
{ |
22
|
|
|
if (!isset($this->viewClosures[$name])) { |
23
|
|
|
throw new \InvalidArgumentException(sprintf( |
24
|
|
|
'Child view "%s" has not been set, known children: "%s"', |
25
|
|
|
$name, implode('", "', array_keys($this->viewClosures)) |
26
|
|
|
)); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
return $this->viewClosures[$name](); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function offsetExists($name) |
33
|
|
|
{ |
34
|
|
|
return isset($this->viewClosures[$name]); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function offsetSet($name, $value) |
38
|
|
|
{ |
39
|
|
|
throw new \BadMethodCallException( |
40
|
|
|
'Cannot modify an object view.' |
41
|
|
|
); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function offsetUnset($name) |
45
|
|
|
{ |
46
|
|
|
throw new \BadMethodCallException( |
47
|
|
|
'Cannot modify an object view.' |
48
|
|
|
); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function current() |
52
|
|
|
{ |
53
|
|
|
return $this->offsetGet(key($this->viewClosures)); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function key() |
57
|
|
|
{ |
58
|
|
|
return key($this->viewClosures); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function next() |
62
|
|
|
{ |
63
|
|
|
return next($this->viewClosures); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function rewind() |
67
|
|
|
{ |
68
|
|
|
return reset($this->viewClosures); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function valid() |
72
|
|
|
{ |
73
|
|
|
return key($this->viewClosures) !== null; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function getTemplate(): string |
77
|
|
|
{ |
78
|
|
|
return $this->template; |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.