Repository navigation
Expand file tree
/
Copy pathKeySet.php
More file actions
284 lines (253 loc) · 5.99 KB
/
Copy pathKeySet.php
File metadata and controls
284 lines (253 loc) · 5.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
<?php
/**
* Copyright 2016 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
namespace Google\Cloud\Spanner;
use Google\Cloud\Core\ValidateTrait;
/**
* Represents a Cloud Spanner KeySet.
*
* Example:
* ```
* use Google\Cloud\Spanner\SpannerClient;
*
* $spanner = new SpannerClient();
*
* $keySet = $spanner->keySet();
* ```
*
* @see https://cloud.google.com/spanner/reference/rpc/google.spanner.v1#keyset KeySet
*/
class KeySet
{
use ValidateTrait;
/**
* @var array
*/
private $keys;
/**
* @var KeyRange[]
*/
private $ranges;
/**
* @var bool
*/
private $all;
/**
* Create a KeySet.
*
* @param array $options [optional] {
* @type array $keys A list of specific keys. Entries in keys should
* have exactly as many elements as there are columns in the
* primary or index key with which this KeySet is used.
* @type KeyRange[] $ranges A list of Key Ranges.
* @type bool $all If true, KeySet will match all keys in a table.
* **Defaults to** `false`.
* }
*/
public function __construct(array $options = [])
{
$options += [
'keys' => [],
'ranges' => [],
'all' => false
];
$this->validateBatch($options['ranges'], KeyRange::class);
if (!is_array($options['keys'])) {
throw new \InvalidArgumentException('$options.keys must be an array.');
}
if (!is_bool($options['all'])) {
throw new \InvalidArgumentException('$options.all must be a boolean.');
}
$this->keys = $options['keys'];
$this->ranges = $options['ranges'];
$this->all = (bool) $options['all'];
}
/**
* Fetch the KeyRanges
*
* Example:
* ```
* $ranges = $keySet->ranges();
* ```
*
* @return KeyRange[]
*/
public function ranges()
{
return $this->ranges;
}
/**
* Add a single KeyRange.
*
* Example:
* ```
* $range = new KeyRange();
* $keySet->addRange($range);
* ```
*
* @param KeyRange $range A KeyRange instance.
* @return void
*/
public function addRange(KeyRange $range)
{
$this->ranges[] = $range;
}
/**
* Set the KeySet's KeyRanges.
*
* Any existing KeyRanges will be overridden.
*
* Example:
* ```
* $range = new KeyRange();
* $keySet->setRanges([$range]);
* ```
*
* @param KeyRange[] $ranges An array of KeyRange objects.
* @return void
*/
public function setRanges(array $ranges)
{
$this->validateBatch($ranges, KeyRange::class);
$this->ranges = $ranges;
}
/**
* Fetch the keys.
*
* Example:
* ```
* $keys = $keySet->keys();
* ```
*
* @return mixed[]
*/
public function keys()
{
return $this->keys;
}
/**
* Add a single key.
*
* A Key should have exactly as many elements as there are columns in the
* primary or index key with which this KeySet is used.
*
* Example:
* ```
* $keySet->addKey('Bob');
* ```
*
* @param mixed $key The Key to add.
* @return void
*/
public function addKey($key)
{
$this->keys[] = $key;
}
/**
* Set the KeySet keys.
*
* Any existing keys will be overridden.
*
* Example:
* ```
* $keySet->setKeys(['Bob', 'Jill']);
* ```
*
* @param mixed[] $keys
* @return void
*/
public function setKeys(array $keys)
{
$this->keys = $keys;
}
/**
* Get the value of Match All.
*
* Example:
* ```
* if ($keySet->matchAll()) {
* echo "All keys will match";
* }
* ```
*
* @return bool
*/
public function matchAll()
{
return $this->all;
}
/**
* Choose whether the KeySet should match all keys in a table.
*
* Example:
* ```
* $keySet->setMatchAll(true);
* ```
*
* @param bool $all If true, all keys in a table will be matched.
* @return void
*/
public function setMatchAll($all)
{
$this->all = (bool) $all;
}
/**
* Format a KeySet object for use in the Spanner API.
*
* @access private
*/
public function keySetObject()
{
$ranges = [];
foreach ($this->ranges as $range) {
$ranges[] = $range->keyRangeObject();
}
$set = [];
if ($this->all) {
$set['all'] = true;
}
if ($this->keys) {
$set['keys'] = $this->keys;
}
if ($ranges) {
$set['ranges'] = $ranges;
}
return $set;
}
/**
* Create a KeySet from an array created by {@see Google\Cloud\Spanner\KeySet::keySetObject()}.
*
* @param array $keySet An array of KeySet data.
* @return KeySet
* @access private
*/
public static function fromArray(array $keySet)
{
$res = new self();
if (isset($keySet['all'])) {
$res->setMatchAll($keySet['all']);
}
if (isset($keySet['keys'])) {
$res->setKeys($keySet['keys']);
}
if (isset($keySet['ranges'])) {
foreach ($keySet['ranges'] as $range) {
$res->addRange(KeyRange::fromArray($range));
}
}
return $res;
}
}