13 * to license@zend.com so we can send you a copy immediately. |
13 * to license@zend.com so we can send you a copy immediately. |
14 * |
14 * |
15 * @category Zend |
15 * @category Zend |
16 * @package Zend_Service_Amazon |
16 * @package Zend_Service_Amazon |
17 * @subpackage SimpleDb |
17 * @subpackage SimpleDb |
18 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) |
18 * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com) |
19 * @license http://framework.zend.com/license/new-bsd New BSD License |
19 * @license http://framework.zend.com/license/new-bsd New BSD License |
20 */ |
20 */ |
21 |
21 |
22 /** |
22 /** |
23 * @see Zend_Service_Amazon_Abstract |
23 * @see Zend_Service_Amazon_Abstract |
98 { |
98 { |
99 parent::__construct($accessKey, $secretKey); |
99 parent::__construct($accessKey, $secretKey); |
100 $this->setEndpoint("https://" . $this->_sdbEndpoint); |
100 $this->setEndpoint("https://" . $this->_sdbEndpoint); |
101 } |
101 } |
102 |
102 |
103 /** |
103 /** |
104 * Set SimpleDB endpoint to use |
104 * Set SimpleDB endpoint to use |
105 * |
105 * |
106 * @param string|Zend_Uri_Http $endpoint |
106 * @param string|Zend_Uri_Http $endpoint |
107 * @return Zend_Service_Amazon_SimpleDb |
107 * @return Zend_Service_Amazon_SimpleDb |
108 */ |
108 */ |
109 public function setEndpoint($endpoint) |
109 public function setEndpoint($endpoint) |
110 { |
110 { |
111 if(!($endpoint instanceof Zend_Uri_Http)) { |
111 if(!($endpoint instanceof Zend_Uri_Http)) { |
112 $endpoint = Zend_Uri::factory($endpoint); |
112 $endpoint = Zend_Uri::factory($endpoint); |
113 } |
113 } |
114 if(!$endpoint->valid()) { |
114 if(!$endpoint->valid()) { |
115 require_once 'Zend/Service/Amazon/SimpleDb/Exception.php'; |
115 require_once 'Zend/Service/Amazon/SimpleDb/Exception.php'; |
116 throw new Zend_Service_Amazon_SimpleDb_Exception("Invalid endpoint supplied"); |
116 throw new Zend_Service_Amazon_SimpleDb_Exception("Invalid endpoint supplied"); |
117 } |
117 } |
118 $this->_endpoint = $endpoint; |
118 $this->_endpoint = $endpoint; |
119 return $this; |
119 return $this; |
120 } |
120 } |
121 |
121 |
122 /** |
122 /** |
123 * Get SimpleDB endpoint |
123 * Get SimpleDB endpoint |
124 * |
124 * |
125 * @return Zend_Uri_Http |
125 * @return Zend_Uri_Http |
126 */ |
126 */ |
127 public function getEndpoint() |
127 public function getEndpoint() |
128 { |
128 { |
129 return $this->_endpoint; |
129 return $this->_endpoint; |
130 } |
130 } |
131 |
131 |
132 /** |
132 /** |
133 * Get attributes API method |
133 * Get attributes API method |
134 * |
134 * |
135 * @param string $domainName Domain name within database |
135 * @param string $domainName Domain name within database |
136 * @param string |
136 * @param string |
137 */ |
137 */ |
138 public function getAttributes( |
138 public function getAttributes( |
139 $domainName, $itemName, $attributeName = null |
139 $domainName, $itemName, $attributeName = null |
140 ) { |
140 ) { |
141 $params = array(); |
141 $params = array(); |
142 $params['Action'] = 'GetAttributes'; |
142 $params['Action'] = 'GetAttributes'; |
143 $params['DomainName'] = $domainName; |
143 $params['DomainName'] = $domainName; |
144 $params['ItemName'] = $itemName; |
144 $params['ItemName'] = $itemName; |
145 |
145 |
146 if (isset($attributeName)) { |
146 if (isset($attributeName)) { |
147 $params['AttributeName'] = $attributeName; |
147 $params['AttributeName'] = $attributeName; |
148 } |
148 } |
149 |
149 |
150 $response = $this->_sendRequest($params); |
150 $response = $this->_sendRequest($params); |
151 $document = $response->getSimpleXMLDocument(); |
151 $document = $response->getSimpleXMLDocument(); |
152 |
152 |
153 $attributeNodes = $document->GetAttributesResult->Attribute; |
153 $attributeNodes = $document->GetAttributesResult->Attribute; |
154 |
154 |
155 // Return an array of arrays |
155 // Return an array of arrays |
186 */ |
186 */ |
187 public function putAttributes( |
187 public function putAttributes( |
188 $domainName, $itemName, $attributes, $replace = array() |
188 $domainName, $itemName, $attributes, $replace = array() |
189 ) { |
189 ) { |
190 $params = array(); |
190 $params = array(); |
191 $params['Action'] = 'PutAttributes'; |
191 $params['Action'] = 'PutAttributes'; |
192 $params['DomainName'] = $domainName; |
192 $params['DomainName'] = $domainName; |
193 $params['ItemName'] = $itemName; |
193 $params['ItemName'] = $itemName; |
194 |
194 |
195 $index = 0; |
195 $index = 0; |
196 foreach ($attributes as $attribute) { |
196 foreach ($attributes as $attribute) { |
197 $attributeName = $attribute->getName(); |
197 $attributeName = $attribute->getName(); |
198 foreach ($attribute->getValues() as $value) { |
198 foreach ($attribute->getValues() as $value) { |
199 $params['Attribute.' . $index . '.Name'] = $attributeName; |
199 $params['Attribute.' . $index . '.Name'] = $attributeName; |
200 $params['Attribute.' . $index . '.Value'] = $value; |
200 $params['Attribute.' . $index . '.Value'] = $value; |
201 |
201 |
202 // Check if it should be replaced |
202 // Check if it should be replaced |
203 if(array_key_exists($attributeName, $replace) && $replace[$attributeName]) { |
203 if(array_key_exists($attributeName, $replace) && $replace[$attributeName]) { |
204 $params['Attribute.' . $index . '.Replace'] = 'true'; |
204 $params['Attribute.' . $index . '.Replace'] = 'true'; |
205 } |
205 } |
206 $index++; |
206 $index++; |
207 } |
207 } |
208 } |
208 } |
209 |
209 |
210 // Exception should get thrown if there's an error |
210 // Exception should get thrown if there's an error |
211 $response = $this->_sendRequest($params); |
211 $response = $this->_sendRequest($params); |
212 } |
212 } |
213 |
213 |
214 /** |
214 /** |
215 * Add many attributes at once |
215 * Add many attributes at once |
216 * |
216 * |
217 * @param array $items |
217 * @param array $items |
218 * @param string $domainName |
218 * @param string $domainName |
219 * @param array $replace |
219 * @param array $replace |
220 * @return void |
220 * @return void |
221 */ |
221 */ |
222 public function batchPutAttributes($items, $domainName, array $replace = array()) |
222 public function batchPutAttributes($items, $domainName, array $replace = array()) |
223 { |
223 { |
224 |
224 |
225 $params = array(); |
225 $params = array(); |
226 $params['Action'] = 'BatchPutAttributes'; |
226 $params['Action'] = 'BatchPutAttributes'; |
227 $params['DomainName'] = $domainName; |
227 $params['DomainName'] = $domainName; |
234 // attribute value cannot be array, so when several items are passed |
234 // attribute value cannot be array, so when several items are passed |
235 // they are treated as separate values with the same attribute name |
235 // they are treated as separate values with the same attribute name |
236 foreach($attribute->getValues() as $value) { |
236 foreach($attribute->getValues() as $value) { |
237 $params['Item.' . $itemIndex . '.Attribute.' . $attributeIndex . '.Name'] = $attribute->getName(); |
237 $params['Item.' . $itemIndex . '.Attribute.' . $attributeIndex . '.Name'] = $attribute->getName(); |
238 $params['Item.' . $itemIndex . '.Attribute.' . $attributeIndex . '.Value'] = $value; |
238 $params['Item.' . $itemIndex . '.Attribute.' . $attributeIndex . '.Value'] = $value; |
239 if (isset($replace[$name]) |
239 if (isset($replace[$name]) |
240 && isset($replace[$name][$attribute->getName()]) |
240 && isset($replace[$name][$attribute->getName()]) |
241 && $replace[$name][$attribute->getName()] |
241 && $replace[$name][$attribute->getName()] |
242 ) { |
242 ) { |
243 $params['Item.' . $itemIndex . '.Attribute.' . $attributeIndex . '.Replace'] = 'true'; |
243 $params['Item.' . $itemIndex . '.Attribute.' . $attributeIndex . '.Replace'] = 'true'; |
244 } |
244 } |
245 $attributeIndex++; |
245 $attributeIndex++; |
251 $response = $this->_sendRequest($params); |
251 $response = $this->_sendRequest($params); |
252 } |
252 } |
253 |
253 |
254 /** |
254 /** |
255 * Delete attributes |
255 * Delete attributes |
256 * |
256 * |
257 * @param string $domainName |
257 * @param string $domainName |
258 * @param string $itemName |
258 * @param string $itemName |
259 * @param array $attributes |
259 * @param array $attributes |
260 * @return void |
260 * @return void |
261 */ |
261 */ |
262 public function deleteAttributes($domainName, $itemName, array $attributes = array()) |
262 public function deleteAttributes($domainName, $itemName, array $attributes = array()) |
263 { |
263 { |
264 $params = array(); |
264 $params = array(); |
265 $params['Action'] = 'DeleteAttributes'; |
265 $params['Action'] = 'DeleteAttributes'; |
266 $params['DomainName'] = $domainName; |
266 $params['DomainName'] = $domainName; |
267 $params['ItemName'] = $itemName; |
267 $params['ItemName'] = $itemName; |
268 |
268 |
269 $attributeIndex = 0; |
269 $attributeIndex = 0; |
270 foreach ($attributes as $attribute) { |
270 foreach ($attributes as $attribute) { |
271 foreach ($attribute->getValues() as $value) { |
271 foreach ($attribute->getValues() as $value) { |
272 $params['Attribute.' . $attributeIndex . '.Name'] = $attribute->getName(); |
272 $params['Attribute.' . $attributeIndex . '.Name'] = $attribute->getName(); |
273 $params['Attribute.' . $attributeIndex . '.Value'] = $value; |
273 $params['Attribute.' . $attributeIndex . '.Value'] = $value; |
274 $attributeIndex++; |
274 $attributeIndex++; |
275 } |
275 } |
276 } |
276 } |
277 |
277 |
278 $response = $this->_sendRequest($params); |
278 $response = $this->_sendRequest($params); |
279 |
279 |
280 return true; |
280 return true; |
281 } |
281 } |
282 |
282 |
283 /** |
283 /** |
284 * List domains |
284 * List domains |
285 * |
285 * |
286 * @param $maxNumberOfDomains int |
286 * @param int $maxNumberOfDomains |
287 * @param $nextToken int |
287 * @param int $nextToken |
288 * @return array 0 or more domain names |
288 * @return array 0 or more domain names |
289 */ |
289 */ |
290 public function listDomains($maxNumberOfDomains = 100, $nextToken = null) |
290 public function listDomains($maxNumberOfDomains = 100, $nextToken = null) |
291 { |
291 { |
292 $params = array(); |
292 $params = array(); |
293 $params['Action'] = 'ListDomains'; |
293 $params['Action'] = 'ListDomains'; |
294 $params['MaxNumberOfDomains'] = $maxNumberOfDomains; |
294 $params['MaxNumberOfDomains'] = $maxNumberOfDomains; |
295 |
295 |
296 if (null !== $nextToken) { |
296 if (null !== $nextToken) { |
297 $params['NextToken'] = $nextToken; |
297 $params['NextToken'] = $nextToken; |
298 } |
298 } |
299 $response = $this->_sendRequest($params); |
299 $response = $this->_sendRequest($params); |
300 |
300 |
301 $domainNodes = $response->getSimpleXMLDocument()->ListDomainsResult->DomainName; |
301 $domainNodes = $response->getSimpleXMLDocument()->ListDomainsResult->DomainName; |
302 |
302 |
303 $data = array(); |
303 $data = array(); |
313 } |
313 } |
314 |
314 |
315 /** |
315 /** |
316 * Retrieve domain metadata |
316 * Retrieve domain metadata |
317 * |
317 * |
318 * @param $domainName string Name of the domain for which metadata will be requested |
318 * @param string $domainName Name of the domain for which metadata will be requested |
319 * @return array Key/value array of metadatum names and values. |
319 * @return array Key/value array of metadatum names and values. |
320 */ |
320 */ |
321 public function domainMetadata($domainName) |
321 public function domainMetadata($domainName) |
322 { |
322 { |
323 $params = array(); |
323 $params = array(); |
324 $params['Action'] = 'DomainMetadata'; |
324 $params['Action'] = 'DomainMetadata'; |
325 $params['DomainName'] = $domainName; |
325 $params['DomainName'] = $domainName; |
326 $response = $this->_sendRequest($params); |
326 $response = $this->_sendRequest($params); |
327 |
327 |
328 $document = $response->getSimpleXMLDocument(); |
328 $document = $response->getSimpleXMLDocument(); |
329 |
329 |
330 $metadataNodes = $document->DomainMetadataResult->children(); |
330 $metadataNodes = $document->DomainMetadataResult->children(); |
338 } |
338 } |
339 |
339 |
340 /** |
340 /** |
341 * Create a new domain |
341 * Create a new domain |
342 * |
342 * |
343 * @param $domainName string Valid domain name of the domain to create |
343 * @param string $domainName Valid domain name of the domain to create |
344 * @return boolean True if successful, false if not |
344 * @return boolean True if successful, false if not |
345 */ |
345 */ |
346 public function createDomain($domainName) |
346 public function createDomain($domainName) |
347 { |
347 { |
348 $params = array(); |
348 $params = array(); |
349 $params['Action'] = 'CreateDomain'; |
349 $params['Action'] = 'CreateDomain'; |
350 $params['DomainName'] = $domainName; |
350 $params['DomainName'] = $domainName; |
351 $response = $this->_sendRequest($params); |
351 $response = $this->_sendRequest($params); |
352 return $response->getHttpResponse()->isSuccessful(); |
352 return $response->getHttpResponse()->isSuccessful(); |
353 } |
353 } |
354 |
354 |
355 /** |
355 /** |
356 * Delete a domain |
356 * Delete a domain |
357 * |
357 * |
358 * @param $domainName string Valid domain name of the domain to delete |
358 * @param string $domainName Valid domain name of the domain to delete |
359 * @return boolean True if successful, false if not |
359 * @return boolean True if successful, false if not |
360 */ |
360 */ |
361 public function deleteDomain($domainName) |
361 public function deleteDomain($domainName) |
362 { |
362 { |
363 $params = array(); |
363 $params = array(); |
364 $params['Action'] = 'DeleteDomain'; |
364 $params['Action'] = 'DeleteDomain'; |
365 $params['DomainName'] = $domainName; |
365 $params['DomainName'] = $domainName; |
366 $response = $this->_sendRequest($params); |
366 $response = $this->_sendRequest($params); |
367 return $response->getHttpResponse()->isSuccessful(); |
367 return $response->getHttpResponse()->isSuccessful(); |
368 } |
368 } |
369 |
369 |
370 /** |
370 /** |
372 * |
372 * |
373 * @param string $selectExpression |
373 * @param string $selectExpression |
374 * @param null|string $nextToken |
374 * @param null|string $nextToken |
375 * @return Zend_Service_Amazon_SimpleDb_Page |
375 * @return Zend_Service_Amazon_SimpleDb_Page |
376 */ |
376 */ |
377 public function select($selectExpression, $nextToken = null) |
377 public function select($selectExpression, $nextToken = null) |
378 { |
378 { |
379 $params = array(); |
379 $params = array(); |
380 $params['Action'] = 'Select'; |
380 $params['Action'] = 'Select'; |
381 $params['SelectExpression'] = $selectExpression; |
381 $params['SelectExpression'] = $selectExpression; |
382 |
382 |
383 if (null !== $nextToken) { |
383 if (null !== $nextToken) { |
384 $params['NextToken'] = $nextToken; |
384 $params['NextToken'] = $nextToken; |
385 } |
385 } |
386 |
386 |
387 $response = $this->_sendRequest($params); |
387 $response = $this->_sendRequest($params); |
388 $xml = $response->getSimpleXMLDocument(); |
388 $xml = $response->getSimpleXMLDocument(); |
389 |
389 |
390 $attributes = array(); |
390 $attributes = array(); |
404 |
404 |
405 $nextToken = (string)$xml->NextToken; |
405 $nextToken = (string)$xml->NextToken; |
406 |
406 |
407 return new Zend_Service_Amazon_SimpleDb_Page($attributes, $nextToken); |
407 return new Zend_Service_Amazon_SimpleDb_Page($attributes, $nextToken); |
408 } |
408 } |
409 |
409 |
410 /** |
410 /** |
411 * Quote SDB value |
411 * Quote SDB value |
412 * |
412 * |
413 * Wraps it in '' |
413 * Wraps it in '' |
414 * |
414 * |
415 * @param string $value |
415 * @param string $value |
416 * @return string |
416 * @return string |
417 */ |
417 */ |
418 public function quote($value) |
418 public function quote($value) |
419 { |
419 { |
420 // wrap in single quotes and convert each ' inside to '' |
420 // wrap in single quotes and convert each ' inside to '' |
421 return "'" . str_replace("'", "''", $value) . "'"; |
421 return "'" . str_replace("'", "''", $value) . "'"; |
422 } |
422 } |
423 |
423 |
424 /** |
424 /** |
425 * Quote SDB column or table name |
425 * Quote SDB column or table name |
426 * |
426 * |
427 * Wraps it in `` |
427 * Wraps it in `` |
428 * @param string $name |
428 * @param string $name |
429 * @return string |
429 * @return string |
430 */ |
430 */ |
431 public function quoteName($name) |
431 public function quoteName($name) |
432 { |
432 { |
433 if (preg_match('/^[a-z_$][a-z0-9_$-]*$/i', $name) == false) { |
433 if (preg_match('/^[a-z_$][a-z0-9_$-]*$/i', $name) == false) { |
434 throw new Zend_Service_Amazon_SimpleDb_Exception("Invalid name: can contain only alphanumeric characters, \$ and _"); |
434 throw new Zend_Service_Amazon_SimpleDb_Exception("Invalid name: can contain only alphanumeric characters, \$ and _"); |
435 } |
435 } |
436 return "`$name`"; |
436 return "`$name`"; |
437 } |
437 } |
438 |
438 |
439 /** |
439 /** |
440 * Sends a HTTP request to the SimpleDB service using Zend_Http_Client |
440 * Sends a HTTP request to the SimpleDB service using Zend_Http_Client |
441 * |
441 * |
442 * @param array $params List of parameters to send with the request |
442 * @param array $params List of parameters to send with the request |
443 * @return Zend_Service_Amazon_SimpleDb_Response |
443 * @return Zend_Service_Amazon_SimpleDb_Response |