"Fossies" - the Fresh Open Source Software Archive

Member "drupal-9.4.5/vendor/laminas/laminas-feed/src/Writer/Renderer/Feed/AbstractAtom.php" (24 Mar 2022, 11656 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\Writer\Renderer\Feed;
    4 
    5 use DateTime;
    6 use DOMDocument;
    7 use DOMElement;
    8 use Laminas\Feed\Writer;
    9 use Laminas\Feed\Writer\Renderer;
   10 use Laminas\Feed\Writer\Version;
   11 
   12 use function array_key_exists;
   13 use function strtolower;
   14 
   15 class AbstractAtom extends Renderer\AbstractRenderer
   16 {
   17     /**
   18      * @param Writer\AbstractFeed $container
   19      */
   20     public function __construct($container)
   21     {
   22         parent::__construct($container);
   23     }
   24 
   25     // phpcs:disable PSR2.Methods.MethodDeclaration.Underscore
   26 
   27     /**
   28      * Set feed language
   29      *
   30      * @return void
   31      */
   32     protected function _setLanguage(DOMDocument $dom, DOMElement $root)
   33     {
   34         if ($this->getDataContainer()->getLanguage()) {
   35             $root->setAttribute('xml:lang', $this->getDataContainer()->getLanguage());
   36         }
   37     }
   38 
   39     /**
   40      * Set feed title
   41      *
   42      * @return void
   43      * @throws Writer\Exception\InvalidArgumentException
   44      */
   45     protected function _setTitle(DOMDocument $dom, DOMElement $root)
   46     {
   47         if (! $this->getDataContainer()->getTitle()) {
   48             $message   = 'Atom 1.0 feed elements MUST contain exactly one'
   49                 . ' atom:title element but a title has not been set';
   50             $exception = new Writer\Exception\InvalidArgumentException($message);
   51             if (! $this->ignoreExceptions) {
   52                 throw $exception;
   53             } else {
   54                 $this->exceptions[] = $exception;
   55                 return;
   56             }
   57         }
   58 
   59         $title = $dom->createElement('title');
   60         $root->appendChild($title);
   61         $title->setAttribute('type', 'text');
   62         $text = $dom->createTextNode($this->getDataContainer()->getTitle());
   63         $title->appendChild($text);
   64     }
   65 
   66     /**
   67      * Set feed description
   68      *
   69      * @return void
   70      */
   71     protected function _setDescription(DOMDocument $dom, DOMElement $root)
   72     {
   73         if (! $this->getDataContainer()->getDescription()) {
   74             return;
   75         }
   76         $subtitle = $dom->createElement('subtitle');
   77         $root->appendChild($subtitle);
   78         $subtitle->setAttribute('type', 'text');
   79         $text = $dom->createTextNode($this->getDataContainer()->getDescription());
   80         $subtitle->appendChild($text);
   81     }
   82 
   83     /**
   84      * Set date feed was last modified
   85      *
   86      * @return void
   87      * @throws Writer\Exception\InvalidArgumentException
   88      */
   89     protected function _setDateModified(DOMDocument $dom, DOMElement $root)
   90     {
   91         if (! $this->getDataContainer()->getDateModified()) {
   92             $message   = 'Atom 1.0 feed elements MUST contain exactly one'
   93                 . ' atom:updated element but a modification date has not been set';
   94             $exception = new Writer\Exception\InvalidArgumentException($message);
   95             if (! $this->ignoreExceptions) {
   96                 throw $exception;
   97             } else {
   98                 $this->exceptions[] = $exception;
   99                 return;
  100             }
  101         }
  102 
  103         $updated = $dom->createElement('updated');
  104         $root->appendChild($updated);
  105         $text = $dom->createTextNode(
  106             $this->getDataContainer()->getDateModified()->format(DateTime::ATOM)
  107         );
  108         $updated->appendChild($text);
  109     }
  110 
  111     /**
  112      * Set feed generator string
  113      *
  114      * @return void
  115      */
  116     protected function _setGenerator(DOMDocument $dom, DOMElement $root)
  117     {
  118         if (! $this->getDataContainer()->getGenerator()) {
  119             $this->getDataContainer()->setGenerator(
  120                 'Laminas_Feed_Writer',
  121                 Version::VERSION,
  122                 'https://getlaminas.org'
  123             );
  124         }
  125 
  126         $gdata     = $this->getDataContainer()->getGenerator();
  127         $generator = $dom->createElement('generator');
  128         $root->appendChild($generator);
  129         $text = $dom->createTextNode($gdata['name']);
  130         $generator->appendChild($text);
  131         if (array_key_exists('uri', $gdata)) {
  132             $generator->setAttribute('uri', $gdata['uri']);
  133         }
  134         if (array_key_exists('version', $gdata)) {
  135             $generator->setAttribute('version', $gdata['version']);
  136         }
  137     }
  138 
  139     /**
  140      * Set link to feed
  141      *
  142      * @return void
  143      */
  144     protected function _setLink(DOMDocument $dom, DOMElement $root)
  145     {
  146         if (! $this->getDataContainer()->getLink()) {
  147             return;
  148         }
  149         $link = $dom->createElement('link');
  150         $root->appendChild($link);
  151         $link->setAttribute('rel', 'alternate');
  152         $link->setAttribute('type', 'text/html');
  153         $link->setAttribute('href', $this->getDataContainer()->getLink());
  154     }
  155 
  156     /**
  157      * Set feed links
  158      *
  159      * @return void
  160      * @throws Writer\Exception\InvalidArgumentException
  161      */
  162     protected function _setFeedLinks(DOMDocument $dom, DOMElement $root)
  163     {
  164         $flinks = $this->getDataContainer()->getFeedLinks();
  165         if (! $flinks || ! array_key_exists('atom', $flinks)) {
  166             $message   = 'Atom 1.0 feed elements SHOULD contain one atom:link '
  167                 . 'element with a rel attribute value of "self".  This is the '
  168                 . 'preferred URI for retrieving Atom Feed Documents representing '
  169                 . 'this Atom feed but a feed link has not been set';
  170             $exception = new Writer\Exception\InvalidArgumentException($message);
  171             if (! $this->ignoreExceptions) {
  172                 throw $exception;
  173             } else {
  174                 $this->exceptions[] = $exception;
  175                 return;
  176             }
  177         }
  178 
  179         foreach ($flinks as $type => $href) {
  180             $mime  = 'application/' . strtolower($type) . '+xml';
  181             $flink = $dom->createElement('link');
  182             $root->appendChild($flink);
  183             $flink->setAttribute('rel', 'self');
  184             $flink->setAttribute('type', $mime);
  185             $flink->setAttribute('href', $href);
  186         }
  187     }
  188 
  189     /**
  190      * Set feed authors
  191      *
  192      * @return void
  193      */
  194     protected function _setAuthors(DOMDocument $dom, DOMElement $root)
  195     {
  196         $authors = $this->container->getAuthors();
  197         if (! $authors || empty($authors)) {
  198             /**
  199              * Technically we should defer an exception until we can check
  200              * that all entries contain an author. If any entry is missing
  201              * an author, then a missing feed author element is invalid
  202              */
  203             return;
  204         }
  205         foreach ($authors as $data) {
  206             $author = $this->dom->createElement('author');
  207             $name   = $this->dom->createElement('name');
  208             $author->appendChild($name);
  209             $root->appendChild($author);
  210             $text = $dom->createTextNode($data['name']);
  211             $name->appendChild($text);
  212             if (array_key_exists('email', $data)) {
  213                 $email = $this->dom->createElement('email');
  214                 $author->appendChild($email);
  215                 $text = $dom->createTextNode($data['email']);
  216                 $email->appendChild($text);
  217             }
  218             if (array_key_exists('uri', $data)) {
  219                 $uri = $this->dom->createElement('uri');
  220                 $author->appendChild($uri);
  221                 $text = $dom->createTextNode($data['uri']);
  222                 $uri->appendChild($text);
  223             }
  224         }
  225     }
  226 
  227     /**
  228      * Set feed identifier
  229      *
  230      * @return void
  231      * @throws Writer\Exception\InvalidArgumentException
  232      */
  233     protected function _setId(DOMDocument $dom, DOMElement $root)
  234     {
  235         if (
  236             ! $this->getDataContainer()->getId()
  237             && ! $this->getDataContainer()->getLink()
  238         ) {
  239             $message   = 'Atom 1.0 feed elements MUST contain exactly one '
  240                 . 'atom:id element, or as an alternative, we can use the same '
  241                 . 'value as atom:link however neither a suitable link nor an '
  242                 . 'id have been set';
  243             $exception = new Writer\Exception\InvalidArgumentException($message);
  244             if (! $this->ignoreExceptions) {
  245                 throw $exception;
  246             } else {
  247                 $this->exceptions[] = $exception;
  248                 return;
  249             }
  250         }
  251 
  252         if (! $this->getDataContainer()->getId()) {
  253             $this->getDataContainer()->setId(
  254                 $this->getDataContainer()->getLink()
  255             );
  256         }
  257         $id = $dom->createElement('id');
  258         $root->appendChild($id);
  259         $text = $dom->createTextNode($this->getDataContainer()->getId());
  260         $id->appendChild($text);
  261     }
  262 
  263     /**
  264      * Set feed copyright
  265      *
  266      * @return void
  267      */
  268     protected function _setCopyright(DOMDocument $dom, DOMElement $root)
  269     {
  270         $copyright = $this->getDataContainer()->getCopyright();
  271         if (! $copyright) {
  272             return;
  273         }
  274         $copy = $dom->createElement('rights');
  275         $root->appendChild($copy);
  276         $text = $dom->createTextNode($copyright);
  277         $copy->appendChild($text);
  278     }
  279 
  280     /**
  281      * Set feed level logo (image)
  282      *
  283      * @return void
  284      */
  285     protected function _setImage(DOMDocument $dom, DOMElement $root)
  286     {
  287         $image = $this->getDataContainer()->getImage();
  288         if (! $image) {
  289             return;
  290         }
  291         $img = $dom->createElement('logo');
  292         $root->appendChild($img);
  293         $text = $dom->createTextNode($image['uri']);
  294         $img->appendChild($text);
  295     }
  296 
  297     /**
  298      * Set date feed was created
  299      *
  300      * @return void
  301      */
  302     protected function _setDateCreated(DOMDocument $dom, DOMElement $root)
  303     {
  304         if (! $this->getDataContainer()->getDateCreated()) {
  305             return;
  306         }
  307         if (! $this->getDataContainer()->getDateModified()) {
  308             $this->getDataContainer()->setDateModified(
  309                 $this->getDataContainer()->getDateCreated()
  310             );
  311         }
  312     }
  313 
  314     /**
  315      * Set base URL to feed links
  316      *
  317      * @return void
  318      */
  319     protected function _setBaseUrl(DOMDocument $dom, DOMElement $root)
  320     {
  321         $baseUrl = $this->getDataContainer()->getBaseUrl();
  322         if (! $baseUrl) {
  323             return;
  324         }
  325         $root->setAttribute('xml:base', $baseUrl);
  326     }
  327 
  328     /**
  329      * Set hubs to which this feed pushes
  330      *
  331      * @return void
  332      */
  333     protected function _setHubs(DOMDocument $dom, DOMElement $root)
  334     {
  335         $hubs = $this->getDataContainer()->getHubs();
  336         if (! $hubs) {
  337             return;
  338         }
  339         foreach ($hubs as $hubUrl) {
  340             $hub = $dom->createElement('link');
  341             $hub->setAttribute('rel', 'hub');
  342             $hub->setAttribute('href', $hubUrl);
  343             $root->appendChild($hub);
  344         }
  345     }
  346 
  347     /**
  348      * Set feed categories
  349      *
  350      * @return void
  351      */
  352     protected function _setCategories(DOMDocument $dom, DOMElement $root)
  353     {
  354         $categories = $this->getDataContainer()->getCategories();
  355         if (! $categories) {
  356             return;
  357         }
  358         foreach ($categories as $cat) {
  359             $category = $dom->createElement('category');
  360             $category->setAttribute('term', $cat['term']);
  361             if (isset($cat['label'])) {
  362                 $category->setAttribute('label', $cat['label']);
  363             } else {
  364                 $category->setAttribute('label', $cat['term']);
  365             }
  366             if (isset($cat['scheme'])) {
  367                 $category->setAttribute('scheme', $cat['scheme']);
  368             }
  369             $root->appendChild($category);
  370         }
  371     }
  372 
  373     // phpcs:enable PSR2.Methods.MethodDeclaration.Underscore
  374 }