PageRenderTime 44ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/trunk/library/Microsoft/WindowsAzure/Storage/DynamicTableEntity.php

#
PHP | 234 lines | 107 code | 20 blank | 107 comment | 47 complexity | d3fd0f577d1d9d29d055c40a3f048ad2 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /**
  3. * Copyright (c) 2009 - 2012, RealDolmen
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are met:
  8. * * Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * * Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * * Neither the name of RealDolmen nor the
  14. * names of its contributors may be used to endorse or promote products
  15. * derived from this software without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY RealDolmen ''AS IS'' AND ANY
  18. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  19. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  20. * DISCLAIMED. IN NO EVENT SHALL RealDolmen BE LIABLE FOR ANY
  21. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  22. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  23. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  24. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  26. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. *
  28. * @category Microsoft
  29. * @package Microsoft_WindowsAzure
  30. * @subpackage Storage
  31. * @copyright Copyright (c) 2009 - 2012, RealDolmen (http://www.realdolmen.com)
  32. * @license http://phpazure.codeplex.com/license
  33. * @version $Id: BlobInstance.php 14561 2009-05-07 08:05:12Z unknown $
  34. */
  35. /**
  36. * @see Microsoft_AutoLoader
  37. */
  38. require_once dirname(__FILE__) . '/../../AutoLoader.php';
  39. /**
  40. * @category Microsoft
  41. * @package Microsoft_WindowsAzure
  42. * @subpackage Storage
  43. * @copyright Copyright (c) 2009 - 2012, RealDolmen (http://www.realdolmen.com)
  44. * @license http://phpazure.codeplex.com/license
  45. */
  46. class Microsoft_WindowsAzure_Storage_DynamicTableEntity extends Microsoft_WindowsAzure_Storage_TableEntity
  47. {
  48. /**
  49. * Dynamic properties
  50. *
  51. * @var array
  52. */
  53. protected $_dynamicProperties = array();
  54. /**
  55. * Magic overload for setting properties
  56. *
  57. * @param string $name Name of the property
  58. * @param string $value Value to set
  59. */
  60. public function __set($name, $value) {
  61. $this->setAzureProperty($name, $value, null);
  62. }
  63. /**
  64. * Magic overload for getting properties
  65. *
  66. * @param string $name Name of the property
  67. */
  68. public function __get($name) {
  69. return $this->getAzureProperty($name);
  70. }
  71. /**
  72. * Set an Azure property
  73. *
  74. * @param string $name Property name
  75. * @param mixed $value Property value
  76. * @param string $type Property type (Edm.xxxx)
  77. * @return Microsoft_WindowsAzure_Storage_DynamicTableEntity
  78. */
  79. public function setAzureProperty($name, $value = '', $type = null)
  80. {
  81. if (strtolower($name) == 'partitionkey') {
  82. $this->setPartitionKey($value);
  83. } else if (strtolower($name) == 'rowkey') {
  84. $this->setRowKey($value);
  85. } else if (strtolower($name) == 'etag') {
  86. $this->setEtag($value);
  87. } else {
  88. if (!array_key_exists(strtolower($name), $this->_dynamicProperties)) {
  89. // Determine type?
  90. if (is_null($type)) {
  91. $type = 'Edm.String';
  92. if (is_int($value)) {
  93. $type = 'Edm.Int32';
  94. } else if (is_float($value)) {
  95. $type = 'Edm.Double';
  96. } else if (is_bool($value)) {
  97. $type = 'Edm.Boolean';
  98. } else if ($value instanceof DateTime || $this->_convertToDateTime($value) !== false) {
  99. if (!$value instanceof DateTime) {
  100. $value = $this->_convertToDateTime($value);
  101. }
  102. $type = 'Edm.DateTime';
  103. }
  104. }
  105. // Set dynamic property
  106. $this->_dynamicProperties[strtolower($name)] = (object)array(
  107. 'Name' => $name,
  108. 'Type' => $type,
  109. 'Value' => $value,
  110. );
  111. }
  112. // Set type?
  113. if (!is_null($type)) {
  114. $this->_dynamicProperties[strtolower($name)]->Type = $type;
  115. // Try to convert the type
  116. if ($type == 'Edm.Int32' || $type == 'Edm.Int64') {
  117. $value = intval($value);
  118. } else if ($type == 'Edm.Double') {
  119. $value = floatval($value);
  120. } else if ($type == 'Edm.Boolean') {
  121. if (!is_bool($value)) {
  122. $value = strtolower($value) == 'true';
  123. }
  124. } else if ($type == 'Edm.DateTime') {
  125. if (!$value instanceof DateTime) {
  126. $value = $this->_convertToDateTime($value);
  127. }
  128. }
  129. }
  130. // Set value
  131. $this->_dynamicProperties[strtolower($name)]->Value = $value;
  132. }
  133. return $this;
  134. }
  135. /**
  136. * Set an Azure property type
  137. *
  138. * @param string $name Property name
  139. * @param string $type Property type (Edm.xxxx)
  140. * @return Microsoft_WindowsAzure_Storage_DynamicTableEntity
  141. */
  142. public function setAzurePropertyType($name, $type = 'Edm.String')
  143. {
  144. if (!array_key_exists(strtolower($name), $this->_dynamicProperties)) {
  145. $this->setAzureProperty($name, '', $type);
  146. } else {
  147. $this->_dynamicProperties[strtolower($name)]->Type = $type;
  148. }
  149. return $this;
  150. }
  151. /**
  152. * Get an Azure property
  153. *
  154. * @param string $name Property name
  155. * @param mixed $value Property value
  156. * @param string $type Property type (Edm.xxxx)
  157. * @return Microsoft_WindowsAzure_Storage_DynamicTableEntity
  158. */
  159. public function getAzureProperty($name)
  160. {
  161. if (strtolower($name) == 'partitionkey') {
  162. return $this->getPartitionKey();
  163. }
  164. if (strtolower($name) == 'rowkey') {
  165. return $this->getRowKey();
  166. }
  167. if (strtolower($name) == 'etag') {
  168. return $this->getEtag();
  169. }
  170. if (!array_key_exists(strtolower($name), $this->_dynamicProperties)) {
  171. $this->setAzureProperty($name);
  172. }
  173. return $this->_dynamicProperties[strtolower($name)]->Value;
  174. }
  175. /**
  176. * Get an Azure property type
  177. *
  178. * @param string $name Property name
  179. * @return string Property type (Edm.xxxx)
  180. */
  181. public function getAzurePropertyType($name)
  182. {
  183. if (!array_key_exists(strtolower($name), $this->_dynamicProperties)) {
  184. $this->setAzureProperty($name, '', $type);
  185. }
  186. return $this->_dynamicProperties[strtolower($name)]->Type;
  187. }
  188. /**
  189. * Get Azure values
  190. *
  191. * @return array
  192. */
  193. public function getAzureValues()
  194. {
  195. return array_merge(array_values($this->_dynamicProperties), parent::getAzureValues());
  196. }
  197. /**
  198. * Set Azure values
  199. *
  200. * @param array $values
  201. * @param boolean $throwOnError Throw Microsoft_WindowsAzure_Exception when a property is not specified in $values?
  202. * @throws Microsoft_WindowsAzure_Exception
  203. */
  204. public function setAzureValues($values = array(), $throwOnError = false)
  205. {
  206. // Set parent values
  207. parent::setAzureValues($values, false);
  208. // Set current values
  209. foreach ($values as $key => $value)
  210. {
  211. $this->$key = $value;
  212. }
  213. }
  214. }