|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Psi\Component\ContentType\View; |
|
4
|
|
|
|
|
5
|
|
|
class View implements \ArrayAccess, \IteratorAggregate, \Countable |
|
6
|
|
|
{ |
|
7
|
|
|
private $children = []; |
|
8
|
|
|
private $value; |
|
9
|
|
|
private $template; |
|
10
|
|
|
|
|
11
|
|
|
public function __construct(string $template = null) |
|
12
|
|
|
{ |
|
13
|
|
|
$this->template = $template; |
|
14
|
|
|
} |
|
15
|
|
|
|
|
16
|
|
|
public function getTemplate() |
|
17
|
|
|
{ |
|
18
|
|
|
return $this->template; |
|
19
|
|
|
} |
|
20
|
|
|
|
|
21
|
|
View Code Duplication |
public function offsetGet($name) |
|
|
|
|
|
|
22
|
|
|
{ |
|
23
|
|
|
if (!isset($this->children[$name])) { |
|
24
|
|
|
throw new \InvalidArgumentException(sprintf( |
|
25
|
|
|
'View value "%s" has not been set, available children: "%s"', |
|
26
|
|
|
$name, implode('", "', array_keys($this->children)) |
|
27
|
|
|
)); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
return $this->children[$name]; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
public function offsetExists($name) |
|
34
|
|
|
{ |
|
35
|
|
|
return isset($this->children[$name]); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
public function offsetSet($name, $value) |
|
39
|
|
|
{ |
|
40
|
|
|
$this->children[$name] = $value; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
public function offsetUnset($name) |
|
44
|
|
|
{ |
|
45
|
|
|
unset($this->children[$name]); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
public function getIterator() |
|
49
|
|
|
{ |
|
50
|
|
|
return new \ArrayIterator($this->children); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
public function count() |
|
54
|
|
|
{ |
|
55
|
|
|
return count($this->children); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
public function setValue($value) |
|
59
|
|
|
{ |
|
60
|
|
|
$this->value = $value; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
public function getValue() |
|
64
|
|
|
{ |
|
65
|
|
|
return $this->value; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
public function getChildren() |
|
69
|
|
|
{ |
|
70
|
|
|
return $this->children; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
public function __toString() |
|
74
|
|
|
{ |
|
75
|
|
|
if (isset($this->value)) { |
|
76
|
|
|
return $this->value; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
return '<no primary value>'; |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|
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.