forked from aws/aws-sdk-php
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Attribute.php
248 lines (220 loc) · 7.37 KB
/
Attribute.php
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
<?php
/**
* Copyright 2010-2013 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file 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 Aws\DynamoDb\Model;
use Aws\Common\Exception\InvalidArgumentException;
use Aws\DynamoDb\Enum\Type;
use Guzzle\Common\ToArrayInterface;
/**
* Class representing a DynamoDB item attribute. Contains helpers for building
* attributes and arrays of attributes.
*
* @deprecated The new DynamoDB document model, including the new types (L, M,
* BOOL, NULL), is not supported by this class.
*/
class Attribute implements ToArrayInterface
{
/**
* @var string A constant used to express the attribute be formatted for expected conditions
*/
const FORMAT_EXPECTED = 'expected';
/**
* @var string A constant used to express the attribute be formatted for put operations
*/
const FORMAT_PUT = 'put';
/**
* @var string A constant used to express the attribute be formatted for update operations
*/
const FORMAT_UPDATE = 'update';
/**
* @var string The suffix for all string types
*/
const SET_SUFFIX = 'S';
/**
* @var string The DynamoDB attribute type (e.g. N, S, B, NS, SS, BS)
*/
protected $type;
/**
* @var string|array The DynamoDB attribute value
*/
protected $value;
/**
* Creates a DynamoDB attribute, validates it, and prepares the type and
* value. Some objects can be used as values as well. If the object has a
* __toString method or implements the Traversable interface, it can be
* converted to a string or array, respectively.
*
* @param mixed $value The DynamoDB attribute value
* @param int $depth A variable used internally to keep track of recursion
* depth of array processing
*
* @return Attribute
*
* @throws InvalidArgumentException
*/
public static function factory($value, $depth = 0)
{
// Handle deep recursion
if ($depth > 1) {
throw new InvalidArgumentException('Sets must be at most one level deep.');
}
// Handle specific, allowed object types
if ($value instanceof Attribute) {
return $value;
} elseif ($value instanceof \Traversable) {
$value = iterator_to_array($value);
} elseif (is_object($value) && method_exists($value, '__toString')) {
$value = (string) $value;
}
// Ensure that the value is valid
if ($value === null || $value === array() || $value === '') {
// Note: "Empty" values are not allowed except for zero and false.
throw new InvalidArgumentException('The value must not be empty.');
} elseif (is_resource($value) || is_object($value)) {
throw new InvalidArgumentException('The value must be able to be converted to string.');
}
// Create the attribute to return
if (is_int($value) || is_float($value)) {
// Handle numeric values
$attribute = new Attribute((string) $value, Type::NUMBER);
} elseif (is_bool($value)) {
// Handle boolean values
$attribute = new Attribute($value ? '1' : '0', Type::NUMBER);
} elseif (is_array($value) || $value instanceof \Traversable) {
// Handle arrays
$setType = null;
$attribute = new Attribute(array());
// Loop through each value to analyze and prepare it
foreach ($value as $subValue) {
// Recursively get the attribute for the set. The depth param only allows one level of recursion
$subAttribute = static::factory($subValue, $depth + 1);
// The type of each sub-value must be the same, or else the whole array is invalid
if ($setType === null) {
$setType = $subAttribute->type;
} elseif ($setType !== $subAttribute->type) {
throw new InvalidArgumentException('The set did not contain values of a uniform type.');
}
// Save the value for the upstream array
$attribute->value[] = (string) $subAttribute->value;
}
// Make sure the type is changed to be a set type
$attribute->type = $setType . self::SET_SUFFIX;
} else {
$attribute = new Attribute((string) $value);
}
return $attribute;
}
/**
* Instantiates a DynamoDB attribute.
*
* @param string|array $value The DynamoDB attribute value
* @param string $type The DynamoDB attribute type (N, S, B, NS, SS, BS)
*/
public function __construct($value, $type = Type::STRING)
{
$this->setValue($value);
$this->setType($type);
}
/**
* Convert the attribute to a string
*
* @return string
*/
public function __toString()
{
return implode(', ', (array) $this->value);
}
/**
* Retrieve the formatted data
*
* @param string $format The format to apply to the data
*
* @return string The formatted version of the data
*/
public function getFormatted($format = Attribute::FORMAT_PUT)
{
switch ($format) {
case self::FORMAT_EXPECTED:
// no break
case self::FORMAT_UPDATE:
$formatted = array('Value' => array($this->type => $this->value));
break;
case self::FORMAT_PUT:
// no break
default:
$formatted = array($this->type => $this->value);
}
return $formatted;
}
/**
* Retrieve the attribute type
*
* @return string The attribute type
*/
public function getType()
{
return $this->type;
}
/**
* Retrieve the attribute value
*
* @return string The attribute value
*/
public function getValue()
{
return $this->value;
}
/**
* Set the attribute type
*
* @param string $type The attribute type to set
*
* @return self
* @throws InvalidArgumentException
*/
public function setType($type)
{
if (in_array($type, Type::values())) {
$this->type = $type;
} else {
throw new InvalidArgumentException('An attribute type must be a valid DynamoDB type.');
}
return $this;
}
/**
* Set the attribute value
*
* @param string|array $value The attribute value
*
* @return self
* @throws InvalidArgumentException
*/
public function setValue($value)
{
if (is_string($value) || is_array($value)) {
$this->value = $value;
} else {
throw new InvalidArgumentException('An attribute value may only be a string or array.');
}
return $this;
}
/**
* {@inheritdoc}
*/
public function toArray()
{
return $this->getFormatted();
}
}