PageRenderTime 52ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/Microsoft/WindowsAzure/Storage/TableEntity.php

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