annotate vendor/zendframework/zend-feed/src/Writer/Renderer/Feed/AbstractAtom.php @ 0:c75dbcec494b

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