annotate vendor/zendframework/zend-feed/src/Writer/Renderer/Entry/Rss.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents af1871eacc83
children
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\Entry;
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
Chris@0 19 /**
Chris@0 20 */
Chris@0 21 class Rss extends Renderer\AbstractRenderer implements Renderer\RendererInterface
Chris@0 22 {
Chris@0 23 /**
Chris@0 24 * Constructor
Chris@0 25 *
Chris@0 26 * @param Writer\Entry $container
Chris@0 27 */
Chris@0 28 public function __construct(Writer\Entry $container)
Chris@0 29 {
Chris@0 30 parent::__construct($container);
Chris@0 31 }
Chris@0 32
Chris@0 33 /**
Chris@0 34 * Render RSS entry
Chris@0 35 *
Chris@0 36 * @return Rss
Chris@0 37 */
Chris@0 38 public function render()
Chris@0 39 {
Chris@0 40 $this->dom = new DOMDocument('1.0', $this->container->getEncoding());
Chris@0 41 $this->dom->formatOutput = true;
Chris@0 42 $this->dom->substituteEntities = false;
Chris@0 43 $entry = $this->dom->createElement('item');
Chris@0 44 $this->dom->appendChild($entry);
Chris@0 45
Chris@0 46 $this->_setTitle($this->dom, $entry);
Chris@0 47 $this->_setDescription($this->dom, $entry);
Chris@0 48 $this->_setDateCreated($this->dom, $entry);
Chris@0 49 $this->_setDateModified($this->dom, $entry);
Chris@0 50 $this->_setLink($this->dom, $entry);
Chris@0 51 $this->_setId($this->dom, $entry);
Chris@0 52 $this->_setAuthors($this->dom, $entry);
Chris@0 53 $this->_setEnclosure($this->dom, $entry);
Chris@0 54 $this->_setCommentLink($this->dom, $entry);
Chris@0 55 $this->_setCategories($this->dom, $entry);
Chris@0 56 foreach ($this->extensions as $ext) {
Chris@0 57 $ext->setType($this->getType());
Chris@0 58 $ext->setRootElement($this->getRootElement());
Chris@12 59 $ext->setDomDocument($this->getDomDocument(), $entry);
Chris@0 60 $ext->render();
Chris@0 61 }
Chris@0 62
Chris@0 63 return $this;
Chris@0 64 }
Chris@0 65
Chris@0 66 /**
Chris@0 67 * Set entry title
Chris@0 68 *
Chris@0 69 * @param DOMDocument $dom
Chris@0 70 * @param DOMElement $root
Chris@0 71 * @return void
Chris@0 72 * @throws Writer\Exception\InvalidArgumentException
Chris@0 73 */
Chris@12 74 // @codingStandardsIgnoreStart
Chris@0 75 protected function _setTitle(DOMDocument $dom, DOMElement $root)
Chris@0 76 {
Chris@12 77 // @codingStandardsIgnoreEnd
Chris@12 78 if (! $this->getDataContainer()->getDescription()
Chris@12 79 && ! $this->getDataContainer()->getTitle()) {
Chris@0 80 $message = 'RSS 2.0 entry elements SHOULD contain exactly one'
Chris@0 81 . ' title element but a title has not been set. In addition, there'
Chris@0 82 . ' is no description as required in the absence of a title.';
Chris@0 83 $exception = new Writer\Exception\InvalidArgumentException($message);
Chris@12 84 if (! $this->ignoreExceptions) {
Chris@0 85 throw $exception;
Chris@0 86 } else {
Chris@0 87 $this->exceptions[] = $exception;
Chris@0 88 return;
Chris@0 89 }
Chris@0 90 }
Chris@0 91 $title = $dom->createElement('title');
Chris@0 92 $root->appendChild($title);
Chris@0 93 $text = $dom->createTextNode($this->getDataContainer()->getTitle());
Chris@0 94 $title->appendChild($text);
Chris@0 95 }
Chris@0 96
Chris@0 97 /**
Chris@0 98 * Set entry description
Chris@0 99 *
Chris@0 100 * @param DOMDocument $dom
Chris@0 101 * @param DOMElement $root
Chris@0 102 * @return void
Chris@0 103 * @throws Writer\Exception\InvalidArgumentException
Chris@0 104 */
Chris@12 105 // @codingStandardsIgnoreStart
Chris@0 106 protected function _setDescription(DOMDocument $dom, DOMElement $root)
Chris@0 107 {
Chris@12 108 // @codingStandardsIgnoreEnd
Chris@12 109 if (! $this->getDataContainer()->getDescription()
Chris@12 110 && ! $this->getDataContainer()->getTitle()) {
Chris@0 111 $message = 'RSS 2.0 entry elements SHOULD contain exactly one'
Chris@0 112 . ' description element but a description has not been set. In'
Chris@0 113 . ' addition, there is no title element as required in the absence'
Chris@0 114 . ' of a description.';
Chris@0 115 $exception = new Writer\Exception\InvalidArgumentException($message);
Chris@12 116 if (! $this->ignoreExceptions) {
Chris@0 117 throw $exception;
Chris@0 118 } else {
Chris@0 119 $this->exceptions[] = $exception;
Chris@0 120 return;
Chris@0 121 }
Chris@0 122 }
Chris@12 123 if (! $this->getDataContainer()->getDescription()) {
Chris@0 124 return;
Chris@0 125 }
Chris@0 126 $subtitle = $dom->createElement('description');
Chris@0 127 $root->appendChild($subtitle);
Chris@0 128 $text = $dom->createCDATASection($this->getDataContainer()->getDescription());
Chris@0 129 $subtitle->appendChild($text);
Chris@0 130 }
Chris@0 131
Chris@0 132 /**
Chris@0 133 * Set date entry was last modified
Chris@0 134 *
Chris@0 135 * @param DOMDocument $dom
Chris@0 136 * @param DOMElement $root
Chris@0 137 * @return void
Chris@0 138 */
Chris@12 139 // @codingStandardsIgnoreStart
Chris@0 140 protected function _setDateModified(DOMDocument $dom, DOMElement $root)
Chris@0 141 {
Chris@12 142 // @codingStandardsIgnoreEnd
Chris@12 143 if (! $this->getDataContainer()->getDateModified()) {
Chris@0 144 return;
Chris@0 145 }
Chris@0 146
Chris@0 147 $updated = $dom->createElement('pubDate');
Chris@0 148 $root->appendChild($updated);
Chris@0 149 $text = $dom->createTextNode(
Chris@0 150 $this->getDataContainer()->getDateModified()->format(DateTime::RSS)
Chris@0 151 );
Chris@0 152 $updated->appendChild($text);
Chris@0 153 }
Chris@0 154
Chris@0 155 /**
Chris@0 156 * Set date entry was created
Chris@0 157 *
Chris@0 158 * @param DOMDocument $dom
Chris@0 159 * @param DOMElement $root
Chris@0 160 * @return void
Chris@0 161 */
Chris@12 162 // @codingStandardsIgnoreStart
Chris@0 163 protected function _setDateCreated(DOMDocument $dom, DOMElement $root)
Chris@0 164 {
Chris@12 165 // @codingStandardsIgnoreEnd
Chris@12 166 if (! $this->getDataContainer()->getDateCreated()) {
Chris@0 167 return;
Chris@0 168 }
Chris@12 169 if (! $this->getDataContainer()->getDateModified()) {
Chris@0 170 $this->getDataContainer()->setDateModified(
Chris@0 171 $this->getDataContainer()->getDateCreated()
Chris@0 172 );
Chris@0 173 }
Chris@0 174 }
Chris@0 175
Chris@0 176 /**
Chris@0 177 * Set entry authors
Chris@0 178 *
Chris@0 179 * @param DOMDocument $dom
Chris@0 180 * @param DOMElement $root
Chris@0 181 * @return void
Chris@0 182 */
Chris@12 183 // @codingStandardsIgnoreStart
Chris@0 184 protected function _setAuthors(DOMDocument $dom, DOMElement $root)
Chris@0 185 {
Chris@12 186 // @codingStandardsIgnoreEnd
Chris@0 187 $authors = $this->container->getAuthors();
Chris@12 188 if ((! $authors || empty($authors))) {
Chris@0 189 return;
Chris@0 190 }
Chris@0 191 foreach ($authors as $data) {
Chris@0 192 $author = $this->dom->createElement('author');
Chris@0 193 $name = $data['name'];
Chris@0 194 if (array_key_exists('email', $data)) {
Chris@0 195 $name = $data['email'] . ' (' . $data['name'] . ')';
Chris@0 196 }
Chris@0 197 $text = $dom->createTextNode($name);
Chris@0 198 $author->appendChild($text);
Chris@0 199 $root->appendChild($author);
Chris@0 200 }
Chris@0 201 }
Chris@0 202
Chris@0 203 /**
Chris@0 204 * Set entry enclosure
Chris@0 205 *
Chris@0 206 * @param DOMDocument $dom
Chris@0 207 * @param DOMElement $root
Chris@0 208 * @return void
Chris@0 209 * @throws Writer\Exception\InvalidArgumentException
Chris@0 210 */
Chris@12 211 // @codingStandardsIgnoreStart
Chris@0 212 protected function _setEnclosure(DOMDocument $dom, DOMElement $root)
Chris@0 213 {
Chris@12 214 // @codingStandardsIgnoreEnd
Chris@0 215 $data = $this->container->getEnclosure();
Chris@12 216 if ((! $data || empty($data))) {
Chris@0 217 return;
Chris@0 218 }
Chris@12 219 if (! isset($data['type'])) {
Chris@0 220 $exception = new Writer\Exception\InvalidArgumentException('Enclosure "type" is not set');
Chris@12 221 if (! $this->ignoreExceptions) {
Chris@0 222 throw $exception;
Chris@0 223 } else {
Chris@0 224 $this->exceptions[] = $exception;
Chris@0 225 return;
Chris@0 226 }
Chris@0 227 }
Chris@12 228 if (! isset($data['length'])) {
Chris@0 229 $exception = new Writer\Exception\InvalidArgumentException('Enclosure "length" is not set');
Chris@12 230 if (! $this->ignoreExceptions) {
Chris@0 231 throw $exception;
Chris@0 232 } else {
Chris@0 233 $this->exceptions[] = $exception;
Chris@0 234 return;
Chris@0 235 }
Chris@0 236 }
Chris@12 237 if ((int) $data['length'] < 0 || ! ctype_digit((string) $data['length'])) {
Chris@0 238 $exception = new Writer\Exception\InvalidArgumentException('Enclosure "length" must be an integer'
Chris@0 239 . ' indicating the content\'s length in bytes');
Chris@12 240 if (! $this->ignoreExceptions) {
Chris@0 241 throw $exception;
Chris@0 242 } else {
Chris@0 243 $this->exceptions[] = $exception;
Chris@0 244 return;
Chris@0 245 }
Chris@0 246 }
Chris@0 247 $enclosure = $this->dom->createElement('enclosure');
Chris@0 248 $enclosure->setAttribute('type', $data['type']);
Chris@0 249 $enclosure->setAttribute('length', $data['length']);
Chris@0 250 $enclosure->setAttribute('url', $data['uri']);
Chris@0 251 $root->appendChild($enclosure);
Chris@0 252 }
Chris@0 253
Chris@0 254 /**
Chris@0 255 * Set link to entry
Chris@0 256 *
Chris@0 257 * @param DOMDocument $dom
Chris@0 258 * @param DOMElement $root
Chris@0 259 * @return void
Chris@0 260 */
Chris@12 261 // @codingStandardsIgnoreStart
Chris@0 262 protected function _setLink(DOMDocument $dom, DOMElement $root)
Chris@0 263 {
Chris@12 264 // @codingStandardsIgnoreEnd
Chris@12 265 if (! $this->getDataContainer()->getLink()) {
Chris@0 266 return;
Chris@0 267 }
Chris@0 268 $link = $dom->createElement('link');
Chris@0 269 $root->appendChild($link);
Chris@0 270 $text = $dom->createTextNode($this->getDataContainer()->getLink());
Chris@0 271 $link->appendChild($text);
Chris@0 272 }
Chris@0 273
Chris@0 274 /**
Chris@0 275 * Set entry identifier
Chris@0 276 *
Chris@0 277 * @param DOMDocument $dom
Chris@0 278 * @param DOMElement $root
Chris@0 279 * @return void
Chris@0 280 */
Chris@12 281 // @codingStandardsIgnoreStart
Chris@0 282 protected function _setId(DOMDocument $dom, DOMElement $root)
Chris@0 283 {
Chris@12 284 // @codingStandardsIgnoreEnd
Chris@12 285 if (! $this->getDataContainer()->getId()
Chris@12 286 && ! $this->getDataContainer()->getLink()) {
Chris@0 287 return;
Chris@0 288 }
Chris@0 289
Chris@0 290 $id = $dom->createElement('guid');
Chris@0 291 $root->appendChild($id);
Chris@12 292 if (! $this->getDataContainer()->getId()) {
Chris@0 293 $this->getDataContainer()->setId(
Chris@12 294 $this->getDataContainer()->getLink()
Chris@12 295 );
Chris@0 296 }
Chris@0 297 $text = $dom->createTextNode($this->getDataContainer()->getId());
Chris@0 298 $id->appendChild($text);
Chris@18 299
Chris@18 300 $uri = Uri::factory($this->getDataContainer()->getId());
Chris@18 301 if (! $uri->isValid() || ! $uri->isAbsolute()) {
Chris@18 302 /** @see http://www.rssboard.org/rss-profile#element-channel-item-guid */
Chris@0 303 $id->setAttribute('isPermaLink', 'false');
Chris@0 304 }
Chris@0 305 }
Chris@0 306
Chris@0 307 /**
Chris@0 308 * Set link to entry comments
Chris@0 309 *
Chris@0 310 * @param DOMDocument $dom
Chris@0 311 * @param DOMElement $root
Chris@0 312 * @return void
Chris@0 313 */
Chris@12 314 // @codingStandardsIgnoreStart
Chris@0 315 protected function _setCommentLink(DOMDocument $dom, DOMElement $root)
Chris@0 316 {
Chris@12 317 // @codingStandardsIgnoreEnd
Chris@0 318 $link = $this->getDataContainer()->getCommentLink();
Chris@12 319 if (! $link) {
Chris@0 320 return;
Chris@0 321 }
Chris@0 322 $clink = $this->dom->createElement('comments');
Chris@0 323 $text = $dom->createTextNode($link);
Chris@0 324 $clink->appendChild($text);
Chris@0 325 $root->appendChild($clink);
Chris@0 326 }
Chris@0 327
Chris@0 328 /**
Chris@0 329 * Set entry categories
Chris@0 330 *
Chris@0 331 * @param DOMDocument $dom
Chris@0 332 * @param DOMElement $root
Chris@0 333 * @return void
Chris@0 334 */
Chris@12 335 // @codingStandardsIgnoreStart
Chris@0 336 protected function _setCategories(DOMDocument $dom, DOMElement $root)
Chris@0 337 {
Chris@12 338 // @codingStandardsIgnoreEnd
Chris@0 339 $categories = $this->getDataContainer()->getCategories();
Chris@12 340 if (! $categories) {
Chris@0 341 return;
Chris@0 342 }
Chris@0 343 foreach ($categories as $cat) {
Chris@0 344 $category = $dom->createElement('category');
Chris@0 345 if (isset($cat['scheme'])) {
Chris@0 346 $category->setAttribute('domain', $cat['scheme']);
Chris@0 347 }
Chris@0 348 $text = $dom->createCDATASection($cat['term']);
Chris@0 349 $category->appendChild($text);
Chris@0 350 $root->appendChild($category);
Chris@0 351 }
Chris@0 352 }
Chris@0 353 }