PageRenderTime 24ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

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

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