/lib/Enclosure.php

https://bitbucket.org/mgduk/mgdreader · PHP · 91 lines · 69 code · 13 blank · 9 comment · 4 complexity · 83de9511065a75b5c3c0e682c01426ae MD5 · raw file

  1. <?php
  2. namespace DolanReader;
  3. /**
  4. * Represents media attached to a FeedItem
  5. */
  6. class Enclosure {
  7. protected $item;
  8. protected $url;
  9. protected $type;
  10. protected $length;
  11. public function __get ($var) {
  12. switch ($var) {
  13. case 'item': return $this->item;
  14. case 'url': return $this->url;
  15. case 'type': return $this->type;
  16. case 'title': return $this->getTitle();
  17. case 'length': return $this->length;
  18. case 'data': return $this->getData();
  19. }
  20. }
  21. public function __set ($var,$value) {
  22. switch ($var) {
  23. case 'url': $this->url = $value; break;
  24. case 'type': $this->type = $value; break;
  25. case 'length': $this->length = $value; break;
  26. case 'data': $this->setData($value); break;
  27. }
  28. }
  29. /**
  30. * Returns a human-readable title
  31. * @return string
  32. */
  33. protected function getTitle () {
  34. switch ($this->type) {
  35. // some common types
  36. case 'image/jpeg': $type = 'JPEG Image'; break;
  37. case 'image/gif': $type = 'GIF Image'; break;
  38. case 'audio/x-m4a': $type = 'MPEG-`4 Audio'; break;
  39. // attempt make other types more human readable
  40. default:
  41. $typeBits = array_reverse(explode('/',$this->type));
  42. array_walk($typeBits,function(&$s){$s=ucfirst($s);});
  43. $type = implode(' ',$typeBits);
  44. }
  45. return $type.': '.basename($this->url);
  46. }
  47. protected function getData () {
  48. return array(
  49. 'url' => $this->url,
  50. 'type' => $this->type,
  51. 'title' => $this->title,
  52. 'length' => $this->length
  53. );
  54. }
  55. protected function setData ($data) {
  56. foreach ($data as $key=>$value) {
  57. $this->__set($key,$value);
  58. }
  59. }
  60. public function save () {
  61. $db = Db::get();
  62. $query = $db->prepare("INSERT INTO `enclosures` (`item`,`url`,`type`,`length`)
  63. VALUES (:item,:url,:type,:length)");
  64. $success = $query->execute(array(
  65. 'item' => $this->item->id,
  66. 'url' => $this->url,
  67. 'type' => $this->type,
  68. 'length' => $this->length
  69. ));
  70. if ($db->lastInsertId())
  71. $this->id = $db->lastInsertId();
  72. return $success;
  73. }
  74. public function __construct ($item) {
  75. $this->item = $item;
  76. }
  77. }
  78. ?>