/Zend/Service/WindowsAzure/Storage/TableEntity.php

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