"Fossies" - the Fresh Open Source Software Archive

Member "drupal-9.4.5/vendor/laminas/laminas-feed/src/Reader/Entry/AbstractEntry.php" (24 Mar 2022, 5155 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\Entry;
    4 
    5 use DOMDocument;
    6 use DOMElement;
    7 use DOMXPath;
    8 use Laminas\Feed\Reader;
    9 use Laminas\Feed\Reader\Exception;
   10 
   11 use function call_user_func_array;
   12 use function in_array;
   13 use function method_exists;
   14 use function sprintf;
   15 use function version_compare;
   16 
   17 use const PHP_VERSION;
   18 
   19 abstract class AbstractEntry
   20 {
   21     /**
   22      * Feed entry data
   23      *
   24      * @var array
   25      */
   26     protected $data = [];
   27 
   28     /**
   29      * DOM document object
   30      *
   31      * @var DOMDocument
   32      */
   33     protected $domDocument;
   34 
   35     /**
   36      * Entry instance
   37      *
   38      * @var DOMElement
   39      */
   40     protected $entry;
   41 
   42     /**
   43      * Pointer to the current entry
   44      *
   45      * @var int
   46      */
   47     protected $entryKey = 0;
   48 
   49     /**
   50      * XPath object
   51      *
   52      * @var DOMXPath
   53      */
   54     protected $xpath;
   55 
   56     /**
   57      * Registered extensions
   58      *
   59      * @var array
   60      */
   61     protected $extensions = [];
   62 
   63     /**
   64      * @param int $entryKey
   65      * @param null|string $type
   66      */
   67     public function __construct(DOMElement $entry, $entryKey, $type = null)
   68     {
   69         $this->entry       = $entry;
   70         $this->entryKey    = $entryKey;
   71         $this->domDocument = $entry->ownerDocument;
   72         if ($type !== null) {
   73             $this->data['type'] = $type;
   74         } elseif ($this->domDocument !== null) {
   75             $this->data['type'] = Reader\Reader::detectType($this->domDocument);
   76         } else {
   77             $this->data['type'] = Reader\Reader::TYPE_ANY;
   78         }
   79         $this->loadExtensions();
   80     }
   81 
   82     /**
   83      * Get the DOM
   84      *
   85      * @return DOMDocument
   86      */
   87     public function getDomDocument()
   88     {
   89         return $this->domDocument;
   90     }
   91 
   92     /**
   93      * Get the entry element
   94      *
   95      * @return DOMElement
   96      */
   97     public function getElement()
   98     {
   99         return $this->entry;
  100     }
  101 
  102     /**
  103      * Get the Entry's encoding
  104      *
  105      * @return string
  106      */
  107     public function getEncoding()
  108     {
  109         $assumed = $this->getDomDocument()->encoding;
  110         if (empty($assumed)) {
  111             $assumed = 'UTF-8';
  112         }
  113         return $assumed;
  114     }
  115 
  116     /**
  117      * Get entry as xml
  118      *
  119      * @return string
  120      */
  121     public function saveXml()
  122     {
  123         $dom   = new DOMDocument('1.0', $this->getEncoding());
  124         $deep  = version_compare(PHP_VERSION, '7', 'ge') ? 1 : true;
  125         $entry = $dom->importNode($this->getElement(), $deep);
  126         $dom->appendChild($entry);
  127         return $dom->saveXML();
  128     }
  129 
  130     /**
  131      * Get the entry type
  132      *
  133      * @return string
  134      */
  135     public function getType()
  136     {
  137         return $this->data['type'];
  138     }
  139 
  140     /**
  141      * Get the XPath query object
  142      *
  143      * @return DOMXPath
  144      */
  145     public function getXpath()
  146     {
  147         if (! $this->xpath) {
  148             $this->setXpath(new DOMXPath($this->getDomDocument()));
  149         }
  150         return $this->xpath;
  151     }
  152 
  153     /**
  154      * Set the XPath query
  155      *
  156      * @return $this
  157      */
  158     public function setXpath(DOMXPath $xpath)
  159     {
  160         $this->xpath = $xpath;
  161         return $this;
  162     }
  163 
  164     /**
  165      * Get registered extensions
  166      *
  167      * @return array
  168      */
  169     public function getExtensions()
  170     {
  171         return $this->extensions;
  172     }
  173 
  174     /**
  175      * Return an Extension object with the matching name (postfixed with _Entry)
  176      *
  177      * @param  string $name
  178      * @return null|Reader\Extension\AbstractEntry
  179      */
  180     public function getExtension($name)
  181     {
  182         $extensionClass = $name . '\\Entry';
  183         return isset($this->extensions[$extensionClass])
  184             && $this->extensions[$extensionClass] instanceof Reader\Extension\AbstractEntry
  185             ? $this->extensions[$extensionClass]
  186             : null;
  187     }
  188 
  189     /**
  190      * Method overloading: call given method on first extension implementing it
  191      *
  192      * @param  string $method
  193      * @param  array $args
  194      * @return mixed
  195      * @throws Exception\RuntimeException If no extensions implements the method.
  196      */
  197     public function __call($method, $args)
  198     {
  199         foreach ($this->extensions as $extension) {
  200             if (method_exists($extension, $method)) {
  201                 return call_user_func_array([$extension, $method], $args);
  202             }
  203         }
  204         throw new Exception\RuntimeException(sprintf(
  205             'Method: %s does not exist and could not be located on a registered Extension',
  206             $method
  207         ));
  208     }
  209 
  210     /**
  211      * Load extensions from Laminas\Feed\Reader\Reader
  212      *
  213      * @return void
  214      */
  215     protected function loadExtensions()
  216     {
  217         $all     = Reader\Reader::getExtensions();
  218         $manager = Reader\Reader::getExtensionManager();
  219         $feed    = $all['entry'];
  220         foreach ($feed as $extension) {
  221             if (in_array($extension, $all['core'])) {
  222                 continue;
  223             }
  224             $plugin = $manager->get($extension);
  225             $plugin->setEntryElement($this->getElement());
  226             $plugin->setEntryKey($this->entryKey);
  227             $plugin->setType($this->data['type']);
  228             $this->extensions[$extension] = $plugin;
  229         }
  230     }
  231 }