"Fossies" - the Fresh Open Source Software Archive

Member "drupal-9.4.5/vendor/laminas/laminas-feed/src/Reader/AbstractFeed.php" (24 Mar 2022, 7077 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. See also the last Fossies "Diffs" side-by-side code changes report for "AbstractFeed.php": 9.3.16_vs_9.4.0.

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