DynamicTableEntity.php 6.0 KB

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