1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Copyright 2006 - 2013 Eric D. Hough (http://ehough.com) |
4
|
|
|
* |
5
|
|
|
* This file is part of chaingang (https://github.com/ehough/chaingang) |
6
|
|
|
* |
7
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public |
8
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this |
9
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* This class is heavily based on the Commons Chain project. |
14
|
|
|
* http://commons.apache.org/chain |
15
|
|
|
* |
16
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more |
17
|
|
|
* contributor license agreements. See the NOTICE file distributed with |
18
|
|
|
* this work for additional information regarding copyright ownership. |
19
|
|
|
* The ASF licenses this file to You under the Apache License, Version 2.0 |
20
|
|
|
* (the "License"); you may not use this file except in compliance with |
21
|
|
|
* the License. You may obtain a copy of the License at |
22
|
|
|
* |
23
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
24
|
|
|
* |
25
|
|
|
* Unless required by applicable law or agreed to in writing, software |
26
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
27
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
28
|
|
|
* See the License for the specific language governing permissions and |
29
|
|
|
* limitations under the License. |
30
|
|
|
*/ |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* A Context represents the state information that is accessed and manipulated |
34
|
|
|
* by the execution of a Command or a Chain |
35
|
|
|
*/ |
36
|
|
|
class ehough_chaingang_impl_StandardContext implements ehough_chaingang_api_Context |
37
|
|
|
{ |
38
|
|
|
private $_map = array(); |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Removes all mappings from this map. |
42
|
|
|
* |
43
|
|
|
* @return void |
44
|
|
|
*/ |
45
|
1 |
|
public final function clear() |
46
|
|
|
{ |
47
|
1 |
|
$this->_map = array(); |
48
|
1 |
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Returns true if this map contains a mapping for the specified key. |
52
|
|
|
* |
53
|
|
|
* @param string $key Key whose presence in this map is to be tested. |
54
|
|
|
* |
55
|
|
|
* @return bool True if this map contains a mapping for the specified key, false otherwise. |
56
|
|
|
*/ |
57
|
3 |
|
public final function containsKey($key) |
58
|
|
|
{ |
59
|
3 |
|
return array_key_exists($key, $this->_map); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Returns true if this map maps one or more keys to the specified value. |
64
|
|
|
* |
65
|
|
|
* @param mixed $value Value whose presence in this map is to be tested. |
66
|
|
|
* |
67
|
|
|
* @return bool True if this map maps one or more keys to the specified value. |
68
|
|
|
*/ |
69
|
2 |
|
public final function containsValue($value) |
70
|
|
|
{ |
71
|
2 |
|
return array_search($value, $this->_map) !== false; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Returns the value to which this map maps the specified key. |
76
|
|
|
* |
77
|
|
|
* Returns null if the map contains no mapping for this key. A return value of null |
78
|
|
|
* does not necessarily indicate that the map contains no mapping for the key; it's |
79
|
|
|
* also possible that the map explicitly maps the key to null. The containsKey |
80
|
|
|
* operation may be used to distinguish these two cases. |
81
|
|
|
* |
82
|
|
|
* @param string $key Key whose associated value is to be returned. |
83
|
|
|
* |
84
|
|
|
* @return mixed The value to which this map maps the specified key, or null if the |
85
|
|
|
* map contains no mapping for this key. |
86
|
|
|
*/ |
87
|
2 |
|
public final function get($key) |
88
|
|
|
{ |
89
|
2 |
|
if (isset($this->_map[$key])) { |
90
|
|
|
|
91
|
2 |
|
return $this->_map[$key]; |
92
|
|
|
} |
93
|
|
|
|
94
|
2 |
|
return null; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Returns true if this map contains no key-value mappings. |
99
|
|
|
* |
100
|
|
|
* @return bool True if this map contains no key-value mappings. |
101
|
|
|
*/ |
102
|
2 |
|
public final function isEmpty() |
103
|
|
|
{ |
104
|
2 |
|
return $this->size() === 0; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Associates the specified value with the specified key in this map. |
109
|
|
|
* |
110
|
|
|
* If the map previously contained a mapping for this key, the old value |
111
|
|
|
* is replaced by the specified value. |
112
|
|
|
* |
113
|
|
|
* @param string $key Key with which the specified value is to be associated. |
114
|
|
|
* @param mixed $value Value to be associated with the specified key. |
115
|
|
|
* |
116
|
|
|
* @return mixed Previous value associated with specified key, or null if there |
117
|
|
|
* was no mapping for key. A null return can also indicate that |
118
|
|
|
* the map previously associated null with the specified key, if |
119
|
|
|
* the implementation supports null values. |
120
|
|
|
*/ |
121
|
2 |
|
public final function put($key, $value) |
122
|
|
|
{ |
123
|
2 |
|
$previous = $this->get($key); |
124
|
|
|
|
125
|
2 |
|
$this->_map[$key] = $value; |
126
|
|
|
|
127
|
2 |
|
return $previous; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Copies all of the mappings from the specified map to this map. |
132
|
|
|
* |
133
|
|
|
* The effect of this call is equivalent to that of calling put(k, v) on this map |
134
|
|
|
* once for each mapping from key k to value v in the specified map. |
135
|
|
|
* |
136
|
|
|
* @param array $values Mappings to be stored in this map. |
137
|
|
|
* |
138
|
|
|
* @return void |
139
|
|
|
*/ |
140
|
1 |
|
public final function putAll(array $values) |
141
|
|
|
{ |
142
|
1 |
|
if (! is_array($values)) { |
143
|
|
|
|
144
|
|
|
return; |
145
|
|
|
} |
146
|
|
|
|
147
|
1 |
|
foreach ($values as $key => $value) { |
148
|
|
|
|
149
|
1 |
|
$this->put($key, $value); |
150
|
1 |
|
} |
151
|
1 |
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Removes the mapping for this key from this map if it is present. |
155
|
|
|
* |
156
|
|
|
* @param string $key Key whose mapping is to be removed from the map. |
157
|
|
|
* |
158
|
|
|
* @return mixed Previous value associated with specified key, or null if there was no mapping for key. |
159
|
|
|
*/ |
160
|
1 |
|
public final function remove($key) |
161
|
|
|
{ |
162
|
1 |
|
$previous = $this->get($key); |
163
|
|
|
|
164
|
1 |
|
if (isset($this->_map[$key])) { |
165
|
|
|
|
166
|
1 |
|
unset($this->_map[$key]); |
167
|
1 |
|
} |
168
|
|
|
|
169
|
1 |
|
return $previous; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* Returns the number of key-value mappings in this map. |
174
|
|
|
* |
175
|
|
|
* @return int The number of key-value mappings in this map. |
176
|
|
|
*/ |
177
|
2 |
|
public final function size() |
178
|
|
|
{ |
179
|
2 |
|
return count($this->_map); |
180
|
|
|
} |
181
|
|
|
} |