TableEntity.php 9.9 KB

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