annotate vendor/zendframework/zend-feed/src/Writer/Renderer/Entry/Rss.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
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\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@0 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@0 74 protected function _setTitle(DOMDocument $dom, DOMElement $root)
Chris@0 75 {
Chris@0 76 if (!$this->getDataContainer()->getDescription()
Chris@0 77 && !$this->getDataContainer()->getTitle()) {
Chris@0 78 $message = 'RSS 2.0 entry elements SHOULD contain exactly one'
Chris@0 79 . ' title element but a title has not been set. In addition, there'
Chris@0 80 . ' is no description as required in the absence of a title.';
Chris@0 81 $exception = new Writer\Exception\InvalidArgumentException($message);
Chris@0 82 if (!$this->ignoreExceptions) {
Chris@0 83 throw $exception;
Chris@0 84 } else {
Chris@0 85 $this->exceptions[] = $exception;
Chris@0 86 return;
Chris@0 87 }
Chris@0 88 }
Chris@0 89 $title = $dom->createElement('title');
Chris@0 90 $root->appendChild($title);
Chris@0 91 $text = $dom->createTextNode($this->getDataContainer()->getTitle());
Chris@0 92 $title->appendChild($text);
Chris@0 93 }
Chris@0 94
Chris@0 95 /**
Chris@0 96 * Set entry description
Chris@0 97 *
Chris@0 98 * @param DOMDocument $dom
Chris@0 99 * @param DOMElement $root
Chris@0 100 * @return void
Chris@0 101 * @throws Writer\Exception\InvalidArgumentException
Chris@0 102 */
Chris@0 103 protected function _setDescription(DOMDocument $dom, DOMElement $root)
Chris@0 104 {
Chris@0 105 if (!$this->getDataContainer()->getDescription()
Chris@0 106 && !$this->getDataContainer()->getTitle()) {
Chris@0 107 $message = 'RSS 2.0 entry elements SHOULD contain exactly one'
Chris@0 108 . ' description element but a description has not been set. In'
Chris@0 109 . ' addition, there is no title element as required in the absence'
Chris@0 110 . ' of a description.';
Chris@0 111 $exception = new Writer\Exception\InvalidArgumentException($message);
Chris@0 112 if (!$this->ignoreExceptions) {
Chris@0 113 throw $exception;
Chris@0 114 } else {
Chris@0 115 $this->exceptions[] = $exception;
Chris@0 116 return;
Chris@0 117 }
Chris@0 118 }
Chris@0 119 if (!$this->getDataContainer()->getDescription()) {
Chris@0 120 return;
Chris@0 121 }
Chris@0 122 $subtitle = $dom->createElement('description');
Chris@0 123 $root->appendChild($subtitle);
Chris@0 124 $text = $dom->createCDATASection($this->getDataContainer()->getDescription());
Chris@0 125 $subtitle->appendChild($text);
Chris@0 126 }
Chris@0 127
Chris@0 128 /**
Chris@0 129 * Set date entry was last modified
Chris@0 130 *
Chris@0 131 * @param DOMDocument $dom
Chris@0 132 * @param DOMElement $root
Chris@0 133 * @return void
Chris@0 134 */
Chris@0 135 protected function _setDateModified(DOMDocument $dom, DOMElement $root)
Chris@0 136 {
Chris@0 137 if (!$this->getDataContainer()->getDateModified()) {
Chris@0 138 return;
Chris@0 139 }
Chris@0 140
Chris@0 141 $updated = $dom->createElement('pubDate');
Chris@0 142 $root->appendChild($updated);
Chris@0 143 $text = $dom->createTextNode(
Chris@0 144 $this->getDataContainer()->getDateModified()->format(DateTime::RSS)
Chris@0 145 );
Chris@0 146 $updated->appendChild($text);
Chris@0 147 }
Chris@0 148
Chris@0 149 /**
Chris@0 150 * Set date entry was created
Chris@0 151 *
Chris@0 152 * @param DOMDocument $dom
Chris@0 153 * @param DOMElement $root
Chris@0 154 * @return void
Chris@0 155 */
Chris@0 156 protected function _setDateCreated(DOMDocument $dom, DOMElement $root)
Chris@0 157 {
Chris@0 158 if (!$this->getDataContainer()->getDateCreated()) {
Chris@0 159 return;
Chris@0 160 }
Chris@0 161 if (!$this->getDataContainer()->getDateModified()) {
Chris@0 162 $this->getDataContainer()->setDateModified(
Chris@0 163 $this->getDataContainer()->getDateCreated()
Chris@0 164 );
Chris@0 165 }
Chris@0 166 }
Chris@0 167
Chris@0 168 /**
Chris@0 169 * Set entry authors
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 _setAuthors(DOMDocument $dom, DOMElement $root)
Chris@0 176 {
Chris@0 177 $authors = $this->container->getAuthors();
Chris@0 178 if ((!$authors || empty($authors))) {
Chris@0 179 return;
Chris@0 180 }
Chris@0 181 foreach ($authors as $data) {
Chris@0 182 $author = $this->dom->createElement('author');
Chris@0 183 $name = $data['name'];
Chris@0 184 if (array_key_exists('email', $data)) {
Chris@0 185 $name = $data['email'] . ' (' . $data['name'] . ')';
Chris@0 186 }
Chris@0 187 $text = $dom->createTextNode($name);
Chris@0 188 $author->appendChild($text);
Chris@0 189 $root->appendChild($author);
Chris@0 190 }
Chris@0 191 }
Chris@0 192
Chris@0 193 /**
Chris@0 194 * Set entry enclosure
Chris@0 195 *
Chris@0 196 * @param DOMDocument $dom
Chris@0 197 * @param DOMElement $root
Chris@0 198 * @return void
Chris@0 199 * @throws Writer\Exception\InvalidArgumentException
Chris@0 200 */
Chris@0 201 protected function _setEnclosure(DOMDocument $dom, DOMElement $root)
Chris@0 202 {
Chris@0 203 $data = $this->container->getEnclosure();
Chris@0 204 if ((!$data || empty($data))) {
Chris@0 205 return;
Chris@0 206 }
Chris@0 207 if (!isset($data['type'])) {
Chris@0 208 $exception = new Writer\Exception\InvalidArgumentException('Enclosure "type" is not set');
Chris@0 209 if (!$this->ignoreExceptions) {
Chris@0 210 throw $exception;
Chris@0 211 } else {
Chris@0 212 $this->exceptions[] = $exception;
Chris@0 213 return;
Chris@0 214 }
Chris@0 215 }
Chris@0 216 if (!isset($data['length'])) {
Chris@0 217 $exception = new Writer\Exception\InvalidArgumentException('Enclosure "length" is not set');
Chris@0 218 if (!$this->ignoreExceptions) {
Chris@0 219 throw $exception;
Chris@0 220 } else {
Chris@0 221 $this->exceptions[] = $exception;
Chris@0 222 return;
Chris@0 223 }
Chris@0 224 }
Chris@0 225 if ((int) $data['length'] < 0 || !ctype_digit((string) $data['length'])) {
Chris@0 226 $exception = new Writer\Exception\InvalidArgumentException('Enclosure "length" must be an integer'
Chris@0 227 . ' indicating the content\'s length in bytes');
Chris@0 228 if (!$this->ignoreExceptions) {
Chris@0 229 throw $exception;
Chris@0 230 } else {
Chris@0 231 $this->exceptions[] = $exception;
Chris@0 232 return;
Chris@0 233 }
Chris@0 234 }
Chris@0 235 $enclosure = $this->dom->createElement('enclosure');
Chris@0 236 $enclosure->setAttribute('type', $data['type']);
Chris@0 237 $enclosure->setAttribute('length', $data['length']);
Chris@0 238 $enclosure->setAttribute('url', $data['uri']);
Chris@0 239 $root->appendChild($enclosure);
Chris@0 240 }
Chris@0 241
Chris@0 242 /**
Chris@0 243 * Set link to entry
Chris@0 244 *
Chris@0 245 * @param DOMDocument $dom
Chris@0 246 * @param DOMElement $root
Chris@0 247 * @return void
Chris@0 248 */
Chris@0 249 protected function _setLink(DOMDocument $dom, DOMElement $root)
Chris@0 250 {
Chris@0 251 if (!$this->getDataContainer()->getLink()) {
Chris@0 252 return;
Chris@0 253 }
Chris@0 254 $link = $dom->createElement('link');
Chris@0 255 $root->appendChild($link);
Chris@0 256 $text = $dom->createTextNode($this->getDataContainer()->getLink());
Chris@0 257 $link->appendChild($text);
Chris@0 258 }
Chris@0 259
Chris@0 260 /**
Chris@0 261 * Set entry identifier
Chris@0 262 *
Chris@0 263 * @param DOMDocument $dom
Chris@0 264 * @param DOMElement $root
Chris@0 265 * @return void
Chris@0 266 */
Chris@0 267 protected function _setId(DOMDocument $dom, DOMElement $root)
Chris@0 268 {
Chris@0 269 if (!$this->getDataContainer()->getId()
Chris@0 270 && !$this->getDataContainer()->getLink()) {
Chris@0 271 return;
Chris@0 272 }
Chris@0 273
Chris@0 274 $id = $dom->createElement('guid');
Chris@0 275 $root->appendChild($id);
Chris@0 276 if (!$this->getDataContainer()->getId()) {
Chris@0 277 $this->getDataContainer()->setId(
Chris@0 278 $this->getDataContainer()->getLink());
Chris@0 279 }
Chris@0 280 $text = $dom->createTextNode($this->getDataContainer()->getId());
Chris@0 281 $id->appendChild($text);
Chris@0 282 if (!Uri::factory($this->getDataContainer()->getId())->isValid()) {
Chris@0 283 $id->setAttribute('isPermaLink', 'false');
Chris@0 284 }
Chris@0 285 }
Chris@0 286
Chris@0 287 /**
Chris@0 288 * Set link to entry comments
Chris@0 289 *
Chris@0 290 * @param DOMDocument $dom
Chris@0 291 * @param DOMElement $root
Chris@0 292 * @return void
Chris@0 293 */
Chris@0 294 protected function _setCommentLink(DOMDocument $dom, DOMElement $root)
Chris@0 295 {
Chris@0 296 $link = $this->getDataContainer()->getCommentLink();
Chris@0 297 if (!$link) {
Chris@0 298 return;
Chris@0 299 }
Chris@0 300 $clink = $this->dom->createElement('comments');
Chris@0 301 $text = $dom->createTextNode($link);
Chris@0 302 $clink->appendChild($text);
Chris@0 303 $root->appendChild($clink);
Chris@0 304 }
Chris@0 305
Chris@0 306 /**
Chris@0 307 * Set entry categories
Chris@0 308 *
Chris@0 309 * @param DOMDocument $dom
Chris@0 310 * @param DOMElement $root
Chris@0 311 * @return void
Chris@0 312 */
Chris@0 313 protected function _setCategories(DOMDocument $dom, DOMElement $root)
Chris@0 314 {
Chris@0 315 $categories = $this->getDataContainer()->getCategories();
Chris@0 316 if (!$categories) {
Chris@0 317 return;
Chris@0 318 }
Chris@0 319 foreach ($categories as $cat) {
Chris@0 320 $category = $dom->createElement('category');
Chris@0 321 if (isset($cat['scheme'])) {
Chris@0 322 $category->setAttribute('domain', $cat['scheme']);
Chris@0 323 }
Chris@0 324 $text = $dom->createCDATASection($cat['term']);
Chris@0 325 $category->appendChild($text);
Chris@0 326 $root->appendChild($category);
Chris@0 327 }
Chris@0 328 }
Chris@0 329 }