"Fossies" - the Fresh Open Source Software Archive

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