Source for file Model.php
Documentation is available at Model.php
* @package Amazon_SimpleDB
* @copyright Copyright 2008 Amazon Technologies, Inc.
* @link http://aws.amazon.com
* @license http://aws.amazon.com/apache2.0 Apache License, Version 2.0
/*******************************************************************************
* Amazon Simple DB PHP5 Library
* Generated: Thu Aug 28 06:20:51 PDT 2008
* Amazon_SimpleDB_Model - base class for all model classes
* Construct new model class
* @param mixed $data - DOMElement or Associative Array to construct from.
if ($this->_isAssociativeArray($data)) {
$this->_fromAssociativeArray($data);
} elseif ($this->_isDOMElement($data)) {
$this->_fromDOMElement($data);
throw
new Exception ("Unable to construct from provided data.
Please be sure to pass associative array or DOMElement");
* Support for virtual properties getters.
* Virtual property call example:
* Direct getter(preferred):
* @param string $propertyName name of the property
public function __get($propertyName)
$getter =
"get$propertyName";
* Support for virtual properties setters.
* Virtual property call example:
* $action->Property = 'ABC'
* Direct setter (preferred):
* $action->setProperty('ABC')
* @param string $propertyName name of the property
public function __set($propertyName, $propertyValue)
$setter =
"set$propertyName";
$this->$setter($propertyValue);
* XML fragment representation of this object
* Note, name of the root determined by caller
* This fragment returns inner fields representation only
* @return string XML fragment for this object
foreach ($this->_fields as $fieldName =>
$field) {
$fieldValue =
$field['FieldValue'];
$fieldType =
$field['FieldType'];
if ($this->_isComplexType($fieldType[0])) {
foreach ($fieldValue as $item) {
$xml .=
$item->_toXMLFragment();
foreach ($fieldValue as $item) {
$xml .=
$this->_escapeXML($item);
if ($this->_isComplexType($fieldType)) {
$xml .=
$fieldValue->_toXMLFragment();
$xml .=
$this->_escapeXML($fieldValue);
* Escape special XML characters
* @return string with escaped XML characters
private function _escapeXML($str)
$from =
array( "&", "<", ">", "'", "\"");
$to =
array( "&", "<", ">", "'", """);
* Construct from DOMElement
* This function iterates over object fields and queries XML
* for corresponding tag value. If query succeeds, value extracted
* from xml, and field value properly constructed based on field type.
* Field types defined as arrays always constructed as arrays,
* even if XML contains a single element - to make sure that
* data structure is predictable, and no is_array checks are
* @param DOMElement $dom XML element to construct from
private function _fromDOMElement(DOMElement $dom)
$xpath =
new DOMXPath($dom->ownerDocument);
$xpath->registerNamespace('a', 'http://sdb.amazonaws.com/doc/2007-11-07/');
foreach ($this->_fields as $fieldName =>
$field) {
$fieldType =
$field['FieldType'];
if ($this->_isComplexType($fieldType[0])) {
$elements =
$xpath->query("./a:$fieldName", $dom);
if ($elements->length >=
1) {
require_once (str_replace('_', DIRECTORY_SEPARATOR, $fieldType[0]) .
".php");
foreach ($elements as $element) {
$this->_fields[$fieldName]['FieldValue'][] =
new $fieldType[0]($element);
$elements =
$xpath->query("./a:$fieldName", $dom);
if ($elements->length >=
1) {
foreach ($elements as $element) {
$text =
$xpath->query('./text()', $element);
$this->_fields[$fieldName]['FieldValue'][] =
$text->item(0)->data;
if ($this->_isComplexType($fieldType)) {
$elements =
$xpath->query("./a:$fieldName", $dom);
if ($elements->length ==
1) {
require_once (str_replace('_', DIRECTORY_SEPARATOR, $fieldType) .
".php");
$this->_fields[$fieldName]['FieldValue'] =
new $fieldType($elements->item(0));
$element =
$xpath->query("./a:$fieldName/text()", $dom);
if ($element->length ==
1) {
$this->_fields[$fieldName]['FieldValue'] =
$element->item(0)->data;
* Construct from Associative Array
* @param array $array associative array to construct from
private function _fromAssociativeArray(array $array)
foreach ($this->_fields as $fieldName =>
$field) {
$fieldType =
$field['FieldType'];
if ($this->_isComplexType($fieldType[0])) {
$elements =
$array[$fieldName];
$elements =
array($elements);
if (count ($elements) >=
1) {
require_once (str_replace('_', DIRECTORY_SEPARATOR, $fieldType[0]) .
".php");
foreach ($elements as $element) {
$this->_fields[$fieldName]['FieldValue'][] =
new $fieldType[0]($element);
$elements =
$array[$fieldName];
$elements =
array($elements);
if (count ($elements) >=
1) {
foreach ($elements as $element) {
$this->_fields[$fieldName]['FieldValue'][] =
$element;
if ($this->_isComplexType($fieldType)) {
require_once (str_replace('_', DIRECTORY_SEPARATOR, $fieldType) .
".php");
$this->_fields[$fieldName]['FieldValue'] =
new $fieldType($array[$fieldName]);
$this->_fields[$fieldName]['FieldValue'] =
$array[$fieldName];
* Determines if field is complex type
* @param string $fieldType field type name
private function _isComplexType ($fieldType)
return preg_match('/^Amazon_SimpleDB_Model_/', $fieldType);
* Checks whether passed variable is an associative array
* @return TRUE if passed variable is an associative array
private function _isAssociativeArray($var) {
* Checks whether passed variable is DOMElement
* @return TRUE if passed variable is DOMElement
private function _isDOMElement($var) {
return $var instanceof
DOMElement;
* Checks whether passed variable is numeric array
* @return TRUE if passed variable is an numeric array
Documentation generated on Thu, 28 Aug 2008 06:35:52 -0700 by phpDocumentor 1.4.2