[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/src/Feed/Parser/ -> AtomParser.php (source)

   1  <?php
   2  
   3  /**
   4   * Joomla! Content Management System
   5   *
   6   * @copyright  (C) 2012 Open Source Matters, Inc. <https://www.joomla.org>
   7   * @license    GNU General Public License version 2 or later; see LICENSE.txt
   8   */
   9  
  10  namespace Joomla\CMS\Feed\Parser;
  11  
  12  use Joomla\CMS\Feed\Feed;
  13  use Joomla\CMS\Feed\FeedEntry;
  14  use Joomla\CMS\Feed\FeedLink;
  15  use Joomla\CMS\Feed\FeedParser;
  16  
  17  // phpcs:disable PSR1.Files.SideEffects
  18  \defined('JPATH_PLATFORM') or die;
  19  // phpcs:enable PSR1.Files.SideEffects
  20  
  21  /**
  22   * ATOM Feed Parser class.
  23   *
  24   * @link   http://www.atomenabled.org/developers/syndication/
  25   * @since  3.1.4
  26   */
  27  class AtomParser extends FeedParser
  28  {
  29      /**
  30       * @var    string  The feed format version.
  31       * @since  3.1.4
  32       */
  33      protected $version;
  34  
  35      /**
  36       * Method to handle the `<author>` element for the feed.
  37       *
  38       * @param   Feed               $feed  The Feed object being built from the parsed feed.
  39       * @param   \SimpleXMLElement  $el    The current XML element object to handle.
  40       *
  41       * @return  void
  42       *
  43       * @since   3.1.4
  44       */
  45      protected function handleAuthor(Feed $feed, \SimpleXMLElement $el)
  46      {
  47          // Set the author information from the XML element.
  48          $feed->setAuthor(
  49              $this->inputFilter->clean((string) $el->name, 'html'),
  50              filter_var((string) $el->email, FILTER_VALIDATE_EMAIL),
  51              filter_var((string) $el->uri, FILTER_VALIDATE_URL)
  52          );
  53      }
  54  
  55      /**
  56       * Method to handle the `<contributor>` element for the feed.
  57       *
  58       * @param   Feed               $feed  The Feed object being built from the parsed feed.
  59       * @param   \SimpleXMLElement  $el    The current XML element object to handle.
  60       *
  61       * @return  void
  62       *
  63       * @since   3.1.4
  64       */
  65      protected function handleContributor(Feed $feed, \SimpleXMLElement $el)
  66      {
  67          $feed->addContributor(
  68              $this->inputFilter->clean((string) $el->name, 'html'),
  69              filter_var((string) $el->email, FILTER_VALIDATE_EMAIL),
  70              filter_var((string) $el->uri, FILTER_VALIDATE_URL)
  71          );
  72      }
  73  
  74      /**
  75       * Method to handle the `<generator>` element for the feed.
  76       *
  77       * @param   Feed               $feed  The Feed object being built from the parsed feed.
  78       * @param   \SimpleXMLElement  $el    The current XML element object to handle.
  79       *
  80       * @return  void
  81       *
  82       * @since   3.1.4
  83       */
  84      protected function handleGenerator(Feed $feed, \SimpleXMLElement $el)
  85      {
  86          $feed->generator = $this->inputFilter->clean((string) $el, 'html');
  87      }
  88  
  89      /**
  90       * Method to handle the `<id>` element for the feed.
  91       *
  92       * @param   Feed               $feed  The Feed object being built from the parsed feed.
  93       * @param   \SimpleXMLElement  $el    The current XML element object to handle.
  94       *
  95       * @return  void
  96       *
  97       * @since   3.1.4
  98       */
  99      protected function handleId(Feed $feed, \SimpleXMLElement $el)
 100      {
 101          $feed->uri = (string) $el;
 102      }
 103  
 104      /**
 105       * Method to handle the `<link>` element for the feed.
 106       *
 107       * @param   Feed               $feed  The Feed object being built from the parsed feed.
 108       * @param   \SimpleXMLElement  $el    The current XML element object to handle.
 109       *
 110       * @return  void
 111       *
 112       * @since   3.1.4
 113       */
 114      protected function handleLink(Feed $feed, \SimpleXMLElement $el)
 115      {
 116          $link = new FeedLink();
 117          $link->uri      = (string) $el['href'];
 118          $link->language = (string) $el['hreflang'];
 119          $link->length   = (int) $el['length'];
 120          $link->relation = (string) $el['rel'];
 121          $link->title    = $this->inputFilter->clean((string) $el['title'], 'html');
 122          $link->type     = (string) $el['type'];
 123  
 124          $feed->link = $link;
 125      }
 126  
 127      /**
 128       * Method to handle the `<rights>` element for the feed.
 129       *
 130       * @param   Feed               $feed  The Feed object being built from the parsed feed.
 131       * @param   \SimpleXMLElement  $el    The current XML element object to handle.
 132       *
 133       * @return  void
 134       *
 135       * @since   3.1.4
 136       */
 137      protected function handleRights(Feed $feed, \SimpleXMLElement $el)
 138      {
 139          $feed->copyright = $this->inputFilter->clean((string) $el, 'html');
 140      }
 141  
 142      /**
 143       * Method to handle the `<subtitle>` element for the feed.
 144       *
 145       * @param   Feed               $feed  The Feed object being built from the parsed feed.
 146       * @param   \SimpleXMLElement  $el    The current XML element object to handle.
 147       *
 148       * @return  void
 149       *
 150       * @since   3.1.4
 151       */
 152      protected function handleSubtitle(Feed $feed, \SimpleXMLElement $el)
 153      {
 154          $feed->description = $this->inputFilter->clean((string) $el, 'html');
 155      }
 156  
 157      /**
 158       * Method to handle the `<title>` element for the feed.
 159       *
 160       * @param   Feed               $feed  The Feed object being built from the parsed feed.
 161       * @param   \SimpleXMLElement  $el    The current XML element object to handle.
 162       *
 163       * @return  void
 164       *
 165       * @since   3.1.4
 166       */
 167      protected function handleTitle(Feed $feed, \SimpleXMLElement $el)
 168      {
 169          $feed->title = $this->inputFilter->clean((string) $el, 'html');
 170      }
 171  
 172      /**
 173       * Method to handle the `<updated>` element for the feed.
 174       *
 175       * @param   Feed               $feed  The Feed object being built from the parsed feed.
 176       * @param   \SimpleXMLElement  $el    The current XML element object to handle.
 177       *
 178       * @return  void
 179       *
 180       * @since   3.1.4
 181       */
 182      protected function handleUpdated(Feed $feed, \SimpleXMLElement $el)
 183      {
 184          $feed->updatedDate = $this->inputFilter->clean((string) $el, 'html');
 185      }
 186  
 187      /**
 188       * Method to initialise the feed for parsing.  Here we detect the version and advance the stream
 189       * reader so that it is ready to parse feed elements.
 190       *
 191       * @return  void
 192       *
 193       * @since   3.1.4
 194       */
 195      protected function initialise()
 196      {
 197          // We are on the first XML Element after the xml doc type declaration
 198          $this->version = ($this->stream->getAttribute('version') == '0.3') ? '0.3' : '1.0';
 199          $this->moveToNextElement();
 200      }
 201  
 202      /**
 203       * Method to handle a `<entry>` element for the feed.
 204       *
 205       * @param   FeedEntry          $entry  The FeedEntry object being built from the parsed feed entry.
 206       * @param   \SimpleXMLElement  $el     The current XML element object to handle.
 207       *
 208       * @return  void
 209       *
 210       * @since   3.1.4
 211       */
 212      protected function processFeedEntry(FeedEntry $entry, \SimpleXMLElement $el)
 213      {
 214          $entry->uri         = (string) $el->id;
 215          $entry->title       = $this->inputFilter->clean((string) $el->title, 'html');
 216          $entry->updatedDate = $this->inputFilter->clean((string) $el->updated, 'html');
 217          $entry->content     = $this->inputFilter->clean((string) $el->summary, 'html');
 218  
 219          if (!$entry->content) {
 220              $entry->content = $this->inputFilter->clean((string) $el->content, 'html');
 221          }
 222  
 223          if (filter_var($entry->uri, FILTER_VALIDATE_URL) === false && !\is_null($el->link) && $el->link) {
 224              $link = $el->link;
 225  
 226              if (\is_array($link)) {
 227                  $link = $this->bestLinkForUri($link);
 228              }
 229  
 230              $uri = (string) $link['href'];
 231  
 232              if ($uri) {
 233                  $entry->uri = $uri;
 234              }
 235          }
 236      }
 237  
 238      /**
 239       * If there is more than one <link> in the feed entry, find the most appropriate one and return it.
 240       *
 241       * @param   array  $links  Array of <link> elements from the feed entry.
 242       *
 243       * @return  \SimpleXMLElement
 244       */
 245      private function bestLinkForUri(array $links)
 246      {
 247          $linkPrefs = array('', 'self', 'alternate');
 248  
 249          foreach ($linkPrefs as $pref) {
 250              foreach ($links as $link) {
 251                  $rel = (string) $link['rel'];
 252  
 253                  if ($rel === $pref) {
 254                      return $link;
 255                  }
 256              }
 257          }
 258  
 259          return array_shift($links);
 260      }
 261  }


Generated: Wed Sep 7 05:41:13 2022 Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer