"Fossies" - the Fresh Open Source Software Archive

Member "drupal-9.4.5/vendor/laminas/laminas-feed/src/Reader/Feed/AbstractFeed.php" (24 Mar 2022, 7257 Bytes) of package /linux/www/drupal-9.4.5.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) PHP source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 <?php
    2 
    3 namespace Laminas\Feed\Reader\Feed;
    4 
    5 use DOMDocument;
    6 use DOMElement;
    7 use DOMXPath;
    8 use Laminas\Feed\Reader;
    9 use Laminas\Feed\Reader\Exception;
   10 // phpcs:ignore SlevomatCodingStandard.Namespaces.UnusedUses.UnusedUse
   11 use ReturnTypeWillChange;
   12 
   13 use function array_key_exists;
   14 use function call_user_func_array;
   15 use function count;
   16 use function in_array;
   17 use function method_exists;
   18 use function sprintf;
   19 use function strpos;
   20 
   21 abstract class AbstractFeed implements FeedInterface
   22 {
   23     /**
   24      * Parsed feed data
   25      *
   26      * @var array
   27      */
   28     protected $data = [];
   29 
   30     /**
   31      * Parsed feed data in the shape of a DOMDocument
   32      *
   33      * @var DOMDocument
   34      */
   35     protected $domDocument;
   36 
   37     /**
   38      * An array of parsed feed entries
   39      *
   40      * @var array
   41      */
   42     protected $entries = [];
   43 
   44     /**
   45      * A pointer for the iterator to keep track of the entries array
   46      *
   47      * @var int
   48      */
   49     protected $entriesKey = 0;
   50 
   51     /**
   52      * The base XPath query used to retrieve feed data
   53      *
   54      * @var DOMXPath
   55      */
   56     protected $xpath;
   57 
   58     /**
   59      * Array of loaded extensions
   60      *
   61      * @var array
   62      */
   63     protected $extensions = [];
   64 
   65     /**
   66      * Original Source URI (set if imported from a URI)
   67      *
   68      * @var string
   69      */
   70     protected $originalSourceUri;
   71 
   72     /**
   73      * @param DOMDocument $domDocument The DOM object for the feed's XML
   74      * @param null|string $type Feed type
   75      */
   76     public function __construct(DOMDocument $domDocument, $type = null)
   77     {
   78         $this->domDocument = $domDocument;
   79         $this->xpath       = new DOMXPath($this->domDocument);
   80 
   81         if ($type !== null) {
   82             $this->data['type'] = $type;
   83         } else {
   84             $this->data['type'] = Reader\Reader::detectType($this->domDocument);
   85         }
   86         $this->registerNamespaces();
   87         $this->indexEntries();
   88         $this->loadExtensions();
   89     }
   90 
   91     /**
   92      * Set an original source URI for the feed being parsed. This value
   93      * is returned from getFeedLink() method if the feed does not carry
   94      * a self-referencing URI.
   95      *
   96      * @param string $uri
   97      * @return void
   98      */
   99     public function setOriginalSourceUri($uri)
  100     {
  101         $this->originalSourceUri = $uri;
  102     }
  103 
  104     /**
  105      * Get an original source URI for the feed being parsed. Returns null if
  106      * unset or the feed was not imported from a URI.
  107      *
  108      * @return null|string
  109      */
  110     public function getOriginalSourceUri()
  111     {
  112         return $this->originalSourceUri;
  113     }
  114 
  115     /**
  116      * Get the number of feed entries.
  117      * Required by the Iterator interface.
  118      *
  119      * @return int
  120      */
  121     #[ReturnTypeWillChange]
  122     public function count()
  123     {
  124         return count($this->entries);
  125     }
  126 
  127     /**
  128      * Return the current entry
  129      *
  130      * @return Reader\Entry\EntryInterface
  131      */
  132     #[ReturnTypeWillChange]
  133     public function current()
  134     {
  135         if (0 === strpos($this->getType(), 'rss')) {
  136             $reader = new Reader\Entry\Rss($this->entries[$this->key()], $this->key(), $this->getType());
  137         } else {
  138             $reader = new Reader\Entry\Atom($this->entries[$this->key()], $this->key(), $this->getType());
  139         }
  140 
  141         $reader->setXpath($this->xpath);
  142 
  143         return $reader;
  144     }
  145 
  146     /**
  147      * Get the DOM
  148      *
  149      * @return DOMDocument
  150      */
  151     public function getDomDocument()
  152     {
  153         return $this->domDocument;
  154     }
  155 
  156     /**
  157      * Get the Feed's encoding
  158      *
  159      * @return string
  160      */
  161     public function getEncoding()
  162     {
  163         $assumed = $this->getDomDocument()->encoding;
  164         if (empty($assumed)) {
  165             $assumed = 'UTF-8';
  166         }
  167         return $assumed;
  168     }
  169 
  170     /**
  171      * Get feed as xml
  172      *
  173      * @return string
  174      */
  175     public function saveXml()
  176     {
  177         return $this->getDomDocument()->saveXML();
  178     }
  179 
  180     /**
  181      * Get the DOMElement representing the items/feed element
  182      *
  183      * @return DOMElement
  184      */
  185     public function getElement()
  186     {
  187         return $this->getDomDocument()->documentElement;
  188     }
  189 
  190     /**
  191      * Get the DOMXPath object for this feed
  192      *
  193      * @return DOMXPath
  194      */
  195     public function getXpath()
  196     {
  197         return $this->xpath;
  198     }
  199 
  200     /**
  201      * Get the feed type
  202      *
  203      * @return string
  204      */
  205     public function getType()
  206     {
  207         return $this->data['type'];
  208     }
  209 
  210     /**
  211      * Return the current feed key
  212      *
  213      * @return int
  214      */
  215     #[ReturnTypeWillChange]
  216     public function key()
  217     {
  218         return $this->entriesKey;
  219     }
  220 
  221     /**
  222      * Move the feed pointer forward
  223      */
  224     #[ReturnTypeWillChange]
  225     public function next()
  226     {
  227         ++$this->entriesKey;
  228     }
  229 
  230     /**
  231      * Reset the pointer in the feed object
  232      */
  233     #[ReturnTypeWillChange]
  234     public function rewind()
  235     {
  236         $this->entriesKey = 0;
  237     }
  238 
  239     /**
  240      * Check to see if the iterator is still valid
  241      *
  242      * @return bool
  243      */
  244     #[ReturnTypeWillChange]
  245     public function valid()
  246     {
  247         return 0 <= $this->entriesKey && $this->entriesKey < $this->count();
  248     }
  249 
  250     /** @return array */
  251     public function getExtensions()
  252     {
  253         return $this->extensions;
  254     }
  255 
  256     /**
  257      * @param string $method
  258      * @param mixed[] $args
  259      * @return mixed
  260      */
  261     public function __call($method, $args)
  262     {
  263         foreach ($this->extensions as $extension) {
  264             if (method_exists($extension, $method)) {
  265                 return call_user_func_array([$extension, $method], $args);
  266             }
  267         }
  268         throw new Exception\BadMethodCallException(
  269             'Method: ' . $method . ' does not exist and could not be located on a registered Extension'
  270         );
  271     }
  272 
  273     /**
  274      * Return an Extension object with the matching name (postfixed with _Feed)
  275      *
  276      * @param  string $name
  277      * @return null|Reader\Extension\AbstractFeed
  278      */
  279     public function getExtension($name)
  280     {
  281         if (array_key_exists($name . '\\Feed', $this->extensions)) {
  282             return $this->extensions[$name . '\\Feed'];
  283         }
  284 
  285         return null;
  286     }
  287 
  288     protected function loadExtensions()
  289     {
  290         $all     = Reader\Reader::getExtensions();
  291         $manager = Reader\Reader::getExtensionManager();
  292         $feed    = $all['feed'];
  293         foreach ($feed as $extension) {
  294             if (in_array($extension, $all['core'])) {
  295                 continue;
  296             }
  297             if (! $manager->has($extension)) {
  298                 throw new Exception\RuntimeException(
  299                     sprintf('Unable to load extension "%s"; cannot find class', $extension)
  300                 );
  301             }
  302             $plugin = $manager->get($extension);
  303             $plugin->setDomDocument($this->getDomDocument());
  304             $plugin->setType($this->data['type']);
  305             $plugin->setXpath($this->xpath);
  306             $this->extensions[$extension] = $plugin;
  307         }
  308     }
  309 
  310     /**
  311      * Read all entries to the internal entries array
  312      */
  313     abstract protected function indexEntries();
  314 
  315     /**
  316      * Register the default namespaces for the current feed format
  317      */
  318     abstract protected function registerNamespaces();
  319 }