PageRenderTime 47ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/library/Service/WindowsAzure/Storage/TableEntity.php

https://github.com/kervin/kyzstudio
PHP | 328 lines | 152 code | 32 blank | 144 comment | 30 complexity | 4a2e027858d95216d9a9eb1682c8d695 MD5 | raw file
  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: TableEntity.php 23167 2010-10-19 17:53:31Z mabe $
  21. */
  22. /**
  23. * @see Zend_Service_WindowsAzure_Exception
  24. */
  25. #require_once 'Zend/Service/WindowsAzure/Exception.php';
  26. /**
  27. * @category Zend
  28. * @package Zend_Service_WindowsAzure
  29. * @subpackage Storage
  30. * @copyright Copyright (c) 2005-2010 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_TableEntity
  34. {
  35. const DEFAULT_TIMESTAMP = '1900-01-01T00:00:00';
  36. /**
  37. * Partition key
  38. *
  39. * @var string
  40. */
  41. protected $_partitionKey;
  42. /**
  43. * Row key
  44. *
  45. * @var string
  46. */
  47. protected $_rowKey;
  48. /**
  49. * Timestamp
  50. *
  51. * @var string
  52. */
  53. protected $_timestamp;
  54. /**
  55. * Etag
  56. *
  57. * @var string
  58. */
  59. protected $_etag = '';
  60. /**
  61. * Constructor
  62. *
  63. * @param string $partitionKey Partition key
  64. * @param string $rowKey Row key
  65. */
  66. public function __construct($partitionKey = '', $rowKey = '')
  67. {
  68. $this->_partitionKey = $partitionKey;
  69. $this->_rowKey = $rowKey;
  70. }
  71. /**
  72. * Get partition key
  73. *
  74. * @azure PartitionKey
  75. * @return string
  76. */
  77. public function getPartitionKey()
  78. {
  79. return $this->_partitionKey;
  80. }
  81. /**
  82. * Set partition key
  83. *
  84. * @azure PartitionKey
  85. * @param string $value
  86. */
  87. public function setPartitionKey($value)
  88. {
  89. $this->_partitionKey = $value;
  90. }
  91. /**
  92. * Get row key
  93. *
  94. * @azure RowKey
  95. * @return string
  96. */
  97. public function getRowKey()
  98. {
  99. return $this->_rowKey;
  100. }
  101. /**
  102. * Set row key
  103. *
  104. * @azure RowKey
  105. * @param string $value
  106. */
  107. public function setRowKey($value)
  108. {
  109. $this->_rowKey = $value;
  110. }
  111. /**
  112. * Get timestamp
  113. *
  114. * @azure Timestamp Edm.DateTime
  115. * @return string
  116. */
  117. public function getTimestamp()
  118. {
  119. if (null === $this->_timestamp) {
  120. $this->setTimestamp(self::DEFAULT_TIMESTAMP);
  121. }
  122. return $this->_timestamp;
  123. }
  124. /**
  125. * Set timestamp
  126. *
  127. * @azure Timestamp Edm.DateTime
  128. * @param string $value
  129. */
  130. public function setTimestamp($value = '1900-01-01T00:00:00')
  131. {
  132. $this->_timestamp = $value;
  133. }
  134. /**
  135. * Get etag
  136. *
  137. * @return string
  138. */
  139. public function getEtag()
  140. {
  141. return $this->_etag;
  142. }
  143. /**
  144. * Set etag
  145. *
  146. * @param string $value
  147. */
  148. public function setEtag($value = '')
  149. {
  150. $this->_etag = $value;
  151. }
  152. /**
  153. * Get Azure values
  154. *
  155. * @return array
  156. */
  157. public function getAzureValues()
  158. {
  159. // Get accessors
  160. $accessors = self::getAzureAccessors(get_class($this));
  161. // Loop accessors and retrieve values
  162. $returnValue = array();
  163. foreach ($accessors as $accessor) {
  164. if ($accessor->EntityType == 'ReflectionProperty') {
  165. $property = $accessor->EntityAccessor;
  166. $returnValue[] = (object)array(
  167. 'Name' => $accessor->AzurePropertyName,
  168. 'Type' => $accessor->AzurePropertyType,
  169. 'Value' => $this->$property,
  170. );
  171. } else if ($accessor->EntityType == 'ReflectionMethod' && substr(strtolower($accessor->EntityAccessor), 0, 3) == 'get') {
  172. $method = $accessor->EntityAccessor;
  173. $returnValue[] = (object)array(
  174. 'Name' => $accessor->AzurePropertyName,
  175. 'Type' => $accessor->AzurePropertyType,
  176. 'Value' => $this->$method(),
  177. );
  178. }
  179. }
  180. // Return
  181. return $returnValue;
  182. }
  183. /**
  184. * Set Azure values
  185. *
  186. * @param array $values
  187. * @param boolean $throwOnError Throw Zend_Service_WindowsAzure_Exception when a property is not specified in $values?
  188. * @throws Zend_Service_WindowsAzure_Exception
  189. */
  190. public function setAzureValues($values = array(), $throwOnError = false)
  191. {
  192. // Get accessors
  193. $accessors = self::getAzureAccessors(get_class($this));
  194. // Loop accessors and set values
  195. $returnValue = array();
  196. foreach ($accessors as $accessor) {
  197. if (isset($values[$accessor->AzurePropertyName])) {
  198. // Cast to correct type
  199. if ($accessor->AzurePropertyType != '') {
  200. switch (strtolower($accessor->AzurePropertyType)) {
  201. case 'edm.int32':
  202. case 'edm.int64':
  203. $values[$accessor->AzurePropertyName] = intval($values[$accessor->AzurePropertyName]); break;
  204. case 'edm.boolean':
  205. if ($values[$accessor->AzurePropertyName] == 'true' || $values[$accessor->AzurePropertyName] == '1')
  206. $values[$accessor->AzurePropertyName] = true;
  207. else
  208. $values[$accessor->AzurePropertyName] = false;
  209. break;
  210. case 'edm.double':
  211. $values[$accessor->AzurePropertyName] = floatval($values[$accessor->AzurePropertyName]); break;
  212. }
  213. }
  214. // Assign value
  215. if ($accessor->EntityType == 'ReflectionProperty') {
  216. $property = $accessor->EntityAccessor;
  217. $this->$property = $values[$accessor->AzurePropertyName];
  218. } else if ($accessor->EntityType == 'ReflectionMethod' && substr(strtolower($accessor->EntityAccessor), 0, 3) == 'set') {
  219. $method = $accessor->EntityAccessor;
  220. $this->$method($values[$accessor->AzurePropertyName]);
  221. }
  222. } else if ($throwOnError) {
  223. throw new Zend_Service_WindowsAzure_Exception("Property '" . $accessor->AzurePropertyName . "' was not found in \$values array");
  224. }
  225. }
  226. // Return
  227. return $returnValue;
  228. }
  229. /**
  230. * Get Azure accessors from class
  231. *
  232. * @param string $className Class to get accessors for
  233. * @return array
  234. */
  235. public static function getAzureAccessors($className = '')
  236. {
  237. // List of accessors
  238. $azureAccessors = array();
  239. // Get all types
  240. $type = new ReflectionClass($className);
  241. // Loop all properties
  242. $properties = $type->getProperties();
  243. foreach ($properties as $property) {
  244. $accessor = self::getAzureAccessor($property);
  245. if ($accessor !== null) {
  246. $azureAccessors[] = $accessor;
  247. }
  248. }
  249. // Loop all methods
  250. $methods = $type->getMethods();
  251. foreach ($methods as $method) {
  252. $accessor = self::getAzureAccessor($method);
  253. if ($accessor !== null) {
  254. $azureAccessors[] = $accessor;
  255. }
  256. }
  257. // Return
  258. return $azureAccessors;
  259. }
  260. /**
  261. * Get Azure accessor from reflection member
  262. *
  263. * @param ReflectionProperty|ReflectionMethod $member
  264. * @return object
  265. */
  266. public static function getAzureAccessor($member)
  267. {
  268. // Get comment
  269. $docComment = $member->getDocComment();
  270. // Check for Azure comment
  271. if (strpos($docComment, '@azure') === false)
  272. {
  273. return null;
  274. }
  275. // Search for @azure contents
  276. $azureComment = '';
  277. $commentLines = explode("\n", $docComment);
  278. foreach ($commentLines as $commentLine) {
  279. if (strpos($commentLine, '@azure') !== false) {
  280. $azureComment = trim(substr($commentLine, strpos($commentLine, '@azure') + 6));
  281. while (strpos($azureComment, ' ') !== false) {
  282. $azureComment = str_replace(' ', ' ', $azureComment);
  283. }
  284. break;
  285. }
  286. }
  287. // Fetch @azure properties
  288. $azureProperties = explode(' ', $azureComment);
  289. return (object)array(
  290. 'EntityAccessor' => $member->getName(),
  291. 'EntityType' => get_class($member),
  292. 'AzurePropertyName' => $azureProperties[0],
  293. 'AzurePropertyType' => isset($azureProperties[1]) ? $azureProperties[1] : ''
  294. );
  295. }
  296. }