File indexing completed on 2024-06-23 05:55:45

0001 <?php
0002 /**
0003  * Zend Framework
0004  *
0005  * LICENSE
0006  *
0007  * This source file is subject to the new BSD license that is bundled
0008  * with this package in the file LICENSE.txt.
0009  * It is also available through the world-wide-web at this URL:
0010  * http://framework.zend.com/license/new-bsd
0011  * If you did not receive a copy of the license and are unable to
0012  * obtain it through the world-wide-web, please send an email
0013  * to license@zend.com so we can send you a copy immediately.
0014  *
0015  * @category   Zend
0016  * @package    Zend_Service_WindowsAzure
0017  * @subpackage Storage
0018  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0019  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0020  * @version    $Id$
0021  */
0022 
0023 /**
0024  * @category   Zend
0025  * @package    Zend_Service_WindowsAzure
0026  * @subpackage Storage
0027  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0028  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0029  */
0030 class Zend_Service_WindowsAzure_Storage_TableEntity
0031 {
0032     /**
0033      * Partition key
0034      * 
0035      * @var string
0036      */
0037     protected $_partitionKey;
0038     
0039     /**
0040      * Row key
0041      * 
0042      * @var string
0043      */
0044     protected $_rowKey;
0045     
0046     /**
0047      * Timestamp
0048      * 
0049      * @var string
0050      */
0051     protected $_timestamp;
0052     
0053     /**
0054      * Etag
0055      * 
0056      * @var string
0057      */
0058     protected $_etag = '';
0059     
0060     /**
0061      * Constructor
0062      * 
0063      * @param string  $partitionKey    Partition key
0064      * @param string  $rowKey          Row key
0065      */
0066     public function __construct($partitionKey = '', $rowKey = '') 
0067     {         
0068         $this->_partitionKey = $partitionKey;
0069         $this->_rowKey       = $rowKey;
0070     }
0071     
0072     /**
0073      * Get partition key
0074      * 
0075      * @azure PartitionKey
0076      * @return string
0077      */
0078     public function getPartitionKey()
0079     {
0080         return $this->_partitionKey;
0081     }
0082     
0083     /**
0084      * Set partition key
0085      * 
0086      * @azure PartitionKey
0087      * @param string $value
0088      */
0089     public function setPartitionKey($value)
0090     {
0091         $this->_partitionKey = $value;
0092     }
0093     
0094     /**
0095      * Get row key
0096      * 
0097      * @azure RowKey
0098      * @return string
0099      */
0100     public function getRowKey()
0101     {
0102         return $this->_rowKey;
0103     }
0104     
0105     /**
0106      * Set row key
0107      * 
0108      * @azure RowKey
0109      * @param string $value
0110      */
0111     public function setRowKey($value)
0112     {
0113         $this->_rowKey = $value;
0114     }
0115     
0116     /**
0117      * Get timestamp
0118      * 
0119      * @azure Timestamp Edm.DateTime
0120      * @return string
0121      */
0122     public function getTimestamp()
0123     {
0124       if (null === $this->_timestamp) {
0125             $this->setTimestamp(new DateTime());
0126         }
0127         return $this->_timestamp;
0128     }
0129     
0130     /**
0131      * Set timestamp
0132      * 
0133      * @azure Timestamp Edm.DateTime
0134      * @param DateTime $value
0135      */
0136     public function setTimestamp(DateTime $value)
0137     {
0138         $this->_timestamp = $value;
0139     }
0140     
0141     /**
0142      * Get etag
0143      * 
0144      * @return string
0145      */
0146     public function getEtag()
0147     {
0148         return $this->_etag;
0149     }
0150     
0151     /**
0152      * Set etag
0153      * 
0154      * @param string $value
0155      */
0156     public function setEtag($value = '')
0157     {
0158         $this->_etag = $value;
0159     }
0160     
0161     /**
0162      * Get Azure values
0163      * 
0164      * @return array
0165      */
0166     public function getAzureValues()
0167     {
0168         // Get accessors
0169         $accessors = self::getAzureAccessors(get_class($this));
0170         
0171         // Loop accessors and retrieve values
0172         $returnValue = array();
0173         foreach ($accessors as $accessor) {
0174             if ($accessor->EntityType == 'ReflectionProperty') {
0175                 $property = $accessor->EntityAccessor;
0176                 $returnValue[] = (object)array(
0177                     'Name'  => $accessor->AzurePropertyName,
0178                   'Type'  => $accessor->AzurePropertyType,
0179                   'Value' => $this->$property,
0180                 );
0181             } else if ($accessor->EntityType == 'ReflectionMethod' && substr(strtolower($accessor->EntityAccessor), 0, 3) == 'get') {
0182                 $method = $accessor->EntityAccessor;
0183                 $returnValue[] = (object)array(
0184                     'Name'  => $accessor->AzurePropertyName,
0185                   'Type'  => $accessor->AzurePropertyType,
0186                   'Value' => $this->$method(),
0187                 );
0188             }
0189         }
0190         
0191         // Return
0192         return $returnValue;
0193     }
0194     
0195     /**
0196      * Set Azure values
0197      * 
0198      * @param array $values
0199      * @param boolean $throwOnError Throw Zend_Service_WindowsAzure_Exception when a property is not specified in $values?
0200      * @throws Zend_Service_WindowsAzure_Exception
0201      */
0202     public function setAzureValues($values = array(), $throwOnError = false)
0203     {
0204         // Get accessors
0205         $accessors = self::getAzureAccessors(get_class($this));
0206         
0207         // Loop accessors and set values
0208         $returnValue = array();
0209         foreach ($accessors as $accessor) {
0210             if (isset($values[$accessor->AzurePropertyName])) {
0211                 // Cast to correct type
0212                 if ($accessor->AzurePropertyType != '') {
0213                     switch (strtolower($accessor->AzurePropertyType)) {
0214                       case 'edm.int32':
0215                       case 'edm.int64':
0216                           $values[$accessor->AzurePropertyName] = intval($values[$accessor->AzurePropertyName]); break;
0217                       case 'edm.boolean':
0218                           if ($values[$accessor->AzurePropertyName] == 'true' || $values[$accessor->AzurePropertyName] == '1')
0219                               $values[$accessor->AzurePropertyName] = true;
0220                           else
0221                               $values[$accessor->AzurePropertyName] = false;
0222                           break;
0223                       case 'edm.double':
0224                           $values[$accessor->AzurePropertyName] = floatval($values[$accessor->AzurePropertyName]); break;
0225                       case 'edm.datetime':
0226                         $values[$accessor->AzurePropertyName] = $this->_convertToDateTime($values[$accessor->AzurePropertyName]); break;
0227                   }
0228                 }
0229                 
0230                 // Assign value
0231                 if ($accessor->EntityType == 'ReflectionProperty') {
0232                     $property = $accessor->EntityAccessor;
0233                     $this->$property = $values[$accessor->AzurePropertyName];
0234                 } else if ($accessor->EntityType == 'ReflectionMethod' && substr(strtolower($accessor->EntityAccessor), 0, 3) == 'set') {
0235                     $method = $accessor->EntityAccessor;
0236                     $this->$method($values[$accessor->AzurePropertyName]);
0237                 }
0238             } else if ($throwOnError) {
0239         // require_once 'Zend/Service/WindowsAzure/Exception.php';
0240                 throw new Zend_Service_WindowsAzure_Exception("Property '" . $accessor->AzurePropertyName . "' was not found in \$values array");    
0241             }
0242         }
0243         
0244         // Return
0245         return $returnValue;
0246     }
0247     
0248     /**
0249      * Get Azure accessors from class
0250      * 
0251      * @param string $className Class to get accessors for
0252      * @return array
0253      */
0254     public static function getAzureAccessors($className = '')
0255     {
0256         // List of accessors
0257         $azureAccessors = array();
0258         
0259         // Get all types
0260         $type = new ReflectionClass($className);
0261         
0262         // Loop all properties
0263         $properties = $type->getProperties();
0264         foreach ($properties as $property) {
0265             $accessor = self::getAzureAccessor($property);
0266             if (!is_null($accessor)) {
0267                 $azureAccessors[] = $accessor;
0268             }
0269         }
0270         
0271         // Loop all methods
0272         $methods = $type->getMethods();
0273         foreach ($methods as $method) {
0274             $accessor = self::getAzureAccessor($method);
0275             if (!is_null($accessor)) {
0276                 $azureAccessors[] = $accessor;
0277             }
0278         }
0279         
0280         // Return
0281         return $azureAccessors;
0282     }
0283     
0284     /**
0285      * Get Azure accessor from reflection member
0286      * 
0287      * @param ReflectionProperty|ReflectionMethod $member
0288      * @return object
0289      */
0290     public static function getAzureAccessor($member)
0291     {
0292         // Get comment
0293         $docComment = $member->getDocComment();
0294         
0295         // Check for Azure comment
0296         if (strpos($docComment, '@azure') === false)
0297         {
0298             return null;
0299         }
0300             
0301         // Search for @azure contents
0302         $azureComment = '';
0303         $commentLines = explode("\n", $docComment);
0304         foreach ($commentLines as $commentLine) {
0305             if (strpos($commentLine, '@azure') !== false) {
0306                 $azureComment = trim(substr($commentLine, strpos($commentLine, '@azure') + 6));
0307                 while (strpos($azureComment, '  ') !== false) {
0308                     $azureComment = str_replace('  ', ' ', $azureComment);
0309                 }
0310                 break;
0311             }
0312         }
0313         
0314         // Fetch @azure properties
0315         $azureProperties = explode(' ', $azureComment);
0316         return (object)array(
0317             'EntityAccessor'    => $member->getName(),
0318             'EntityType'        => get_class($member),
0319             'AzurePropertyName' => $azureProperties[0],
0320           'AzurePropertyType' => isset($azureProperties[1]) ? $azureProperties[1] : ''
0321         );
0322     }
0323     
0324     /**
0325      * Converts a string to a DateTime object. Returns false on failure.
0326      * 
0327      * @param string $value The string value to parse
0328      * @return DateTime|boolean
0329      */
0330     protected function _convertToDateTime($value = '') 
0331     {
0332       if ($value === '') {
0333         return false;
0334       }
0335       
0336       if ($value instanceof DateTime) {
0337         return $value;
0338       }
0339       
0340       if (@strtotime($value) !== false) {
0341         try {
0342           if (substr($value, -1) == 'Z') {
0343             $value = substr($value, 0, strlen($value) - 1);
0344           }
0345           return new DateTime($value, new DateTimeZone('UTC'));
0346         }
0347         catch (Exception $ex) {
0348           return false;
0349         }
0350       }
0351       
0352       return false;
0353     }
0354 }