annotate vendor/zendframework/zend-feed/src/Reader/Extension/Atom/Feed.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents 129ea1e6d783
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\Reader\Extension\Atom;
Chris@0 11
Chris@0 12 use DateTime;
Chris@0 13 use DOMElement;
Chris@0 14 use Zend\Feed\Reader;
Chris@0 15 use Zend\Feed\Reader\Collection;
Chris@0 16 use Zend\Feed\Reader\Extension;
Chris@0 17 use Zend\Feed\Uri;
Chris@0 18
Chris@0 19 class Feed extends Extension\AbstractFeed
Chris@0 20 {
Chris@0 21 /**
Chris@0 22 * Get a single author
Chris@0 23 *
Chris@0 24 * @param int $index
Chris@0 25 * @return string|null
Chris@0 26 */
Chris@0 27 public function getAuthor($index = 0)
Chris@0 28 {
Chris@0 29 $authors = $this->getAuthors();
Chris@0 30
Chris@0 31 if (isset($authors[$index])) {
Chris@0 32 return $authors[$index];
Chris@0 33 }
Chris@0 34
Chris@0 35 return;
Chris@0 36 }
Chris@0 37
Chris@0 38 /**
Chris@0 39 * Get an array with feed authors
Chris@0 40 *
Chris@0 41 * @return Collection\Author
Chris@0 42 */
Chris@0 43 public function getAuthors()
Chris@0 44 {
Chris@0 45 if (array_key_exists('authors', $this->data)) {
Chris@0 46 return $this->data['authors'];
Chris@0 47 }
Chris@0 48
Chris@0 49 $list = $this->xpath->query('//atom:author');
Chris@0 50
Chris@0 51 $authors = [];
Chris@0 52
Chris@0 53 if ($list->length) {
Chris@0 54 foreach ($list as $author) {
Chris@0 55 $author = $this->getAuthorFromElement($author);
Chris@12 56 if (! empty($author)) {
Chris@0 57 $authors[] = $author;
Chris@0 58 }
Chris@0 59 }
Chris@0 60 }
Chris@0 61
Chris@0 62 if (count($authors) == 0) {
Chris@0 63 $authors = new Collection\Author();
Chris@0 64 } else {
Chris@0 65 $authors = new Collection\Author(
Chris@0 66 Reader\Reader::arrayUnique($authors)
Chris@0 67 );
Chris@0 68 }
Chris@0 69
Chris@0 70 $this->data['authors'] = $authors;
Chris@0 71
Chris@0 72 return $this->data['authors'];
Chris@0 73 }
Chris@0 74
Chris@0 75 /**
Chris@0 76 * Get the copyright entry
Chris@0 77 *
Chris@0 78 * @return string|null
Chris@0 79 */
Chris@0 80 public function getCopyright()
Chris@0 81 {
Chris@0 82 if (array_key_exists('copyright', $this->data)) {
Chris@0 83 return $this->data['copyright'];
Chris@0 84 }
Chris@0 85
Chris@0 86 $copyright = null;
Chris@0 87
Chris@0 88 if ($this->getType() === Reader\Reader::TYPE_ATOM_03) {
Chris@0 89 $copyright = $this->xpath->evaluate('string(' . $this->getXpathPrefix() . '/atom:copyright)');
Chris@0 90 } else {
Chris@0 91 $copyright = $this->xpath->evaluate('string(' . $this->getXpathPrefix() . '/atom:rights)');
Chris@0 92 }
Chris@0 93
Chris@12 94 if (! $copyright) {
Chris@0 95 $copyright = null;
Chris@0 96 }
Chris@0 97
Chris@0 98 $this->data['copyright'] = $copyright;
Chris@0 99
Chris@0 100 return $this->data['copyright'];
Chris@0 101 }
Chris@0 102
Chris@0 103 /**
Chris@0 104 * Get the feed creation date
Chris@0 105 *
Chris@0 106 * @return DateTime|null
Chris@0 107 */
Chris@0 108 public function getDateCreated()
Chris@0 109 {
Chris@0 110 if (array_key_exists('datecreated', $this->data)) {
Chris@0 111 return $this->data['datecreated'];
Chris@0 112 }
Chris@0 113
Chris@0 114 $date = null;
Chris@0 115
Chris@0 116 if ($this->getType() === Reader\Reader::TYPE_ATOM_03) {
Chris@0 117 $dateCreated = $this->xpath->evaluate('string(' . $this->getXpathPrefix() . '/atom:created)');
Chris@0 118 } else {
Chris@0 119 $dateCreated = $this->xpath->evaluate('string(' . $this->getXpathPrefix() . '/atom:published)');
Chris@0 120 }
Chris@0 121
Chris@0 122 if ($dateCreated) {
Chris@0 123 $date = new DateTime($dateCreated);
Chris@0 124 }
Chris@0 125
Chris@0 126 $this->data['datecreated'] = $date;
Chris@0 127
Chris@0 128 return $this->data['datecreated'];
Chris@0 129 }
Chris@0 130
Chris@0 131 /**
Chris@0 132 * Get the feed modification date
Chris@0 133 *
Chris@0 134 * @return DateTime|null
Chris@0 135 */
Chris@0 136 public function getDateModified()
Chris@0 137 {
Chris@0 138 if (array_key_exists('datemodified', $this->data)) {
Chris@0 139 return $this->data['datemodified'];
Chris@0 140 }
Chris@0 141
Chris@0 142 $date = null;
Chris@0 143
Chris@0 144 if ($this->getType() === Reader\Reader::TYPE_ATOM_03) {
Chris@0 145 $dateModified = $this->xpath->evaluate('string(' . $this->getXpathPrefix() . '/atom:modified)');
Chris@0 146 } else {
Chris@0 147 $dateModified = $this->xpath->evaluate('string(' . $this->getXpathPrefix() . '/atom:updated)');
Chris@0 148 }
Chris@0 149
Chris@0 150 if ($dateModified) {
Chris@0 151 $date = new DateTime($dateModified);
Chris@0 152 }
Chris@0 153
Chris@0 154 $this->data['datemodified'] = $date;
Chris@0 155
Chris@0 156 return $this->data['datemodified'];
Chris@0 157 }
Chris@0 158
Chris@0 159 /**
Chris@0 160 * Get the feed description
Chris@0 161 *
Chris@0 162 * @return string|null
Chris@0 163 */
Chris@0 164 public function getDescription()
Chris@0 165 {
Chris@0 166 if (array_key_exists('description', $this->data)) {
Chris@0 167 return $this->data['description'];
Chris@0 168 }
Chris@0 169
Chris@0 170 $description = null;
Chris@0 171
Chris@0 172 if ($this->getType() === Reader\Reader::TYPE_ATOM_03) {
Chris@0 173 $description = $this->xpath->evaluate('string(' . $this->getXpathPrefix() . '/atom:tagline)');
Chris@0 174 } else {
Chris@0 175 $description = $this->xpath->evaluate('string(' . $this->getXpathPrefix() . '/atom:subtitle)');
Chris@0 176 }
Chris@0 177
Chris@12 178 if (! $description) {
Chris@0 179 $description = null;
Chris@0 180 }
Chris@0 181
Chris@0 182 $this->data['description'] = $description;
Chris@0 183
Chris@0 184 return $this->data['description'];
Chris@0 185 }
Chris@0 186
Chris@0 187 /**
Chris@0 188 * Get the feed generator entry
Chris@0 189 *
Chris@0 190 * @return string|null
Chris@0 191 */
Chris@0 192 public function getGenerator()
Chris@0 193 {
Chris@0 194 if (array_key_exists('generator', $this->data)) {
Chris@0 195 return $this->data['generator'];
Chris@0 196 }
Chris@0 197 // TODO: Add uri support
Chris@0 198 $generator = $this->xpath->evaluate('string(' . $this->getXpathPrefix() . '/atom:generator)');
Chris@0 199
Chris@12 200 if (! $generator) {
Chris@0 201 $generator = null;
Chris@0 202 }
Chris@0 203
Chris@0 204 $this->data['generator'] = $generator;
Chris@0 205
Chris@0 206 return $this->data['generator'];
Chris@0 207 }
Chris@0 208
Chris@0 209 /**
Chris@0 210 * Get the feed ID
Chris@0 211 *
Chris@0 212 * @return string|null
Chris@0 213 */
Chris@0 214 public function getId()
Chris@0 215 {
Chris@0 216 if (array_key_exists('id', $this->data)) {
Chris@0 217 return $this->data['id'];
Chris@0 218 }
Chris@0 219
Chris@0 220 $id = $this->xpath->evaluate('string(' . $this->getXpathPrefix() . '/atom:id)');
Chris@0 221
Chris@12 222 if (! $id) {
Chris@0 223 if ($this->getLink()) {
Chris@0 224 $id = $this->getLink();
Chris@0 225 } elseif ($this->getTitle()) {
Chris@0 226 $id = $this->getTitle();
Chris@0 227 } else {
Chris@0 228 $id = null;
Chris@0 229 }
Chris@0 230 }
Chris@0 231
Chris@0 232 $this->data['id'] = $id;
Chris@0 233
Chris@0 234 return $this->data['id'];
Chris@0 235 }
Chris@0 236
Chris@0 237 /**
Chris@0 238 * Get the feed language
Chris@0 239 *
Chris@0 240 * @return string|null
Chris@0 241 */
Chris@0 242 public function getLanguage()
Chris@0 243 {
Chris@0 244 if (array_key_exists('language', $this->data)) {
Chris@0 245 return $this->data['language'];
Chris@0 246 }
Chris@0 247
Chris@0 248 $language = $this->xpath->evaluate('string(' . $this->getXpathPrefix() . '/atom:lang)');
Chris@0 249
Chris@12 250 if (! $language) {
Chris@0 251 $language = $this->xpath->evaluate('string(//@xml:lang[1])');
Chris@0 252 }
Chris@0 253
Chris@12 254 if (! $language) {
Chris@0 255 $language = null;
Chris@0 256 }
Chris@0 257
Chris@0 258 $this->data['language'] = $language;
Chris@0 259
Chris@0 260 return $this->data['language'];
Chris@0 261 }
Chris@0 262
Chris@0 263 /**
Chris@0 264 * Get the feed image
Chris@0 265 *
Chris@0 266 * @return array|null
Chris@0 267 */
Chris@0 268 public function getImage()
Chris@0 269 {
Chris@0 270 if (array_key_exists('image', $this->data)) {
Chris@0 271 return $this->data['image'];
Chris@0 272 }
Chris@0 273
Chris@0 274 $imageUrl = $this->xpath->evaluate('string(' . $this->getXpathPrefix() . '/atom:logo)');
Chris@0 275
Chris@12 276 if (! $imageUrl) {
Chris@0 277 $image = null;
Chris@0 278 } else {
Chris@0 279 $image = ['uri' => $imageUrl];
Chris@0 280 }
Chris@0 281
Chris@0 282 $this->data['image'] = $image;
Chris@0 283
Chris@0 284 return $this->data['image'];
Chris@0 285 }
Chris@0 286
Chris@0 287 /**
Chris@0 288 * Get the base URI of the feed (if set).
Chris@0 289 *
Chris@0 290 * @return string|null
Chris@0 291 */
Chris@0 292 public function getBaseUrl()
Chris@0 293 {
Chris@0 294 if (array_key_exists('baseUrl', $this->data)) {
Chris@0 295 return $this->data['baseUrl'];
Chris@0 296 }
Chris@0 297
Chris@0 298 $baseUrl = $this->xpath->evaluate('string(//@xml:base[1])');
Chris@0 299
Chris@12 300 if (! $baseUrl) {
Chris@0 301 $baseUrl = null;
Chris@0 302 }
Chris@0 303 $this->data['baseUrl'] = $baseUrl;
Chris@0 304
Chris@0 305 return $this->data['baseUrl'];
Chris@0 306 }
Chris@0 307
Chris@0 308 /**
Chris@0 309 * Get a link to the source website
Chris@0 310 *
Chris@0 311 * @return string|null
Chris@0 312 */
Chris@0 313 public function getLink()
Chris@0 314 {
Chris@0 315 if (array_key_exists('link', $this->data)) {
Chris@0 316 return $this->data['link'];
Chris@0 317 }
Chris@0 318
Chris@0 319 $link = null;
Chris@0 320
Chris@0 321 $list = $this->xpath->query(
Chris@0 322 $this->getXpathPrefix() . '/atom:link[@rel="alternate"]/@href' . '|' .
Chris@0 323 $this->getXpathPrefix() . '/atom:link[not(@rel)]/@href'
Chris@0 324 );
Chris@0 325
Chris@0 326 if ($list->length) {
Chris@0 327 $link = $list->item(0)->nodeValue;
Chris@0 328 $link = $this->absolutiseUri($link);
Chris@0 329 }
Chris@0 330
Chris@0 331 $this->data['link'] = $link;
Chris@0 332
Chris@0 333 return $this->data['link'];
Chris@0 334 }
Chris@0 335
Chris@0 336 /**
Chris@0 337 * Get a link to the feed's XML Url
Chris@0 338 *
Chris@0 339 * @return string|null
Chris@0 340 */
Chris@0 341 public function getFeedLink()
Chris@0 342 {
Chris@0 343 if (array_key_exists('feedlink', $this->data)) {
Chris@0 344 return $this->data['feedlink'];
Chris@0 345 }
Chris@0 346
Chris@0 347 $link = $this->xpath->evaluate('string(' . $this->getXpathPrefix() . '/atom:link[@rel="self"]/@href)');
Chris@0 348
Chris@0 349 $link = $this->absolutiseUri($link);
Chris@0 350
Chris@0 351 $this->data['feedlink'] = $link;
Chris@0 352
Chris@0 353 return $this->data['feedlink'];
Chris@0 354 }
Chris@0 355
Chris@0 356 /**
Chris@0 357 * Get an array of any supported Pusubhubbub endpoints
Chris@0 358 *
Chris@0 359 * @return array|null
Chris@0 360 */
Chris@0 361 public function getHubs()
Chris@0 362 {
Chris@0 363 if (array_key_exists('hubs', $this->data)) {
Chris@0 364 return $this->data['hubs'];
Chris@0 365 }
Chris@0 366 $hubs = [];
Chris@0 367
Chris@0 368 $list = $this->xpath->query($this->getXpathPrefix()
Chris@0 369 . '//atom:link[@rel="hub"]/@href');
Chris@0 370
Chris@0 371 if ($list->length) {
Chris@0 372 foreach ($list as $uri) {
Chris@0 373 $hubs[] = $this->absolutiseUri($uri->nodeValue);
Chris@0 374 }
Chris@0 375 } else {
Chris@0 376 $hubs = null;
Chris@0 377 }
Chris@0 378
Chris@0 379 $this->data['hubs'] = $hubs;
Chris@0 380
Chris@0 381 return $this->data['hubs'];
Chris@0 382 }
Chris@0 383
Chris@0 384 /**
Chris@0 385 * Get the feed title
Chris@0 386 *
Chris@0 387 * @return string|null
Chris@0 388 */
Chris@0 389 public function getTitle()
Chris@0 390 {
Chris@0 391 if (array_key_exists('title', $this->data)) {
Chris@0 392 return $this->data['title'];
Chris@0 393 }
Chris@0 394
Chris@0 395 $title = $this->xpath->evaluate('string(' . $this->getXpathPrefix() . '/atom:title)');
Chris@0 396
Chris@12 397 if (! $title) {
Chris@0 398 $title = null;
Chris@0 399 }
Chris@0 400
Chris@0 401 $this->data['title'] = $title;
Chris@0 402
Chris@0 403 return $this->data['title'];
Chris@0 404 }
Chris@0 405
Chris@0 406 /**
Chris@0 407 * Get all categories
Chris@0 408 *
Chris@0 409 * @return Collection\Category
Chris@0 410 */
Chris@0 411 public function getCategories()
Chris@0 412 {
Chris@0 413 if (array_key_exists('categories', $this->data)) {
Chris@0 414 return $this->data['categories'];
Chris@0 415 }
Chris@0 416
Chris@0 417 if ($this->getType() == Reader\Reader::TYPE_ATOM_10) {
Chris@0 418 $list = $this->xpath->query($this->getXpathPrefix() . '/atom:category');
Chris@0 419 } else {
Chris@0 420 /**
Chris@0 421 * Since Atom 0.3 did not support categories, it would have used the
Chris@0 422 * Dublin Core extension. However there is a small possibility Atom 0.3
Chris@0 423 * may have been retrofittied to use Atom 1.0 instead.
Chris@0 424 */
Chris@0 425 $this->xpath->registerNamespace('atom10', Reader\Reader::NAMESPACE_ATOM_10);
Chris@0 426 $list = $this->xpath->query($this->getXpathPrefix() . '/atom10:category');
Chris@0 427 }
Chris@0 428
Chris@0 429 if ($list->length) {
Chris@0 430 $categoryCollection = new Collection\Category;
Chris@0 431 foreach ($list as $category) {
Chris@0 432 $categoryCollection[] = [
Chris@0 433 'term' => $category->getAttribute('term'),
Chris@0 434 'scheme' => $category->getAttribute('scheme'),
Chris@0 435 'label' => $category->getAttribute('label')
Chris@0 436 ];
Chris@0 437 }
Chris@0 438 } else {
Chris@0 439 return new Collection\Category;
Chris@0 440 }
Chris@0 441
Chris@0 442 $this->data['categories'] = $categoryCollection;
Chris@0 443
Chris@0 444 return $this->data['categories'];
Chris@0 445 }
Chris@0 446
Chris@0 447 /**
Chris@0 448 * Get an author entry in RSS format
Chris@0 449 *
Chris@0 450 * @param DOMElement $element
Chris@0 451 * @return string
Chris@0 452 */
Chris@0 453 protected function getAuthorFromElement(DOMElement $element)
Chris@0 454 {
Chris@0 455 $author = [];
Chris@0 456
Chris@0 457 $emailNode = $element->getElementsByTagName('email');
Chris@0 458 $nameNode = $element->getElementsByTagName('name');
Chris@0 459 $uriNode = $element->getElementsByTagName('uri');
Chris@0 460
Chris@0 461 if ($emailNode->length && strlen($emailNode->item(0)->nodeValue) > 0) {
Chris@0 462 $author['email'] = $emailNode->item(0)->nodeValue;
Chris@0 463 }
Chris@0 464
Chris@0 465 if ($nameNode->length && strlen($nameNode->item(0)->nodeValue) > 0) {
Chris@0 466 $author['name'] = $nameNode->item(0)->nodeValue;
Chris@0 467 }
Chris@0 468
Chris@0 469 if ($uriNode->length && strlen($uriNode->item(0)->nodeValue) > 0) {
Chris@0 470 $author['uri'] = $uriNode->item(0)->nodeValue;
Chris@0 471 }
Chris@0 472
Chris@0 473 if (empty($author)) {
Chris@0 474 return;
Chris@0 475 }
Chris@0 476 return $author;
Chris@0 477 }
Chris@0 478
Chris@0 479 /**
Chris@0 480 * Attempt to absolutise the URI, i.e. if a relative URI apply the
Chris@0 481 * xml:base value as a prefix to turn into an absolute URI.
Chris@17 482 *
Chris@17 483 * @param string $link
Chris@17 484 * @return string|null
Chris@0 485 */
Chris@0 486 protected function absolutiseUri($link)
Chris@0 487 {
Chris@12 488 if (! Uri::factory($link)->isAbsolute()) {
Chris@0 489 if ($this->getBaseUrl() !== null) {
Chris@0 490 $link = $this->getBaseUrl() . $link;
Chris@12 491 if (! Uri::factory($link)->isValid()) {
Chris@0 492 $link = null;
Chris@0 493 }
Chris@0 494 }
Chris@0 495 }
Chris@0 496 return $link;
Chris@0 497 }
Chris@0 498
Chris@0 499 /**
Chris@0 500 * Register the default namespaces for the current feed format
Chris@0 501 */
Chris@0 502 protected function registerNamespaces()
Chris@0 503 {
Chris@0 504 if ($this->getType() == Reader\Reader::TYPE_ATOM_10
Chris@0 505 || $this->getType() == Reader\Reader::TYPE_ATOM_03
Chris@0 506 ) {
Chris@0 507 return; // pre-registered at Feed level
Chris@0 508 }
Chris@0 509 $atomDetected = $this->getAtomType();
Chris@0 510 switch ($atomDetected) {
Chris@0 511 case Reader\Reader::TYPE_ATOM_03:
Chris@0 512 $this->xpath->registerNamespace('atom', Reader\Reader::NAMESPACE_ATOM_03);
Chris@0 513 break;
Chris@0 514 default:
Chris@0 515 $this->xpath->registerNamespace('atom', Reader\Reader::NAMESPACE_ATOM_10);
Chris@0 516 break;
Chris@0 517 }
Chris@0 518 }
Chris@0 519
Chris@0 520 /**
Chris@0 521 * Detect the presence of any Atom namespaces in use
Chris@0 522 */
Chris@0 523 protected function getAtomType()
Chris@0 524 {
Chris@0 525 $dom = $this->getDomDocument();
Chris@0 526 $prefixAtom03 = $dom->lookupPrefix(Reader\Reader::NAMESPACE_ATOM_03);
Chris@0 527 $prefixAtom10 = $dom->lookupPrefix(Reader\Reader::NAMESPACE_ATOM_10);
Chris@0 528 if ($dom->isDefaultNamespace(Reader\Reader::NAMESPACE_ATOM_10)
Chris@12 529 || ! empty($prefixAtom10)
Chris@0 530 ) {
Chris@0 531 return Reader\Reader::TYPE_ATOM_10;
Chris@0 532 }
Chris@0 533 if ($dom->isDefaultNamespace(Reader\Reader::NAMESPACE_ATOM_03)
Chris@12 534 || ! empty($prefixAtom03)
Chris@0 535 ) {
Chris@0 536 return Reader\Reader::TYPE_ATOM_03;
Chris@0 537 }
Chris@0 538 }
Chris@0 539 }