Object.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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('container', $data)) {
  96. require_once 'Zend/Service/Rackspace/Files/Exception.php';
  97. throw new Zend_Service_Rackspace_Files_Exception("You must pass the container of the object in the array (container)");
  98. }
  99. if (array_key_exists('name', $data)) {
  100. if (!array_key_exists('hash', $data)) {
  101. require_once 'Zend/Service/Rackspace/Files/Exception.php';
  102. throw new Zend_Service_Rackspace_Files_Exception("You must pass the hash of the object in the array (hash)");
  103. }
  104. if (!array_key_exists('bytes', $data)) {
  105. require_once 'Zend/Service/Rackspace/Files/Exception.php';
  106. throw new Zend_Service_Rackspace_Files_Exception("You must pass the byte size of the object in the array (bytes)");
  107. }
  108. if (!array_key_exists('content_type', $data)) {
  109. require_once 'Zend/Service/Rackspace/Files/Exception.php';
  110. throw new Zend_Service_Rackspace_Files_Exception("You must pass the content type of the object in the array (content_type)");
  111. }
  112. if (!array_key_exists('last_modified', $data)) {
  113. require_once 'Zend/Service/Rackspace/Files/Exception.php';
  114. throw new Zend_Service_Rackspace_Files_Exception("You must pass the last modified data of the object in the array (last_modified)");
  115. }
  116. $this->name= $data['name'];
  117. $this->hash= $data['hash'];
  118. $this->size= $data['bytes'];
  119. $this->contentType= $data['content_type'];
  120. $this->lastModified= $data['last_modified'];
  121. if (!empty($data['content'])) {
  122. $this->content= $data['content'];
  123. }
  124. }
  125. elseif (array_key_exists('subdir', $data)) {
  126. $this->name = $data['subdir'];
  127. }
  128. else {
  129. require_once 'Zend/Service/Rackspace/Files/Exception.php';
  130. throw new Zend_Service_Rackspace_Files_Exception("You must pass the name of the object in the array (name)");
  131. }
  132. $this->container= $data['container'];
  133. $this->service= $service;
  134. }
  135. /**
  136. * Get name
  137. *
  138. * @return string
  139. */
  140. public function getName()
  141. {
  142. return $this->name;
  143. }
  144. /**
  145. * Get the name of the container
  146. *
  147. * @return string
  148. */
  149. public function getContainer()
  150. {
  151. return $this->container;
  152. }
  153. /**
  154. * Get the MD5 of the object's content
  155. *
  156. * @return string|boolean
  157. */
  158. public function getHash()
  159. {
  160. return $this->hash;
  161. }
  162. /**
  163. * Get the size (in bytes) of the object's content
  164. *
  165. * @return integer|boolean
  166. */
  167. public function getSize()
  168. {
  169. return $this->size;
  170. }
  171. /**
  172. * Get the content type of the object's content
  173. *
  174. * @return string
  175. */
  176. public function getContentType()
  177. {
  178. return $this->contentType;
  179. }
  180. /**
  181. * Get the data of the last modified of the object
  182. *
  183. * @return string
  184. */
  185. public function getLastModified()
  186. {
  187. return $this->lastModified;
  188. }
  189. /**
  190. * Get the content of the object
  191. *
  192. * @return string
  193. */
  194. public function getContent()
  195. {
  196. return $this->content;
  197. }
  198. /**
  199. * Get the metadata of the object
  200. * If you don't pass the $key it returns the entire array of metadata value
  201. *
  202. * @param string $key
  203. * @return string|array|boolean
  204. */
  205. public function getMetadata($key=null)
  206. {
  207. $result= $this->service->getMetadataObject($this->container,$this->name);
  208. if (!empty($result)) {
  209. if (empty($key)) {
  210. return $result['metadata'];
  211. }
  212. if (isset($result['metadata'][$key])) {
  213. return $result['metadata'][$key];
  214. }
  215. }
  216. return false;
  217. }
  218. /**
  219. * Set the metadata value
  220. * The old metadata values are replaced with the new one
  221. *
  222. * @param array $metadata
  223. * @return boolean
  224. */
  225. public function setMetadata($metadata)
  226. {
  227. return $this->service->setMetadataObject($this->container,$this->name,$metadata);
  228. }
  229. /**
  230. * Copy the object to another container
  231. * You can add metadata information to the destination object, change the
  232. * content_type and the name of the object
  233. *
  234. * @param string $container_dest
  235. * @param string $name_dest
  236. * @param array $metadata
  237. * @param string $content_type
  238. * @return boolean
  239. */
  240. public function copyTo($container_dest,$name_dest,$metadata=array(),$content_type=null)
  241. {
  242. return $this->service->copyObject($this->container,$this->name,$container_dest,$name_dest,$metadata,$content_type);
  243. }
  244. /**
  245. * Get the CDN URL of the object
  246. *
  247. * @return string
  248. */
  249. public function getCdnUrl()
  250. {
  251. $result= $this->service->getInfoCdnContainer($this->container);
  252. if ($result!==false) {
  253. if ($result['cdn_enabled']) {
  254. return $result['cdn_uri'].'/'.$this->name;
  255. }
  256. }
  257. return false;
  258. }
  259. /**
  260. * Get the CDN SSL URL of the object
  261. *
  262. * @return string
  263. */
  264. public function getCdnUrlSsl()
  265. {
  266. $result= $this->service->getInfoCdnContainer($this->container);
  267. if ($result!==false) {
  268. if ($result['cdn_enabled']) {
  269. return $result['cdn_uri_ssl'].'/'.$this->name;
  270. }
  271. }
  272. return false;
  273. }
  274. }