PageRenderTime 44ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/Zend/Service/Rackspace/Files/Object.php

https://bitbucket.org/simukti/zf1
PHP | 271 lines | 123 code | 2 blank | 146 comment | 16 complexity | dcace75149bc5c2aff93a31146f83471 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_Rackspace
  17. * @subpackage Files
  18. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. */
  21. require_once 'Zend/Service/Rackspace/Files.php';
  22. class Zend_Service_Rackspace_Files_Object
  23. {
  24. /**
  25. * The service that has created the object
  26. *
  27. * @var Zend_Service_Rackspace_Files
  28. */
  29. protected $service;
  30. /**
  31. * Name of the object
  32. *
  33. * @var string
  34. */
  35. protected $name;
  36. /**
  37. * MD5 value of the object's content
  38. *
  39. * @var string
  40. */
  41. protected $hash;
  42. /**
  43. * Size in bytes of the object's content
  44. *
  45. * @var integer
  46. */
  47. protected $size;
  48. /**
  49. * Content type of the object's content
  50. *
  51. * @var string
  52. */
  53. protected $contentType;
  54. /**
  55. * Date of the last modified of the object
  56. *
  57. * @var string
  58. */
  59. protected $lastModified;
  60. /**
  61. * Object content
  62. *
  63. * @var string
  64. */
  65. protected $content;
  66. /**
  67. * Name of the container where the object is stored
  68. *
  69. * @var string
  70. */
  71. protected $container;
  72. /**
  73. * Constructor
  74. *
  75. * You must pass the Zend_Service_Rackspace_Files object of the caller and an associative
  76. * array with the keys "name", "container", "hash", "bytes", "content_type",
  77. * "last_modified", "file" where:
  78. * name= name of the object
  79. * container= name of the container where the object is stored
  80. * hash= the MD5 of the object's content
  81. * bytes= size in bytes of the object's content
  82. * content_type= content type of the object's content
  83. * last_modified= date of the last modified of the object
  84. * content= content of the object
  85. *
  86. * @param Zend_Service_Rackspace_Files $service
  87. * @param array $data
  88. */
  89. public function __construct($service,$data)
  90. {
  91. if (!($service instanceof Zend_Service_Rackspace_Files) || !is_array($data)) {
  92. require_once 'Zend/Service/Rackspace/Files/Exception.php';
  93. throw new Zend_Service_Rackspace_Files_Exception("You must pass a RackspaceFiles and an array");
  94. }
  95. if (!array_key_exists('name', $data)) {
  96. require_once 'Zend/Service/Rackspace/Files/Exception.php';
  97. throw new Zend_Service_Rackspace_Files_Exception("You must pass the name of the object in the array (name)");
  98. }
  99. if (!array_key_exists('container', $data)) {
  100. require_once 'Zend/Service/Rackspace/Files/Exception.php';
  101. throw new Zend_Service_Rackspace_Files_Exception("You must pass the container of the object in the array (container)");
  102. }
  103. if (!array_key_exists('hash', $data)) {
  104. require_once 'Zend/Service/Rackspace/Files/Exception.php';
  105. throw new Zend_Service_Rackspace_Files_Exception("You must pass the hash of the object in the array (hash)");
  106. }
  107. if (!array_key_exists('bytes', $data)) {
  108. require_once 'Zend/Service/Rackspace/Files/Exception.php';
  109. throw new Zend_Service_Rackspace_Files_Exception("You must pass the byte size of the object in the array (bytes)");
  110. }
  111. if (!array_key_exists('content_type', $data)) {
  112. require_once 'Zend/Service/Rackspace/Files/Exception.php';
  113. throw new Zend_Service_Rackspace_Files_Exception("You must pass the content type of the object in the array (content_type)");
  114. }
  115. if (!array_key_exists('last_modified', $data)) {
  116. require_once 'Zend/Service/Rackspace/Files/Exception.php';
  117. throw new Zend_Service_Rackspace_Files_Exception("You must pass the last modified data of the object in the array (last_modified)");
  118. }
  119. $this->name= $data['name'];
  120. $this->container= $data['container'];
  121. $this->hash= $data['hash'];
  122. $this->size= $data['bytes'];
  123. $this->contentType= $data['content_type'];
  124. $this->lastModified= $data['last_modified'];
  125. if (!empty($data['content'])) {
  126. $this->content= $data['content'];
  127. }
  128. $this->service= $service;
  129. }
  130. /**
  131. * Get name
  132. *
  133. * @return string
  134. */
  135. public function getName()
  136. {
  137. return $this->name;
  138. }
  139. /**
  140. * Get the name of the container
  141. *
  142. * @return string
  143. */
  144. public function getContainer()
  145. {
  146. return $this->container;
  147. }
  148. /**
  149. * Get the MD5 of the object's content
  150. *
  151. * @return string|boolean
  152. */
  153. public function getHash()
  154. {
  155. return $this->hash;
  156. }
  157. /**
  158. * Get the size (in bytes) of the object's content
  159. *
  160. * @return integer|boolean
  161. */
  162. public function getSize()
  163. {
  164. return $this->size;
  165. }
  166. /**
  167. * Get the content type of the object's content
  168. *
  169. * @return string
  170. */
  171. public function getContentType()
  172. {
  173. return $this->contentType;
  174. }
  175. /**
  176. * Get the data of the last modified of the object
  177. *
  178. * @return string
  179. */
  180. public function getLastModified()
  181. {
  182. return $this->lastModified;
  183. }
  184. /**
  185. * Get the content of the object
  186. *
  187. * @return string
  188. */
  189. public function getContent()
  190. {
  191. return $this->content;
  192. }
  193. /**
  194. * Get the metadata of the object
  195. * If you don't pass the $key it returns the entire array of metadata value
  196. *
  197. * @param string $key
  198. * @return string|array|boolean
  199. */
  200. public function getMetadata($key=null)
  201. {
  202. $result= $this->service->getMetadataObject($this->container,$this->name);
  203. if (!empty($result)) {
  204. if (empty($key)) {
  205. return $result['metadata'];
  206. }
  207. if (isset($result['metadata'][$key])) {
  208. return $result['metadata'][$key];
  209. }
  210. }
  211. return false;
  212. }
  213. /**
  214. * Set the metadata value
  215. * The old metadata values are replaced with the new one
  216. *
  217. * @param array $metadata
  218. * @return boolean
  219. */
  220. public function setMetadata($metadata)
  221. {
  222. return $this->service->setMetadataObject($this->container,$this->name,$metadata);
  223. }
  224. /**
  225. * Copy the object to another container
  226. * You can add metadata information to the destination object, change the
  227. * content_type and the name of the object
  228. *
  229. * @param string $container_dest
  230. * @param string $name_dest
  231. * @param array $metadata
  232. * @param string $content_type
  233. * @return boolean
  234. */
  235. public function copyTo($container_dest,$name_dest,$metadata=array(),$content_type=null)
  236. {
  237. return $this->service->copyObject($this->container,$this->name,$container_dest,$name_dest,$metadata,$content_type);
  238. }
  239. /**
  240. * Get the CDN URL of the object
  241. *
  242. * @return string
  243. */
  244. public function getCdnUrl()
  245. {
  246. $result= $this->service->getInfoCdnContainer($this->container);
  247. if ($result!==false) {
  248. if ($result['cdn_enabled']) {
  249. return $result['cdn_uri'].'/'.$this->name;
  250. }
  251. }
  252. return false;
  253. }
  254. /**
  255. * Get the CDN SSL URL of the object
  256. *
  257. * @return string
  258. */
  259. public function getCdnUrlSsl()
  260. {
  261. $result= $this->service->getInfoCdnContainer($this->container);
  262. if ($result!==false) {
  263. if ($result['cdn_enabled']) {
  264. return $result['cdn_uri_ssl'].'/'.$this->name;
  265. }
  266. }
  267. return false;
  268. }
  269. }