web/lib/Zend/Service/WindowsAzure/Storage/TableEntity.php
changeset 64 162c1de6545a
parent 19 1c2f13fd785c
child 68 ecaf28ffe26e
equal deleted inserted replaced
63:5b37998e522e 64:162c1de6545a
       
     1 <?php
       
     2 /**
       
     3  * Zend Framework
       
     4  *
       
     5  * LICENSE
       
     6  *
       
     7  * This source file is subject to the new BSD license that is bundled
       
     8  * with this package in the file LICENSE.txt.
       
     9  * It is also available through the world-wide-web at this URL:
       
    10  * http://framework.zend.com/license/new-bsd
       
    11  * If you did not receive a copy of the license and are unable to
       
    12  * obtain it through the world-wide-web, please send an email
       
    13  * to license@zend.com so we can send you a copy immediately.
       
    14  *
       
    15  * @category   Zend
       
    16  * @package    Zend_Service_WindowsAzure
       
    17  * @subpackage Storage
       
    18  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    19  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    20  * @version    $Id: TableEntity.php 23167 2010-10-19 17:53:31Z mabe $
       
    21  */
       
    22 
       
    23 /**
       
    24  * @see Zend_Service_WindowsAzure_Exception
       
    25  */
       
    26 require_once 'Zend/Service/WindowsAzure/Exception.php';
       
    27 
       
    28 
       
    29 /**
       
    30  * @category   Zend
       
    31  * @package    Zend_Service_WindowsAzure
       
    32  * @subpackage Storage
       
    33  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    34  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    35  */
       
    36 class Zend_Service_WindowsAzure_Storage_TableEntity
       
    37 {
       
    38     const DEFAULT_TIMESTAMP = '1900-01-01T00:00:00';
       
    39 
       
    40     /**
       
    41      * Partition key
       
    42      * 
       
    43      * @var string
       
    44      */
       
    45     protected $_partitionKey;
       
    46     
       
    47     /**
       
    48      * Row key
       
    49      * 
       
    50      * @var string
       
    51      */
       
    52     protected $_rowKey;
       
    53     
       
    54     /**
       
    55      * Timestamp
       
    56      * 
       
    57      * @var string
       
    58      */
       
    59     protected $_timestamp;
       
    60     
       
    61     /**
       
    62      * Etag
       
    63      * 
       
    64      * @var string
       
    65      */
       
    66     protected $_etag = '';
       
    67     
       
    68     /**
       
    69      * Constructor
       
    70      * 
       
    71      * @param string  $partitionKey    Partition key
       
    72      * @param string  $rowKey          Row key
       
    73      */
       
    74     public function __construct($partitionKey = '', $rowKey = '') 
       
    75     {	        
       
    76         $this->_partitionKey = $partitionKey;
       
    77         $this->_rowKey       = $rowKey;
       
    78     }
       
    79     
       
    80     /**
       
    81      * Get partition key
       
    82      * 
       
    83      * @azure PartitionKey
       
    84      * @return string
       
    85      */
       
    86     public function getPartitionKey()
       
    87     {
       
    88         return $this->_partitionKey;
       
    89     }
       
    90     
       
    91     /**
       
    92      * Set partition key
       
    93      * 
       
    94      * @azure PartitionKey
       
    95      * @param string $value
       
    96      */
       
    97     public function setPartitionKey($value)
       
    98     {
       
    99         $this->_partitionKey = $value;
       
   100     }
       
   101     
       
   102     /**
       
   103      * Get row key
       
   104      * 
       
   105      * @azure RowKey
       
   106      * @return string
       
   107      */
       
   108     public function getRowKey()
       
   109     {
       
   110         return $this->_rowKey;
       
   111     }
       
   112     
       
   113     /**
       
   114      * Set row key
       
   115      * 
       
   116      * @azure RowKey
       
   117      * @param string $value
       
   118      */
       
   119     public function setRowKey($value)
       
   120     {
       
   121         $this->_rowKey = $value;
       
   122     }
       
   123     
       
   124     /**
       
   125      * Get timestamp
       
   126      * 
       
   127      * @azure Timestamp Edm.DateTime
       
   128      * @return string
       
   129      */
       
   130     public function getTimestamp()
       
   131     {
       
   132         if (null === $this->_timestamp) {
       
   133             $this->setTimestamp(self::DEFAULT_TIMESTAMP);
       
   134         }
       
   135         return $this->_timestamp;
       
   136     }
       
   137     
       
   138     /**
       
   139      * Set timestamp
       
   140      * 
       
   141      * @azure Timestamp Edm.DateTime
       
   142      * @param string $value
       
   143      */
       
   144     public function setTimestamp($value = '1900-01-01T00:00:00')
       
   145     {
       
   146         $this->_timestamp = $value;
       
   147     }
       
   148     
       
   149     /**
       
   150      * Get etag
       
   151      * 
       
   152      * @return string
       
   153      */
       
   154     public function getEtag()
       
   155     {
       
   156         return $this->_etag;
       
   157     }
       
   158     
       
   159     /**
       
   160      * Set etag
       
   161      * 
       
   162      * @param string $value
       
   163      */
       
   164     public function setEtag($value = '')
       
   165     {
       
   166         $this->_etag = $value;
       
   167     }
       
   168     
       
   169     /**
       
   170      * Get Azure values
       
   171      * 
       
   172      * @return array
       
   173      */
       
   174     public function getAzureValues()
       
   175     {
       
   176         // Get accessors
       
   177         $accessors = self::getAzureAccessors(get_class($this));
       
   178         
       
   179         // Loop accessors and retrieve values
       
   180         $returnValue = array();
       
   181         foreach ($accessors as $accessor) {
       
   182             if ($accessor->EntityType == 'ReflectionProperty') {
       
   183                 $property = $accessor->EntityAccessor;
       
   184                 $returnValue[] = (object)array(
       
   185                     'Name'  => $accessor->AzurePropertyName,
       
   186                 	'Type'  => $accessor->AzurePropertyType,
       
   187                 	'Value' => $this->$property,
       
   188                 );
       
   189             } else if ($accessor->EntityType == 'ReflectionMethod' && substr(strtolower($accessor->EntityAccessor), 0, 3) == 'get') {
       
   190                 $method = $accessor->EntityAccessor;
       
   191                 $returnValue[] = (object)array(
       
   192                     'Name'  => $accessor->AzurePropertyName,
       
   193                 	'Type'  => $accessor->AzurePropertyType,
       
   194                 	'Value' => $this->$method(),
       
   195                 );
       
   196             }
       
   197         }
       
   198         
       
   199         // Return
       
   200         return $returnValue;
       
   201     }
       
   202     
       
   203     /**
       
   204      * Set Azure values
       
   205      * 
       
   206      * @param array $values
       
   207      * @param boolean $throwOnError Throw Zend_Service_WindowsAzure_Exception when a property is not specified in $values?
       
   208      * @throws Zend_Service_WindowsAzure_Exception
       
   209      */
       
   210     public function setAzureValues($values = array(), $throwOnError = false)
       
   211     {
       
   212         // Get accessors
       
   213         $accessors = self::getAzureAccessors(get_class($this));
       
   214         
       
   215         // Loop accessors and set values
       
   216         $returnValue = array();
       
   217         foreach ($accessors as $accessor) {
       
   218             if (isset($values[$accessor->AzurePropertyName])) {
       
   219                 // Cast to correct type
       
   220                 if ($accessor->AzurePropertyType != '') {
       
   221                     switch (strtolower($accessor->AzurePropertyType)) {
       
   222         	            case 'edm.int32':
       
   223         	            case 'edm.int64':
       
   224         	                $values[$accessor->AzurePropertyName] = intval($values[$accessor->AzurePropertyName]); break;
       
   225         	            case 'edm.boolean':
       
   226         	                if ($values[$accessor->AzurePropertyName] == 'true' || $values[$accessor->AzurePropertyName] == '1')
       
   227         	                    $values[$accessor->AzurePropertyName] = true;
       
   228         	                else
       
   229         	                    $values[$accessor->AzurePropertyName] = false;
       
   230         	                break;
       
   231         	            case 'edm.double':
       
   232         	                $values[$accessor->AzurePropertyName] = floatval($values[$accessor->AzurePropertyName]); break;
       
   233         	        }
       
   234                 }
       
   235                 
       
   236                 // Assign value
       
   237                 if ($accessor->EntityType == 'ReflectionProperty') {
       
   238                     $property = $accessor->EntityAccessor;
       
   239                     $this->$property = $values[$accessor->AzurePropertyName];
       
   240                 } else if ($accessor->EntityType == 'ReflectionMethod' && substr(strtolower($accessor->EntityAccessor), 0, 3) == 'set') {
       
   241                     $method = $accessor->EntityAccessor;
       
   242                     $this->$method($values[$accessor->AzurePropertyName]);
       
   243                 }
       
   244             } else if ($throwOnError) {
       
   245                 throw new Zend_Service_WindowsAzure_Exception("Property '" . $accessor->AzurePropertyName . "' was not found in \$values array");    
       
   246             }
       
   247         }
       
   248         
       
   249         // Return
       
   250         return $returnValue;
       
   251     }
       
   252     
       
   253     /**
       
   254      * Get Azure accessors from class
       
   255      * 
       
   256      * @param string $className Class to get accessors for
       
   257      * @return array
       
   258      */
       
   259     public static function getAzureAccessors($className = '')
       
   260     {
       
   261         // List of accessors
       
   262         $azureAccessors = array();
       
   263         
       
   264         // Get all types
       
   265         $type = new ReflectionClass($className);
       
   266         
       
   267         // Loop all properties
       
   268         $properties = $type->getProperties();
       
   269         foreach ($properties as $property) {
       
   270             $accessor = self::getAzureAccessor($property);
       
   271             if ($accessor !== null) {
       
   272                 $azureAccessors[] = $accessor;
       
   273             }
       
   274         }
       
   275         
       
   276         // Loop all methods
       
   277         $methods = $type->getMethods();
       
   278         foreach ($methods as $method) {
       
   279             $accessor = self::getAzureAccessor($method);
       
   280             if ($accessor !== null) {
       
   281                 $azureAccessors[] = $accessor;
       
   282             }
       
   283         }
       
   284         
       
   285         // Return
       
   286         return $azureAccessors;
       
   287     }
       
   288     
       
   289     /**
       
   290      * Get Azure accessor from reflection member
       
   291      * 
       
   292      * @param ReflectionProperty|ReflectionMethod $member
       
   293      * @return object
       
   294      */
       
   295     public static function getAzureAccessor($member)
       
   296     {
       
   297         // Get comment
       
   298         $docComment = $member->getDocComment();
       
   299         
       
   300         // Check for Azure comment
       
   301         if (strpos($docComment, '@azure') === false)
       
   302         {
       
   303             return null;
       
   304         }
       
   305             
       
   306         // Search for @azure contents
       
   307         $azureComment = '';
       
   308         $commentLines = explode("\n", $docComment);
       
   309         foreach ($commentLines as $commentLine) {
       
   310             if (strpos($commentLine, '@azure') !== false) {
       
   311                 $azureComment = trim(substr($commentLine, strpos($commentLine, '@azure') + 6));
       
   312                 while (strpos($azureComment, '  ') !== false) {
       
   313                     $azureComment = str_replace('  ', ' ', $azureComment);
       
   314                 }
       
   315                 break;
       
   316             }
       
   317         }
       
   318         
       
   319         // Fetch @azure properties
       
   320         $azureProperties = explode(' ', $azureComment);
       
   321         return (object)array(
       
   322             'EntityAccessor'    => $member->getName(),
       
   323             'EntityType'        => get_class($member),
       
   324             'AzurePropertyName' => $azureProperties[0],
       
   325         	'AzurePropertyType' => isset($azureProperties[1]) ? $azureProperties[1] : ''
       
   326         );
       
   327     }
       
   328 }