annotate vendor/zendframework/zend-feed/src/Writer/Renderer/Feed/Rss.php @ 2:92f882872392

Trusted hosts, + remove migration modules
author Chris Cannam
date Tue, 05 Dec 2017 09:26:43 +0000
parents 4c8ae668cc8c
children 7a779792577d
rev   line source
Chris@0 1 <?php
Chris@0 2 /**
Chris@0 3 * Zend Framework (http://framework.zend.com/)
Chris@0 4 *
Chris@0 5 * @link http://github.com/zendframework/zf2 for the canonical source repository
Chris@0 6 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
Chris@0 7 * @license http://framework.zend.com/license/new-bsd New BSD License
Chris@0 8 */
Chris@0 9
Chris@0 10 namespace Zend\Feed\Writer\Renderer\Feed;
Chris@0 11
Chris@0 12 use DateTime;
Chris@0 13 use DOMDocument;
Chris@0 14 use DOMElement;
Chris@0 15 use Zend\Feed\Uri;
Chris@0 16 use Zend\Feed\Writer;
Chris@0 17 use Zend\Feed\Writer\Renderer;
Chris@0 18 use Zend\Feed\Writer\Version;
Chris@0 19
Chris@0 20 /**
Chris@0 21 */
Chris@0 22 class Rss extends Renderer\AbstractRenderer implements Renderer\RendererInterface
Chris@0 23 {
Chris@0 24 /**
Chris@0 25 * Constructor
Chris@0 26 *
Chris@0 27 * @param Writer\Feed $container
Chris@0 28 */
Chris@0 29 public function __construct(Writer\Feed $container)
Chris@0 30 {
Chris@0 31 parent::__construct($container);
Chris@0 32 }
Chris@0 33
Chris@0 34 /**
Chris@0 35 * Render RSS feed
Chris@0 36 *
Chris@0 37 * @return self
Chris@0 38 */
Chris@0 39 public function render()
Chris@0 40 {
Chris@0 41 $this->dom = new DOMDocument('1.0', $this->container->getEncoding());
Chris@0 42 $this->dom->formatOutput = true;
Chris@0 43 $this->dom->substituteEntities = false;
Chris@0 44 $rss = $this->dom->createElement('rss');
Chris@0 45 $this->setRootElement($rss);
Chris@0 46 $rss->setAttribute('version', '2.0');
Chris@0 47
Chris@0 48 $channel = $this->dom->createElement('channel');
Chris@0 49 $rss->appendChild($channel);
Chris@0 50 $this->dom->appendChild($rss);
Chris@0 51 $this->_setLanguage($this->dom, $channel);
Chris@0 52 $this->_setBaseUrl($this->dom, $channel);
Chris@0 53 $this->_setTitle($this->dom, $channel);
Chris@0 54 $this->_setDescription($this->dom, $channel);
Chris@0 55 $this->_setImage($this->dom, $channel);
Chris@0 56 $this->_setDateCreated($this->dom, $channel);
Chris@0 57 $this->_setDateModified($this->dom, $channel);
Chris@0 58 $this->_setLastBuildDate($this->dom, $channel);
Chris@0 59 $this->_setGenerator($this->dom, $channel);
Chris@0 60 $this->_setLink($this->dom, $channel);
Chris@0 61 $this->_setAuthors($this->dom, $channel);
Chris@0 62 $this->_setCopyright($this->dom, $channel);
Chris@0 63 $this->_setCategories($this->dom, $channel);
Chris@0 64
Chris@0 65 foreach ($this->extensions as $ext) {
Chris@0 66 $ext->setType($this->getType());
Chris@0 67 $ext->setRootElement($this->getRootElement());
Chris@0 68 $ext->setDOMDocument($this->getDOMDocument(), $channel);
Chris@0 69 $ext->render();
Chris@0 70 }
Chris@0 71
Chris@0 72 foreach ($this->container as $entry) {
Chris@0 73 if ($this->getDataContainer()->getEncoding()) {
Chris@0 74 $entry->setEncoding($this->getDataContainer()->getEncoding());
Chris@0 75 }
Chris@0 76 if ($entry instanceof Writer\Entry) {
Chris@0 77 $renderer = new Renderer\Entry\Rss($entry);
Chris@0 78 } else {
Chris@0 79 continue;
Chris@0 80 }
Chris@0 81 if ($this->ignoreExceptions === true) {
Chris@0 82 $renderer->ignoreExceptions();
Chris@0 83 }
Chris@0 84 $renderer->setType($this->getType());
Chris@0 85 $renderer->setRootElement($this->dom->documentElement);
Chris@0 86 $renderer->render();
Chris@0 87 $element = $renderer->getElement();
Chris@0 88 $deep = version_compare(PHP_VERSION, '7', 'ge') ? 1 : true;
Chris@0 89 $imported = $this->dom->importNode($element, $deep);
Chris@0 90 $channel->appendChild($imported);
Chris@0 91 }
Chris@0 92 return $this;
Chris@0 93 }
Chris@0 94
Chris@0 95 /**
Chris@0 96 * Set feed language
Chris@0 97 *
Chris@0 98 * @param DOMDocument $dom
Chris@0 99 * @param DOMElement $root
Chris@0 100 * @return void
Chris@0 101 */
Chris@0 102 protected function _setLanguage(DOMDocument $dom, DOMElement $root)
Chris@0 103 {
Chris@0 104 $lang = $this->getDataContainer()->getLanguage();
Chris@0 105 if (!$lang) {
Chris@0 106 return;
Chris@0 107 }
Chris@0 108 $language = $dom->createElement('language');
Chris@0 109 $root->appendChild($language);
Chris@0 110 $language->nodeValue = $lang;
Chris@0 111 }
Chris@0 112
Chris@0 113 /**
Chris@0 114 * Set feed title
Chris@0 115 *
Chris@0 116 * @param DOMDocument $dom
Chris@0 117 * @param DOMElement $root
Chris@0 118 * @return void
Chris@0 119 * @throws Writer\Exception\InvalidArgumentException
Chris@0 120 */
Chris@0 121 protected function _setTitle(DOMDocument $dom, DOMElement $root)
Chris@0 122 {
Chris@0 123 if (!$this->getDataContainer()->getTitle()) {
Chris@0 124 $message = 'RSS 2.0 feed elements MUST contain exactly one'
Chris@0 125 . ' title element but a title has not been set';
Chris@0 126 $exception = new Writer\Exception\InvalidArgumentException($message);
Chris@0 127 if (!$this->ignoreExceptions) {
Chris@0 128 throw $exception;
Chris@0 129 } else {
Chris@0 130 $this->exceptions[] = $exception;
Chris@0 131 return;
Chris@0 132 }
Chris@0 133 }
Chris@0 134
Chris@0 135 $title = $dom->createElement('title');
Chris@0 136 $root->appendChild($title);
Chris@0 137 $text = $dom->createTextNode($this->getDataContainer()->getTitle());
Chris@0 138 $title->appendChild($text);
Chris@0 139 }
Chris@0 140
Chris@0 141 /**
Chris@0 142 * Set feed description
Chris@0 143 *
Chris@0 144 * @param DOMDocument $dom
Chris@0 145 * @param DOMElement $root
Chris@0 146 * @return void
Chris@0 147 * @throws Writer\Exception\InvalidArgumentException
Chris@0 148 */
Chris@0 149 protected function _setDescription(DOMDocument $dom, DOMElement $root)
Chris@0 150 {
Chris@0 151 if (!$this->getDataContainer()->getDescription()) {
Chris@0 152 $message = 'RSS 2.0 feed elements MUST contain exactly one'
Chris@0 153 . ' description element but one has not been set';
Chris@0 154 $exception = new Writer\Exception\InvalidArgumentException($message);
Chris@0 155 if (!$this->ignoreExceptions) {
Chris@0 156 throw $exception;
Chris@0 157 } else {
Chris@0 158 $this->exceptions[] = $exception;
Chris@0 159 return;
Chris@0 160 }
Chris@0 161 }
Chris@0 162 $subtitle = $dom->createElement('description');
Chris@0 163 $root->appendChild($subtitle);
Chris@0 164 $text = $dom->createTextNode($this->getDataContainer()->getDescription());
Chris@0 165 $subtitle->appendChild($text);
Chris@0 166 }
Chris@0 167
Chris@0 168 /**
Chris@0 169 * Set date feed was last modified
Chris@0 170 *
Chris@0 171 * @param DOMDocument $dom
Chris@0 172 * @param DOMElement $root
Chris@0 173 * @return void
Chris@0 174 */
Chris@0 175 protected function _setDateModified(DOMDocument $dom, DOMElement $root)
Chris@0 176 {
Chris@0 177 if (!$this->getDataContainer()->getDateModified()) {
Chris@0 178 return;
Chris@0 179 }
Chris@0 180
Chris@0 181 $updated = $dom->createElement('pubDate');
Chris@0 182 $root->appendChild($updated);
Chris@0 183 $text = $dom->createTextNode(
Chris@0 184 $this->getDataContainer()->getDateModified()->format(DateTime::RSS)
Chris@0 185 );
Chris@0 186 $updated->appendChild($text);
Chris@0 187 }
Chris@0 188
Chris@0 189 /**
Chris@0 190 * Set feed generator string
Chris@0 191 *
Chris@0 192 * @param DOMDocument $dom
Chris@0 193 * @param DOMElement $root
Chris@0 194 * @return void
Chris@0 195 */
Chris@0 196 protected function _setGenerator(DOMDocument $dom, DOMElement $root)
Chris@0 197 {
Chris@0 198 if (!$this->getDataContainer()->getGenerator()) {
Chris@0 199 $this->getDataContainer()->setGenerator(
Chris@0 200 'Zend_Feed_Writer',
Chris@0 201 Version::VERSION,
Chris@0 202 'http://framework.zend.com'
Chris@0 203 );
Chris@0 204 }
Chris@0 205
Chris@0 206 $gdata = $this->getDataContainer()->getGenerator();
Chris@0 207 $generator = $dom->createElement('generator');
Chris@0 208 $root->appendChild($generator);
Chris@0 209 $name = $gdata['name'];
Chris@0 210 if (array_key_exists('version', $gdata)) {
Chris@0 211 $name .= ' ' . $gdata['version'];
Chris@0 212 }
Chris@0 213 if (array_key_exists('uri', $gdata)) {
Chris@0 214 $name .= ' (' . $gdata['uri'] . ')';
Chris@0 215 }
Chris@0 216 $text = $dom->createTextNode($name);
Chris@0 217 $generator->appendChild($text);
Chris@0 218 }
Chris@0 219
Chris@0 220 /**
Chris@0 221 * Set link to feed
Chris@0 222 *
Chris@0 223 * @param DOMDocument $dom
Chris@0 224 * @param DOMElement $root
Chris@0 225 * @return void
Chris@0 226 * @throws Writer\Exception\InvalidArgumentException
Chris@0 227 */
Chris@0 228 protected function _setLink(DOMDocument $dom, DOMElement $root)
Chris@0 229 {
Chris@0 230 $value = $this->getDataContainer()->getLink();
Chris@0 231 if (!$value) {
Chris@0 232 $message = 'RSS 2.0 feed elements MUST contain exactly one'
Chris@0 233 . ' link element but one has not been set';
Chris@0 234 $exception = new Writer\Exception\InvalidArgumentException($message);
Chris@0 235 if (!$this->ignoreExceptions) {
Chris@0 236 throw $exception;
Chris@0 237 } else {
Chris@0 238 $this->exceptions[] = $exception;
Chris@0 239 return;
Chris@0 240 }
Chris@0 241 }
Chris@0 242 $link = $dom->createElement('link');
Chris@0 243 $root->appendChild($link);
Chris@0 244 $text = $dom->createTextNode($value);
Chris@0 245 $link->appendChild($text);
Chris@0 246 if (!Uri::factory($value)->isValid()) {
Chris@0 247 $link->setAttribute('isPermaLink', 'false');
Chris@0 248 }
Chris@0 249 }
Chris@0 250
Chris@0 251 /**
Chris@0 252 * Set feed authors
Chris@0 253 *
Chris@0 254 * @param DOMDocument $dom
Chris@0 255 * @param DOMElement $root
Chris@0 256 * @return void
Chris@0 257 */
Chris@0 258 protected function _setAuthors(DOMDocument $dom, DOMElement $root)
Chris@0 259 {
Chris@0 260 $authors = $this->getDataContainer()->getAuthors();
Chris@0 261 if (!$authors || empty($authors)) {
Chris@0 262 return;
Chris@0 263 }
Chris@0 264 foreach ($authors as $data) {
Chris@0 265 $author = $this->dom->createElement('author');
Chris@0 266 $name = $data['name'];
Chris@0 267 if (array_key_exists('email', $data)) {
Chris@0 268 $name = $data['email'] . ' (' . $data['name'] . ')';
Chris@0 269 }
Chris@0 270 $text = $dom->createTextNode($name);
Chris@0 271 $author->appendChild($text);
Chris@0 272 $root->appendChild($author);
Chris@0 273 }
Chris@0 274 }
Chris@0 275
Chris@0 276 /**
Chris@0 277 * Set feed copyright
Chris@0 278 *
Chris@0 279 * @param DOMDocument $dom
Chris@0 280 * @param DOMElement $root
Chris@0 281 * @return void
Chris@0 282 */
Chris@0 283 protected function _setCopyright(DOMDocument $dom, DOMElement $root)
Chris@0 284 {
Chris@0 285 $copyright = $this->getDataContainer()->getCopyright();
Chris@0 286 if (!$copyright) {
Chris@0 287 return;
Chris@0 288 }
Chris@0 289 $copy = $dom->createElement('copyright');
Chris@0 290 $root->appendChild($copy);
Chris@0 291 $text = $dom->createTextNode($copyright);
Chris@0 292 $copy->appendChild($text);
Chris@0 293 }
Chris@0 294
Chris@0 295 /**
Chris@0 296 * Set feed channel image
Chris@0 297 *
Chris@0 298 * @param DOMDocument $dom
Chris@0 299 * @param DOMElement $root
Chris@0 300 * @return void
Chris@0 301 * @throws Writer\Exception\InvalidArgumentException
Chris@0 302 */
Chris@0 303 protected function _setImage(DOMDocument $dom, DOMElement $root)
Chris@0 304 {
Chris@0 305 $image = $this->getDataContainer()->getImage();
Chris@0 306 if (!$image) {
Chris@0 307 return;
Chris@0 308 }
Chris@0 309
Chris@0 310 if (!isset($image['title']) || empty($image['title'])
Chris@0 311 || !is_string($image['title'])
Chris@0 312 ) {
Chris@0 313 $message = 'RSS 2.0 feed images must include a title';
Chris@0 314 $exception = new Writer\Exception\InvalidArgumentException($message);
Chris@0 315 if (!$this->ignoreExceptions) {
Chris@0 316 throw $exception;
Chris@0 317 } else {
Chris@0 318 $this->exceptions[] = $exception;
Chris@0 319 return;
Chris@0 320 }
Chris@0 321 }
Chris@0 322
Chris@0 323 if (empty($image['link']) || !is_string($image['link'])
Chris@0 324 || !Uri::factory($image['link'])->isValid()
Chris@0 325 ) {
Chris@0 326 $message = 'Invalid parameter: parameter \'link\''
Chris@0 327 . ' must be a non-empty string and valid URI/IRI';
Chris@0 328 $exception = new Writer\Exception\InvalidArgumentException($message);
Chris@0 329 if (!$this->ignoreExceptions) {
Chris@0 330 throw $exception;
Chris@0 331 } else {
Chris@0 332 $this->exceptions[] = $exception;
Chris@0 333 return;
Chris@0 334 }
Chris@0 335 }
Chris@0 336
Chris@0 337 $img = $dom->createElement('image');
Chris@0 338 $root->appendChild($img);
Chris@0 339
Chris@0 340 $url = $dom->createElement('url');
Chris@0 341 $text = $dom->createTextNode($image['uri']);
Chris@0 342 $url->appendChild($text);
Chris@0 343
Chris@0 344 $title = $dom->createElement('title');
Chris@0 345 $text = $dom->createTextNode($image['title']);
Chris@0 346 $title->appendChild($text);
Chris@0 347
Chris@0 348 $link = $dom->createElement('link');
Chris@0 349 $text = $dom->createTextNode($image['link']);
Chris@0 350 $link->appendChild($text);
Chris@0 351
Chris@0 352 $img->appendChild($url);
Chris@0 353 $img->appendChild($title);
Chris@0 354 $img->appendChild($link);
Chris@0 355
Chris@0 356 if (isset($image['height'])) {
Chris@0 357 if (!ctype_digit((string) $image['height']) || $image['height'] > 400) {
Chris@0 358 $message = 'Invalid parameter: parameter \'height\''
Chris@0 359 . ' must be an integer not exceeding 400';
Chris@0 360 $exception = new Writer\Exception\InvalidArgumentException($message);
Chris@0 361 if (!$this->ignoreExceptions) {
Chris@0 362 throw $exception;
Chris@0 363 } else {
Chris@0 364 $this->exceptions[] = $exception;
Chris@0 365 return;
Chris@0 366 }
Chris@0 367 }
Chris@0 368 $height = $dom->createElement('height');
Chris@0 369 $text = $dom->createTextNode($image['height']);
Chris@0 370 $height->appendChild($text);
Chris@0 371 $img->appendChild($height);
Chris@0 372 }
Chris@0 373 if (isset($image['width'])) {
Chris@0 374 if (!ctype_digit((string) $image['width']) || $image['width'] > 144) {
Chris@0 375 $message = 'Invalid parameter: parameter \'width\''
Chris@0 376 . ' must be an integer not exceeding 144';
Chris@0 377 $exception = new Writer\Exception\InvalidArgumentException($message);
Chris@0 378 if (!$this->ignoreExceptions) {
Chris@0 379 throw $exception;
Chris@0 380 } else {
Chris@0 381 $this->exceptions[] = $exception;
Chris@0 382 return;
Chris@0 383 }
Chris@0 384 }
Chris@0 385 $width = $dom->createElement('width');
Chris@0 386 $text = $dom->createTextNode($image['width']);
Chris@0 387 $width->appendChild($text);
Chris@0 388 $img->appendChild($width);
Chris@0 389 }
Chris@0 390 if (isset($image['description'])) {
Chris@0 391 if (empty($image['description']) || !is_string($image['description'])) {
Chris@0 392 $message = 'Invalid parameter: parameter \'description\''
Chris@0 393 . ' must be a non-empty string';
Chris@0 394 $exception = new Writer\Exception\InvalidArgumentException($message);
Chris@0 395 if (!$this->ignoreExceptions) {
Chris@0 396 throw $exception;
Chris@0 397 } else {
Chris@0 398 $this->exceptions[] = $exception;
Chris@0 399 return;
Chris@0 400 }
Chris@0 401 }
Chris@0 402 $desc = $dom->createElement('description');
Chris@0 403 $text = $dom->createTextNode($image['description']);
Chris@0 404 $desc->appendChild($text);
Chris@0 405 $img->appendChild($desc);
Chris@0 406 }
Chris@0 407 }
Chris@0 408
Chris@0 409 /**
Chris@0 410 * Set date feed was created
Chris@0 411 *
Chris@0 412 * @param DOMDocument $dom
Chris@0 413 * @param DOMElement $root
Chris@0 414 * @return void
Chris@0 415 */
Chris@0 416 protected function _setDateCreated(DOMDocument $dom, DOMElement $root)
Chris@0 417 {
Chris@0 418 if (!$this->getDataContainer()->getDateCreated()) {
Chris@0 419 return;
Chris@0 420 }
Chris@0 421 if (!$this->getDataContainer()->getDateModified()) {
Chris@0 422 $this->getDataContainer()->setDateModified(
Chris@0 423 $this->getDataContainer()->getDateCreated()
Chris@0 424 );
Chris@0 425 }
Chris@0 426 }
Chris@0 427
Chris@0 428 /**
Chris@0 429 * Set date feed last build date
Chris@0 430 *
Chris@0 431 * @param DOMDocument $dom
Chris@0 432 * @param DOMElement $root
Chris@0 433 * @return void
Chris@0 434 */
Chris@0 435 protected function _setLastBuildDate(DOMDocument $dom, DOMElement $root)
Chris@0 436 {
Chris@0 437 if (!$this->getDataContainer()->getLastBuildDate()) {
Chris@0 438 return;
Chris@0 439 }
Chris@0 440
Chris@0 441 $lastBuildDate = $dom->createElement('lastBuildDate');
Chris@0 442 $root->appendChild($lastBuildDate);
Chris@0 443 $text = $dom->createTextNode(
Chris@0 444 $this->getDataContainer()->getLastBuildDate()->format(DateTime::RSS)
Chris@0 445 );
Chris@0 446 $lastBuildDate->appendChild($text);
Chris@0 447 }
Chris@0 448
Chris@0 449 /**
Chris@0 450 * Set base URL to feed links
Chris@0 451 *
Chris@0 452 * @param DOMDocument $dom
Chris@0 453 * @param DOMElement $root
Chris@0 454 * @return void
Chris@0 455 */
Chris@0 456 protected function _setBaseUrl(DOMDocument $dom, DOMElement $root)
Chris@0 457 {
Chris@0 458 $baseUrl = $this->getDataContainer()->getBaseUrl();
Chris@0 459 if (!$baseUrl) {
Chris@0 460 return;
Chris@0 461 }
Chris@0 462 $root->setAttribute('xml:base', $baseUrl);
Chris@0 463 }
Chris@0 464
Chris@0 465 /**
Chris@0 466 * Set feed categories
Chris@0 467 *
Chris@0 468 * @param DOMDocument $dom
Chris@0 469 * @param DOMElement $root
Chris@0 470 * @return void
Chris@0 471 */
Chris@0 472 protected function _setCategories(DOMDocument $dom, DOMElement $root)
Chris@0 473 {
Chris@0 474 $categories = $this->getDataContainer()->getCategories();
Chris@0 475 if (!$categories) {
Chris@0 476 return;
Chris@0 477 }
Chris@0 478 foreach ($categories as $cat) {
Chris@0 479 $category = $dom->createElement('category');
Chris@0 480 if (isset($cat['scheme'])) {
Chris@0 481 $category->setAttribute('domain', $cat['scheme']);
Chris@0 482 }
Chris@0 483 $text = $dom->createTextNode($cat['term']);
Chris@0 484 $category->appendChild($text);
Chris@0 485 $root->appendChild($category);
Chris@0 486 }
Chris@0 487 }
Chris@0 488 }