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  * @see Zend_Service_WindowsAzure_Storage_TableEntity
0025  */
0026 // require_once 'Zend/Service/WindowsAzure/Storage/TableEntity.php';
0027 
0028 /**
0029  * @category   Zend
0030  * @package    Zend_Service_WindowsAzure
0031  * @subpackage Storage
0032  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0033  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0034  */
0035 class Zend_Service_WindowsAzure_Storage_DynamicTableEntity extends Zend_Service_WindowsAzure_Storage_TableEntity
0036 {   
0037     /**
0038      * Dynamic properties
0039      * 
0040      * @var array
0041      */
0042     protected $_dynamicProperties = array();
0043     
0044     /**
0045      * Magic overload for setting properties
0046      * 
0047      * @param string $name     Name of the property
0048      * @param string $value    Value to set
0049      */
0050     public function __set($name, $value) {      
0051         $this->setAzureProperty($name, $value, null);
0052     }
0053 
0054     /**
0055      * Magic overload for getting properties
0056      * 
0057      * @param string $name     Name of the property
0058      */
0059     public function __get($name) {
0060         return $this->getAzureProperty($name);
0061     }
0062     
0063     /**
0064      * Set an Azure property
0065      * 
0066      * @param string $name Property name
0067      * @param mixed $value Property value
0068      * @param string $type Property type (Edm.xxxx)
0069      * @return Zend_Service_WindowsAzure_Storage_DynamicTableEntity
0070      */
0071     public function setAzureProperty($name, $value = '', $type = null)
0072     {
0073         if (strtolower($name) == 'partitionkey') {
0074             $this->setPartitionKey($value);
0075         } else if (strtolower($name) == 'rowkey') {
0076             $this->setRowKey($value);
0077         } else if (strtolower($name) == 'etag') {
0078             $this->setEtag($value);
0079         } else {
0080             if (!array_key_exists(strtolower($name), $this->_dynamicProperties)) {
0081                 // Determine type?
0082                 if (is_null($type)) {
0083                     $type = 'Edm.String';
0084                     if (is_int($value)) {
0085                         $type = 'Edm.Int32';
0086                     } else if (is_float($value)) {
0087                         $type = 'Edm.Double';
0088                     } else if (is_bool($value)) {
0089                         $type = 'Edm.Boolean';
0090                     } else if ($value instanceof DateTime || $this->_convertToDateTime($value) !== false) {
0091                         if (!$value instanceof DateTime) {
0092                             $value = $this->_convertToDateTime($value);
0093                         }
0094                         $type = 'Edm.DateTime';
0095                     }
0096                 }
0097                 
0098                 // Set dynamic property
0099                 $this->_dynamicProperties[strtolower($name)] = (object)array(
0100                         'Name'  => $name,
0101                       'Type'  => $type,
0102                       'Value' => $value,
0103                     );
0104             }
0105             
0106             // Set type?
0107             if (!is_null($type)) {
0108               $this->_dynamicProperties[strtolower($name)]->Type = $type;
0109               
0110               // Try to convert the type
0111               if ($type == 'Edm.Int32' || $type == 'Edm.Int64') {
0112                 $value = intval($value);
0113               } else if ($type == 'Edm.Double') {
0114                 $value = floatval($value);
0115               } else if ($type == 'Edm.Boolean') {
0116                 if (!is_bool($value)) {
0117                   $value = strtolower($value) == 'true';
0118                 }
0119               } else if ($type == 'Edm.DateTime') {
0120                 if (!$value instanceof DateTime) {
0121                       $value = $this->_convertToDateTime($value);
0122                     }
0123               }
0124             }
0125        
0126         // Set value
0127             $this->_dynamicProperties[strtolower($name)]->Value = $value;
0128         }
0129         return $this;
0130     }
0131     
0132     /**
0133      * Set an Azure property type
0134      * 
0135      * @param string $name Property name
0136      * @param string $type Property type (Edm.xxxx)
0137      * @return Zend_Service_WindowsAzure_Storage_DynamicTableEntity
0138      */
0139     public function setAzurePropertyType($name, $type = 'Edm.String')
0140     {
0141         if (!array_key_exists(strtolower($name), $this->_dynamicProperties)) {
0142             $this->setAzureProperty($name, '', $type);            
0143         } else {
0144             $this->_dynamicProperties[strtolower($name)]->Type = $type;   
0145         }
0146         return $this;
0147     }
0148     
0149     /**
0150      * Get an Azure property
0151      * 
0152      * @param string $name Property name
0153      * @param mixed $value Property value
0154      * @param string $type Property type (Edm.xxxx)
0155      * @return Zend_Service_WindowsAzure_Storage_DynamicTableEntity
0156      */
0157     public function getAzureProperty($name)
0158     {
0159         if (strtolower($name) == 'partitionkey') {
0160             return $this->getPartitionKey();
0161         }
0162         if (strtolower($name) == 'rowkey') {
0163             return $this->getRowKey();
0164         }
0165         if (strtolower($name) == 'etag') {
0166             return $this->getEtag();
0167         }
0168 
0169         if (!array_key_exists(strtolower($name), $this->_dynamicProperties)) {
0170             $this->setAzureProperty($name);            
0171         }
0172 
0173         return $this->_dynamicProperties[strtolower($name)]->Value;
0174     }
0175     
0176     /**
0177      * Get an Azure property type
0178      * 
0179      * @param string $name Property name
0180      * @return string Property type (Edm.xxxx)
0181      */
0182     public function getAzurePropertyType($name)
0183     {
0184         if (!array_key_exists(strtolower($name), $this->_dynamicProperties)) {
0185             $this->setAzureProperty($name, '', $type);            
0186         }
0187         
0188         return $this->_dynamicProperties[strtolower($name)]->Type;
0189     }
0190     
0191     /**
0192      * Get Azure values
0193      * 
0194      * @return array
0195      */
0196     public function getAzureValues()
0197     {
0198         return array_merge(array_values($this->_dynamicProperties), parent::getAzureValues());
0199     }
0200     
0201     /**
0202      * Set Azure values
0203      * 
0204      * @param array $values
0205      * @param boolean $throwOnError Throw Zend_Service_WindowsAzure_Exception when a property is not specified in $values?
0206      * @throws Zend_Service_WindowsAzure_Exception
0207      */
0208     public function setAzureValues($values = array(), $throwOnError = false)
0209     {
0210         // Set parent values
0211         parent::setAzureValues($values, false);
0212         
0213         // Set current values
0214         foreach ($values as $key => $value) 
0215         {
0216             $this->$key = $value;
0217         }
0218     }
0219 }