DynamicTableEntity.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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-2015 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. /**
  23. * @see Zend_Service_WindowsAzure_Storage_TableEntity
  24. */
  25. require_once 'Zend/Service/WindowsAzure/Storage/TableEntity.php';
  26. /**
  27. * @category Zend
  28. * @package Zend_Service_WindowsAzure
  29. * @subpackage Storage
  30. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  31. * @license http://framework.zend.com/license/new-bsd New BSD License
  32. */
  33. class Zend_Service_WindowsAzure_Storage_DynamicTableEntity extends Zend_Service_WindowsAzure_Storage_TableEntity
  34. {
  35. /**
  36. * Dynamic properties
  37. *
  38. * @var array
  39. */
  40. protected $_dynamicProperties = array();
  41. /**
  42. * Magic overload for setting properties
  43. *
  44. * @param string $name Name of the property
  45. * @param string $value Value to set
  46. */
  47. public function __set($name, $value) {
  48. $this->setAzureProperty($name, $value, null);
  49. }
  50. /**
  51. * Magic overload for getting properties
  52. *
  53. * @param string $name Name of the property
  54. */
  55. public function __get($name) {
  56. return $this->getAzureProperty($name);
  57. }
  58. /**
  59. * Set an Azure property
  60. *
  61. * @param string $name Property name
  62. * @param mixed $value Property value
  63. * @param string $type Property type (Edm.xxxx)
  64. * @return Zend_Service_WindowsAzure_Storage_DynamicTableEntity
  65. */
  66. public function setAzureProperty($name, $value = '', $type = null)
  67. {
  68. if (strtolower($name) == 'partitionkey') {
  69. $this->setPartitionKey($value);
  70. } else if (strtolower($name) == 'rowkey') {
  71. $this->setRowKey($value);
  72. } else if (strtolower($name) == 'etag') {
  73. $this->setEtag($value);
  74. } else {
  75. if (!array_key_exists(strtolower($name), $this->_dynamicProperties)) {
  76. // Determine type?
  77. if (is_null($type)) {
  78. $type = 'Edm.String';
  79. if (is_int($value)) {
  80. $type = 'Edm.Int32';
  81. } else if (is_float($value)) {
  82. $type = 'Edm.Double';
  83. } else if (is_bool($value)) {
  84. $type = 'Edm.Boolean';
  85. } else if ($value instanceof DateTime || $this->_convertToDateTime($value) !== false) {
  86. if (!$value instanceof DateTime) {
  87. $value = $this->_convertToDateTime($value);
  88. }
  89. $type = 'Edm.DateTime';
  90. }
  91. }
  92. // Set dynamic property
  93. $this->_dynamicProperties[strtolower($name)] = (object)array(
  94. 'Name' => $name,
  95. 'Type' => $type,
  96. 'Value' => $value,
  97. );
  98. }
  99. // Set type?
  100. if (!is_null($type)) {
  101. $this->_dynamicProperties[strtolower($name)]->Type = $type;
  102. // Try to convert the type
  103. if ($type == 'Edm.Int32' || $type == 'Edm.Int64') {
  104. $value = intval($value);
  105. } else if ($type == 'Edm.Double') {
  106. $value = floatval($value);
  107. } else if ($type == 'Edm.Boolean') {
  108. if (!is_bool($value)) {
  109. $value = strtolower($value) == 'true';
  110. }
  111. } else if ($type == 'Edm.DateTime') {
  112. if (!$value instanceof DateTime) {
  113. $value = $this->_convertToDateTime($value);
  114. }
  115. }
  116. }
  117. // Set value
  118. $this->_dynamicProperties[strtolower($name)]->Value = $value;
  119. }
  120. return $this;
  121. }
  122. /**
  123. * Set an Azure property type
  124. *
  125. * @param string $name Property name
  126. * @param string $type Property type (Edm.xxxx)
  127. * @return Zend_Service_WindowsAzure_Storage_DynamicTableEntity
  128. */
  129. public function setAzurePropertyType($name, $type = 'Edm.String')
  130. {
  131. if (!array_key_exists(strtolower($name), $this->_dynamicProperties)) {
  132. $this->setAzureProperty($name, '', $type);
  133. } else {
  134. $this->_dynamicProperties[strtolower($name)]->Type = $type;
  135. }
  136. return $this;
  137. }
  138. /**
  139. * Get an Azure property
  140. *
  141. * @param string $name Property name
  142. * @param mixed $value Property value
  143. * @param string $type Property type (Edm.xxxx)
  144. * @return Zend_Service_WindowsAzure_Storage_DynamicTableEntity
  145. */
  146. public function getAzureProperty($name)
  147. {
  148. if (strtolower($name) == 'partitionkey') {
  149. return $this->getPartitionKey();
  150. }
  151. if (strtolower($name) == 'rowkey') {
  152. return $this->getRowKey();
  153. }
  154. if (strtolower($name) == 'etag') {
  155. return $this->getEtag();
  156. }
  157. if (!array_key_exists(strtolower($name), $this->_dynamicProperties)) {
  158. $this->setAzureProperty($name);
  159. }
  160. return $this->_dynamicProperties[strtolower($name)]->Value;
  161. }
  162. /**
  163. * Get an Azure property type
  164. *
  165. * @param string $name Property name
  166. * @return string Property type (Edm.xxxx)
  167. */
  168. public function getAzurePropertyType($name)
  169. {
  170. if (!array_key_exists(strtolower($name), $this->_dynamicProperties)) {
  171. $this->setAzureProperty($name, '', $type);
  172. }
  173. return $this->_dynamicProperties[strtolower($name)]->Type;
  174. }
  175. /**
  176. * Get Azure values
  177. *
  178. * @return array
  179. */
  180. public function getAzureValues()
  181. {
  182. return array_merge(array_values($this->_dynamicProperties), parent::getAzureValues());
  183. }
  184. /**
  185. * Set Azure values
  186. *
  187. * @param array $values
  188. * @param boolean $throwOnError Throw Zend_Service_WindowsAzure_Exception when a property is not specified in $values?
  189. * @throws Zend_Service_WindowsAzure_Exception
  190. */
  191. public function setAzureValues($values = array(), $throwOnError = false)
  192. {
  193. // Set parent values
  194. parent::setAzureValues($values, false);
  195. // Set current values
  196. foreach ($values as $key => $value)
  197. {
  198. $this->$key = $value;
  199. }
  200. }
  201. }