1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of Graze DataStructure |
4
|
|
|
* |
5
|
|
|
* Copyright (c) 2014 Nature Delivered Ltd. <http://graze.com> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
* |
10
|
|
|
* @see http://github.com/graze/data-structure/blob/master/LICENSE |
11
|
|
|
* @link http://github.com/graze/data-structure |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Graze\DataStructure\Container; |
15
|
|
|
|
16
|
|
|
class ImmutableContainer extends Container |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @param array $params |
20
|
|
|
*/ |
21
|
18 |
|
public function __construct(array $params = []) |
22
|
|
|
{ |
23
|
18 |
|
parent::__construct([]); |
24
|
|
|
|
25
|
18 |
|
foreach ($params as $key => $value) { |
26
|
15 |
|
$this->setParameter($key, $value); |
27
|
|
|
} |
28
|
18 |
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @param string $key |
32
|
|
|
* @param mixed $value |
33
|
|
|
* |
34
|
|
|
* @return ContainerInterface |
35
|
|
|
*/ |
36
|
6 |
|
public function set($key, $value) |
37
|
|
|
{ |
38
|
6 |
|
$cont = clone $this; |
39
|
6 |
|
$cont->setParameter($key, $value); |
40
|
|
|
|
41
|
6 |
|
return $cont; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Clone the returned value to ensure any modification does not change our version |
46
|
|
|
* |
47
|
|
|
* @param string $key |
48
|
|
|
* |
49
|
|
|
* @return mixed |
50
|
|
|
*/ |
51
|
4 |
|
public function get($key) |
52
|
|
|
{ |
53
|
4 |
|
return $this->recursiveClone(parent::get($key)); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param string $key |
58
|
|
|
* |
59
|
|
|
* @return ContainerInterface |
60
|
|
|
*/ |
61
|
5 |
|
public function remove($key) |
62
|
|
|
{ |
63
|
5 |
|
if ($this->has($key)) { |
64
|
4 |
|
$cont = clone $this; |
65
|
4 |
|
$cont->removeParameter($key); |
66
|
|
|
|
67
|
4 |
|
return $cont; |
68
|
|
|
} |
69
|
|
|
|
70
|
1 |
|
return $this; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param string $key |
75
|
|
|
* @param mixed $value |
76
|
|
|
* |
77
|
|
|
* @return ContainerInterface |
78
|
|
|
*/ |
79
|
17 |
|
protected function setParameter($key, $value) |
80
|
|
|
{ |
81
|
17 |
|
return parent::set($key, $this->recursiveClone($value)); |
|
|
|
|
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param string $key |
86
|
|
|
* |
87
|
|
|
* @return ContainerInterface |
88
|
|
|
*/ |
89
|
4 |
|
protected function removeParameter($key) |
90
|
|
|
{ |
91
|
4 |
|
return parent::remove($key); |
|
|
|
|
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
This check looks for a call to a parent method whose name is different than the method from which it is called.
Consider the following code:
The
getFirstName()
method in theSon
calls the wrong method in the parent class.