SubscriptionParser.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. namespace PicoFeed\Serialization;
  3. use SimpleXMLElement;
  4. /**
  5. * Class SubscriptionParser
  6. *
  7. * @package PicoFeed\Serialization
  8. * @author Frederic Guillot
  9. */
  10. class SubscriptionParser
  11. {
  12. /**
  13. * @var Subscription
  14. */
  15. protected $subscription;
  16. /**
  17. * @var SimpleXMLElement
  18. */
  19. private $outlineElement;
  20. /**
  21. * @var SimpleXMLElement
  22. */
  23. private $parentElement;
  24. /**
  25. * Constructor
  26. *
  27. * @access public
  28. * @param SimpleXMLElement $parentElement
  29. * @param SimpleXMLElement $outlineElement
  30. */
  31. public function __construct(SimpleXMLElement $parentElement, SimpleXMLElement $outlineElement)
  32. {
  33. $this->parentElement = $parentElement;
  34. $this->outlineElement = $outlineElement;
  35. $this->subscription = new Subscription();
  36. }
  37. /**
  38. * Get object instance
  39. *
  40. * @static
  41. * @access public
  42. * @param SimpleXMLElement $parentElement
  43. * @param SimpleXMLElement $outlineElement
  44. * @return SubscriptionParser
  45. */
  46. public static function create(SimpleXMLElement $parentElement, SimpleXMLElement $outlineElement)
  47. {
  48. return new static($parentElement, $outlineElement);
  49. }
  50. /**
  51. * Parse subscription entry
  52. *
  53. * @access public
  54. * @return Subscription
  55. */
  56. public function parse()
  57. {
  58. $this->subscription->setCategory($this->findCategory());
  59. $this->subscription->setTitle($this->findTitle());
  60. $this->subscription->setFeedUrl($this->findFeedUrl());
  61. $this->subscription->setSiteUrl($this->findSiteUrl());
  62. $this->subscription->setType($this->findType());
  63. $this->subscription->setDescription($this->findDescription());
  64. return $this->subscription;
  65. }
  66. /**
  67. * Find category.
  68. *
  69. * @access protected
  70. * @return string
  71. */
  72. protected function findCategory()
  73. {
  74. return isset($this->parentElement['text']) ? (string) $this->parentElement['text'] : '';
  75. }
  76. /**
  77. * Find title.
  78. *
  79. * @access protected
  80. * @return string
  81. */
  82. protected function findTitle()
  83. {
  84. return isset($this->outlineElement['title']) ? (string) $this->outlineElement['title'] : (string) $this->outlineElement['text'];
  85. }
  86. /**
  87. * Find feed url.
  88. *
  89. * @access protected
  90. * @return string
  91. */
  92. protected function findFeedUrl()
  93. {
  94. return (string) $this->outlineElement['xmlUrl'];
  95. }
  96. /**
  97. * Find site url.
  98. *
  99. * @access protected
  100. * @return string
  101. */
  102. protected function findSiteUrl()
  103. {
  104. return isset($this->outlineElement['htmlUrl']) ? (string) $this->outlineElement['htmlUrl'] : $this->findFeedUrl();
  105. }
  106. /**
  107. * Find type.
  108. *
  109. * @access protected
  110. * @return string
  111. */
  112. protected function findType()
  113. {
  114. return isset($this->outlineElement['version']) ? (string) $this->outlineElement['version'] :
  115. isset($this->outlineElement['type']) ? (string) $this->outlineElement['type'] : 'rss';
  116. }
  117. /**
  118. * Find description.
  119. *
  120. * @access protected
  121. * @return string
  122. */
  123. protected function findDescription()
  124. {
  125. return isset($this->outlineElement['description']) ? (string) $this->outlineElement['description'] : $this->findTitle();
  126. }
  127. }