Source for file Model.php

Documentation is available at Model.php

  1. <?php
  2. /** 
  3.  *  PHP Version 5
  4.  *
  5.  *  @category    Amazon
  6.  *  @package     Amazon_SimpleDB
  7.  *  @copyright   Copyright 2008 Amazon Technologies, Inc.
  8.  *  @link        http://aws.amazon.com
  9.  *  @license     http://aws.amazon.com/apache2.0  Apache License, Version 2.0
  10.  *  @version     2007-11-07
  11.  */
  12. /******************************************************************************* 
  13.  *    __  _    _  ___ 
  14.  *   (  )( \/\/ )/ __)
  15.  *   /__\ \    / \__ \
  16.  *  (_)(_) \/\/  (___/
  17.  * 
  18.  *  Amazon Simple DB PHP5 Library
  19.  *  Generated: Thu Aug 28 06:20:51 PDT 2008
  20.  * 
  21.  */
  22.  
  23. /**
  24.  * Amazon_SimpleDB_Model - base class for all model classes
  25.  */ 
  26. abstract class Amazon_SimpleDB_Model
  27. {
  28.     
  29.     /** @var array */
  30.     protected  $_fields = array ();
  31.           
  32.     /**
  33.      * Construct new model class
  34.      * 
  35.      * @param mixed $data - DOMElement or Associative Array to construct from.
  36.      */
  37.     public function __construct($data null)
  38.     {
  39.         if (!is_null($data)) {
  40.             if ($this->_isAssociativeArray($data)) {
  41.                 $this->_fromAssociativeArray($data);
  42.             elseif ($this->_isDOMElement($data)) {
  43.                 $this->_fromDOMElement($data);
  44.             else {
  45.                 throw new Exception ("Unable to construct from provided data. 
  46.                                 Please be sure to pass associative array or DOMElement");
  47.             }
  48.             
  49.         }
  50.     }
  51.  
  52.     /**
  53.      * Support for virtual properties getters.
  54.      * 
  55.      * Virtual property call example:
  56.      *  
  57.      *   $action->Property
  58.      *   
  59.      * Direct getter(preferred):
  60.      * 
  61.      *   $action->getProperty()
  62.      * 
  63.      * @param string $propertyName name of the property
  64.      */
  65.     public function __get($propertyName)
  66.     {
  67.        $getter "get$propertyName"
  68.        return $this->$getter();
  69.     }
  70.  
  71.     /**
  72.      * Support for virtual properties setters.
  73.      * 
  74.      * Virtual property call example:
  75.      *  
  76.      *   $action->Property  = 'ABC'
  77.      *   
  78.      * Direct setter (preferred):
  79.      * 
  80.      *   $action->setProperty('ABC')
  81.      * 
  82.      * @param string $propertyName name of the property
  83.      */
  84.     public function __set($propertyName$propertyValue)
  85.     {
  86.        $setter "set$propertyName";
  87.        $this->$setter($propertyValue);
  88.        return $this;
  89.     }
  90.  
  91.          
  92.     /**
  93.      * XML fragment representation of this object
  94.      * Note, name of the root determined by caller
  95.      * This fragment returns inner fields representation only
  96.      * @return string XML fragment for this object
  97.      */
  98.     protected function _toXMLFragment(
  99.     {
  100.         $xml "";
  101.         foreach ($this->_fields as $fieldName => $field{
  102.             $fieldValue $field['FieldValue'];
  103.             if (!is_null($fieldValue)) {
  104.                 $fieldType $field['FieldType'];
  105.                 if (is_array($fieldType)) {
  106.                     if ($this->_isComplexType($fieldType[0])) {
  107.                         foreach ($fieldValue as $item{
  108.                             $xml .= "<$fieldName>";
  109.                             $xml .= $item->_toXMLFragment();
  110.                             $xml .= "</$fieldName>";
  111.                         }
  112.                     else {
  113.                         foreach ($fieldValue as $item{
  114.                             $xml .= "<$fieldName>";
  115.                             $xml .= $this->_escapeXML($item);
  116.                             $xml .= "</$fieldName>";
  117.                         }
  118.                     }
  119.                 else {
  120.                     if ($this->_isComplexType($fieldType)) {
  121.                         $xml .= "<$fieldName>";
  122.                         $xml .= $fieldValue->_toXMLFragment();
  123.                         $xml .= "</$fieldName>";
  124.                     else {
  125.                         $xml .= "<$fieldName>";
  126.                         $xml .= $this->_escapeXML($fieldValue);
  127.                         $xml .= "</$fieldName>";
  128.                     }
  129.                 }
  130.             }
  131.         }
  132.         return $xml;
  133.     }
  134.  
  135.  
  136.     /**
  137.      * Escape special XML characters
  138.      * @return string with escaped XML characters
  139.      */
  140.     private function _escapeXML($str
  141.     {
  142.         $from array"&""<"">""'""\"")
  143.         $to array"&amp;""&lt;""&gt;""&#039;""&quot;");
  144.         return str_replace($from$to$str)
  145.     }
  146.  
  147.  
  148.     
  149.     /**
  150.      * Construct from DOMElement
  151.      * 
  152.      * This function iterates over object fields and queries XML
  153.      * for corresponding tag value. If query succeeds, value extracted
  154.      * from xml, and field value properly constructed based on field type.
  155.      *
  156.      * Field types defined as arrays always constructed as arrays,
  157.      * even if XML contains a single element - to make sure that
  158.      * data structure is predictable, and no is_array checks are
  159.      * required.
  160.      * 
  161.      * @param DOMElement $dom XML element to construct from
  162.      */
  163.     private function _fromDOMElement(DOMElement $dom)
  164.     {
  165.         $xpath new DOMXPath($dom->ownerDocument);
  166.         $xpath->registerNamespace('a''http://sdb.amazonaws.com/doc/2007-11-07/');
  167.         
  168.         foreach ($this->_fields as $fieldName => $field{
  169.             $fieldType $field['FieldType'];   
  170.             if (is_array($fieldType)) {
  171.                 if ($this->_isComplexType($fieldType[0])) {
  172.                     $elements $xpath->query("./a:$fieldName"$dom);
  173.                     if ($elements->length >= 1{
  174.                         require_once (str_replace('_'DIRECTORY_SEPARATOR$fieldType[0]".php");
  175.                         foreach ($elements as $element{
  176.                             $this->_fields[$fieldName]['FieldValue'][new $fieldType[0]($element);
  177.                         }
  178.                     
  179.                 else {
  180.                     $elements $xpath->query("./a:$fieldName"$dom);
  181.                     if ($elements->length >= 1{
  182.                         foreach ($elements as $element{
  183.                             $text $xpath->query('./text()'$element);
  184.                             $this->_fields[$fieldName]['FieldValue'][$text->item(0)->data;
  185.                         }
  186.                     }  
  187.                 }
  188.             else {
  189.                 if ($this->_isComplexType($fieldType)) {
  190.                     $elements $xpath->query("./a:$fieldName"$dom);
  191.                     if ($elements->length == 1{
  192.                         require_once (str_replace('_'DIRECTORY_SEPARATOR$fieldType".php");
  193.                         $this->_fields[$fieldName]['FieldValue'new $fieldType($elements->item(0));
  194.                     }   
  195.                 else {
  196.                     $element $xpath->query("./a:$fieldName/text()"$dom);
  197.                     if ($element->length == 1{
  198.                         $this->_fields[$fieldName]['FieldValue'$element->item(0)->data;
  199.                     }
  200.                 }
  201.             }
  202.         }
  203.     }
  204.  
  205.  
  206.     /**
  207.      * Construct from Associative Array
  208.      * 
  209.      * 
  210.      * @param array $array associative array to construct from
  211.      */
  212.     private function _fromAssociativeArray(array $array)
  213.     {
  214.         foreach ($this->_fields as $fieldName => $field{
  215.             $fieldType $field['FieldType'];   
  216.             if (is_array($fieldType)) {
  217.                 if ($this->_isComplexType($fieldType[0])) {
  218.                     if (array_key_exists($fieldName$array)) 
  219.                         $elements $array[$fieldName];
  220.                         if (!$this->_isNumericArray($elements)) {
  221.                             $elements =  array($elements);    
  222.                         }
  223.                         if (count ($elements>= 1{
  224.                             require_once (str_replace('_'DIRECTORY_SEPARATOR$fieldType[0]".php");
  225.                             foreach ($elements as $element{
  226.                                 $this->_fields[$fieldName]['FieldValue'][new $fieldType[0]($element);
  227.                             }
  228.                         }
  229.                     
  230.                 else {
  231.                     if (array_key_exists($fieldName$array)) {
  232.                         $elements $array[$fieldName];
  233.                         if (!$this->_isNumericArray($elements)) {
  234.                             $elements =  array($elements);    
  235.                             }
  236.                         if (count ($elements>= 1{
  237.                             foreach ($elements as $element{
  238.                                 $this->_fields[$fieldName]['FieldValue'][$element;
  239.                             }
  240.                         }  
  241.                     }
  242.                 }
  243.             else {
  244.                 if ($this->_isComplexType($fieldType)) {
  245.                     if (array_key_exists($fieldName$array)) {
  246.                         require_once (str_replace('_'DIRECTORY_SEPARATOR$fieldType".php");
  247.                         $this->_fields[$fieldName]['FieldValue'new $fieldType($array[$fieldName]);
  248.                     }   
  249.                 else {
  250.                     if (array_key_exists($fieldName$array)) {
  251.                         $this->_fields[$fieldName]['FieldValue'$array[$fieldName];
  252.                     }
  253.                 }
  254.             }
  255.         }
  256.     }
  257.  
  258.  
  259.  
  260.     /**
  261.      * Determines if field is complex type
  262.      * 
  263.      * @param string $fieldType field type name
  264.      */
  265.     private function _isComplexType ($fieldType
  266.     {
  267.         return preg_match('/^Amazon_SimpleDB_Model_/'$fieldType);
  268.     }
  269.  
  270.    /**
  271.     * Checks  whether passed variable is an associative array
  272.     *
  273.     * @param mixed $var 
  274.     * @return TRUE if passed variable is an associative array
  275.     */
  276.     private function _isAssociativeArray($var{
  277.         return is_array($var&& array_keys($var!== range(0sizeof($var1);
  278.     }
  279.  
  280.    /**
  281.     * Checks  whether passed variable is DOMElement
  282.     *
  283.     * @param mixed $var 
  284.     * @return TRUE if passed variable is DOMElement
  285.     */
  286.     private function _isDOMElement($var{
  287.         return $var instanceof DOMElement;
  288.     }
  289.  
  290.    /**
  291.     * Checks  whether passed variable is numeric array
  292.     *
  293.     * @param mixed $var 
  294.     * @return TRUE if passed variable is an numeric array
  295.     */
  296.     protected function _isNumericArray($var{
  297.         return is_array($var&& array_keys($var=== range(0sizeof($var1);
  298.     }
  299. }

Documentation generated on Thu, 28 Aug 2008 06:35:52 -0700 by phpDocumentor 1.4.2