diff options
Diffstat (limited to 'src/Source.php')
| -rw-r--r-- | src/Source.php | 170 |
1 files changed, 109 insertions, 61 deletions
diff --git a/src/Source.php b/src/Source.php index c7ed116..932fb84 100644 --- a/src/Source.php +++ b/src/Source.php @@ -1,47 +1,9 @@ <?php +// SPDX-FileCopyrightText: 2004-2023 Ryan Parman, Sam Sneddon, Ryan McCue +// SPDX-License-Identifier: BSD-3-Clause + declare(strict_types=1); -/** - * SimplePie - * - * A PHP-Based RSS and Atom Feed Framework. - * Takes the hard work out of managing a complete RSS/Atom solution. - * - * Copyright (c) 2004-2022, Ryan Parman, Sam Sneddon, Ryan McCue, and contributors - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without modification, are - * permitted provided that the following conditions are met: - * - * * Redistributions of source code must retain the above copyright notice, this list of - * conditions and the following disclaimer. - * - * * Redistributions in binary form must reproduce the above copyright notice, this list - * of conditions and the following disclaimer in the documentation and/or other materials - * provided with the distribution. - * - * * Neither the name of the SimplePie Team nor the names of its contributors may be used - * to endorse or promote products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS - * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY - * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS - * AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR - * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - * - * @package SimplePie - * @copyright 2004-2016 Ryan Parman, Sam Sneddon, Ryan McCue - * @author Ryan Parman - * @author Sam Sneddon - * @author Ryan McCue - * @link http://simplepie.org/ SimplePie - * @license http://www.opensource.org/licenses/bsd-license.php BSD License - */ namespace SimplePie; @@ -51,33 +13,47 @@ namespace SimplePie; * Used by {@see \SimplePie\Item::get_source()} * * This class can be overloaded with {@see \SimplePie::set_source_class()} - * - * @package SimplePie - * @subpackage API */ class Source implements RegistryAware { + /** @var Item */ public $item; + /** @var array<string, mixed> */ public $data = []; + /** @var Registry */ protected $registry; - public function __construct($item, $data) + /** + * @param array<string, mixed> $data + */ + public function __construct(Item $item, array $data) { $this->item = $item; $this->data = $data; } - public function set_registry(\SimplePie\Registry $registry)/* : void */ + /** + * @return void + */ + public function set_registry(\SimplePie\Registry $registry) { $this->registry = $registry; } + /** + * @return string + */ public function __toString() { return md5(serialize($this->data)); } - public function get_source_tags($namespace, $tag) + /** + * @param string $namespace + * @param string $tag + * @return array<array<string, mixed>>|null + */ + public function get_source_tags(string $namespace, string $tag) { if (isset($this->data['child'][$namespace][$tag])) { return $this->data['child'][$namespace][$tag]; @@ -86,21 +62,37 @@ class Source implements RegistryAware return null; } - public function get_base($element = []) + /** + * @param array<string, mixed> $element + * @return string + */ + public function get_base(array $element = []) { return $this->item->get_base($element); } - public function sanitize($data, $type, $base = '') + /** + * @param string $data + * @param int-mask-of<SimplePie::CONSTRUCT_*> $type + * @param string $base + * @return string + */ + public function sanitize(string $data, $type, string $base = '') { return $this->item->sanitize($data, $type, $base); } + /** + * @return Item + */ public function get_item() { return $this->item; } + /** + * @return string|null + */ public function get_title() { if ($return = $this->get_source_tags(\SimplePie\SimplePie::NAMESPACE_ATOM_10, 'title')) { @@ -122,7 +114,11 @@ class Source implements RegistryAware return null; } - public function get_category($key = 0) + /** + * @param int $key + * @return Category|null + */ + public function get_category(int $key = 0) { $categories = $this->get_categories(); if (isset($categories[$key])) { @@ -132,6 +128,9 @@ class Source implements RegistryAware return null; } + /** + * @return array<Category>|null + */ public function get_categories() { $categories = []; @@ -176,7 +175,11 @@ class Source implements RegistryAware return null; } - public function get_author($key = 0) + /** + * @param int $key + * @return Author|null + */ + public function get_author(int $key = 0) { $authors = $this->get_authors(); if (isset($authors[$key])) { @@ -186,6 +189,9 @@ class Source implements RegistryAware return null; } + /** + * @return array<Author>|null + */ public function get_authors() { $authors = []; @@ -197,7 +203,8 @@ class Source implements RegistryAware $name = $this->sanitize($author['child'][\SimplePie\SimplePie::NAMESPACE_ATOM_10]['name'][0]['data'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } if (isset($author['child'][\SimplePie\SimplePie::NAMESPACE_ATOM_10]['uri'][0]['data'])) { - $uri = $this->sanitize($author['child'][\SimplePie\SimplePie::NAMESPACE_ATOM_10]['uri'][0]['data'], \SimplePie\SimplePie::CONSTRUCT_IRI, $this->get_base($author['child'][\SimplePie\SimplePie::NAMESPACE_ATOM_10]['uri'][0])); + $uri = $author['child'][\SimplePie\SimplePie::NAMESPACE_ATOM_10]['uri'][0]; + $uri = $this->sanitize($uri['data'], \SimplePie\SimplePie::CONSTRUCT_IRI, $this->get_base($uri)); } if (isset($author['child'][\SimplePie\SimplePie::NAMESPACE_ATOM_10]['email'][0]['data'])) { $email = $this->sanitize($author['child'][\SimplePie\SimplePie::NAMESPACE_ATOM_10]['email'][0]['data'], \SimplePie\SimplePie::CONSTRUCT_TEXT); @@ -214,7 +221,8 @@ class Source implements RegistryAware $name = $this->sanitize($author[0]['child'][\SimplePie\SimplePie::NAMESPACE_ATOM_03]['name'][0]['data'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } if (isset($author[0]['child'][\SimplePie\SimplePie::NAMESPACE_ATOM_03]['url'][0]['data'])) { - $url = $this->sanitize($author[0]['child'][\SimplePie\SimplePie::NAMESPACE_ATOM_03]['url'][0]['data'], \SimplePie\SimplePie::CONSTRUCT_IRI, $this->get_base($author[0]['child'][\SimplePie\SimplePie::NAMESPACE_ATOM_03]['url'][0])); + $url = $author[0]['child'][\SimplePie\SimplePie::NAMESPACE_ATOM_03]['url'][0]; + $url = $this->sanitize($url['data'], \SimplePie\SimplePie::CONSTRUCT_IRI, $this->get_base($url)); } if (isset($author[0]['child'][\SimplePie\SimplePie::NAMESPACE_ATOM_03]['email'][0]['data'])) { $email = $this->sanitize($author[0]['child'][\SimplePie\SimplePie::NAMESPACE_ATOM_03]['email'][0]['data'], \SimplePie\SimplePie::CONSTRUCT_TEXT); @@ -240,7 +248,11 @@ class Source implements RegistryAware return null; } - public function get_contributor($key = 0) + /** + * @param int $key + * @return Author|null + */ + public function get_contributor(int $key = 0) { $contributors = $this->get_contributors(); if (isset($contributors[$key])) { @@ -250,6 +262,9 @@ class Source implements RegistryAware return null; } + /** + * @return array<Author>|null + */ public function get_contributors() { $contributors = []; @@ -261,7 +276,8 @@ class Source implements RegistryAware $name = $this->sanitize($contributor['child'][\SimplePie\SimplePie::NAMESPACE_ATOM_10]['name'][0]['data'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } if (isset($contributor['child'][\SimplePie\SimplePie::NAMESPACE_ATOM_10]['uri'][0]['data'])) { - $uri = $this->sanitize($contributor['child'][\SimplePie\SimplePie::NAMESPACE_ATOM_10]['uri'][0]['data'], \SimplePie\SimplePie::CONSTRUCT_IRI, $this->get_base($contributor['child'][\SimplePie\SimplePie::NAMESPACE_ATOM_10]['uri'][0])); + $uri = $contributor['child'][\SimplePie\SimplePie::NAMESPACE_ATOM_10]['uri'][0]; + $uri = $this->sanitize($uri['data'], \SimplePie\SimplePie::CONSTRUCT_IRI, $this->get_base($uri)); } if (isset($contributor['child'][\SimplePie\SimplePie::NAMESPACE_ATOM_10]['email'][0]['data'])) { $email = $this->sanitize($contributor['child'][\SimplePie\SimplePie::NAMESPACE_ATOM_10]['email'][0]['data'], \SimplePie\SimplePie::CONSTRUCT_TEXT); @@ -278,7 +294,8 @@ class Source implements RegistryAware $name = $this->sanitize($contributor['child'][\SimplePie\SimplePie::NAMESPACE_ATOM_03]['name'][0]['data'], \SimplePie\SimplePie::CONSTRUCT_TEXT); } if (isset($contributor['child'][\SimplePie\SimplePie::NAMESPACE_ATOM_03]['url'][0]['data'])) { - $url = $this->sanitize($contributor['child'][\SimplePie\SimplePie::NAMESPACE_ATOM_03]['url'][0]['data'], \SimplePie\SimplePie::CONSTRUCT_IRI, $this->get_base($contributor['child'][\SimplePie\SimplePie::NAMESPACE_ATOM_03]['url'][0])); + $url = $contributor['child'][\SimplePie\SimplePie::NAMESPACE_ATOM_03]['url'][0]; + $url = $this->sanitize($url['data'], \SimplePie\SimplePie::CONSTRUCT_IRI, $this->get_base($url)); } if (isset($contributor['child'][\SimplePie\SimplePie::NAMESPACE_ATOM_03]['email'][0]['data'])) { $email = $this->sanitize($contributor['child'][\SimplePie\SimplePie::NAMESPACE_ATOM_03]['email'][0]['data'], \SimplePie\SimplePie::CONSTRUCT_TEXT); @@ -295,7 +312,12 @@ class Source implements RegistryAware return null; } - public function get_link($key = 0, $rel = 'alternate') + /** + * @param int $key + * @param string $rel + * @return string|null + */ + public function get_link(int $key = 0, string $rel = 'alternate') { $links = $this->get_links($rel); if (isset($links[$key])) { @@ -307,13 +329,19 @@ class Source implements RegistryAware /** * Added for parity between the parent-level and the item/entry-level. + * + * @return string|null */ public function get_permalink() { return $this->get_link(0); } - public function get_links($rel = 'alternate') + /** + * @param string $rel + * @return array<string>|null + */ + public function get_links(string $rel = 'alternate') { if (!isset($this->data['links'])) { $this->data['links'] = []; @@ -345,15 +373,17 @@ class Source implements RegistryAware $keys = array_keys($this->data['links']); foreach ($keys as $key) { + $key = (string) $key; + if ($this->registry->call(Misc::class, 'is_isegment_nz_nc', [$key])) { if (isset($this->data['links'][\SimplePie\SimplePie::IANA_LINK_RELATIONS_REGISTRY . $key])) { $this->data['links'][\SimplePie\SimplePie::IANA_LINK_RELATIONS_REGISTRY . $key] = array_merge($this->data['links'][$key], $this->data['links'][\SimplePie\SimplePie::IANA_LINK_RELATIONS_REGISTRY . $key]); - $this->data['links'][$key] =& $this->data['links'][\SimplePie\SimplePie::IANA_LINK_RELATIONS_REGISTRY . $key]; + $this->data['links'][$key] = &$this->data['links'][\SimplePie\SimplePie::IANA_LINK_RELATIONS_REGISTRY . $key]; } else { - $this->data['links'][\SimplePie\SimplePie::IANA_LINK_RELATIONS_REGISTRY . $key] =& $this->data['links'][$key]; + $this->data['links'][\SimplePie\SimplePie::IANA_LINK_RELATIONS_REGISTRY . $key] = &$this->data['links'][$key]; } } elseif (substr($key, 0, 41) === \SimplePie\SimplePie::IANA_LINK_RELATIONS_REGISTRY) { - $this->data['links'][substr($key, 41)] =& $this->data['links'][$key]; + $this->data['links'][substr($key, 41)] = &$this->data['links'][$key]; } $this->data['links'][$key] = array_unique($this->data['links'][$key]); } @@ -366,6 +396,9 @@ class Source implements RegistryAware return null; } + /** + * @return string|null + */ public function get_description() { if ($return = $this->get_source_tags(\SimplePie\SimplePie::NAMESPACE_ATOM_10, 'subtitle')) { @@ -391,6 +424,9 @@ class Source implements RegistryAware return null; } + /** + * @return string|null + */ public function get_copyright() { if ($return = $this->get_source_tags(\SimplePie\SimplePie::NAMESPACE_ATOM_10, 'rights')) { @@ -408,6 +444,9 @@ class Source implements RegistryAware return null; } + /** + * @return string|null + */ public function get_language() { if ($return = $this->get_source_tags(\SimplePie\SimplePie::NAMESPACE_RSS_20, 'language')) { @@ -423,6 +462,9 @@ class Source implements RegistryAware return null; } + /** + * @return float|null + */ public function get_latitude() { if ($return = $this->get_source_tags(\SimplePie\SimplePie::NAMESPACE_W3C_BASIC_GEO, 'lat')) { @@ -434,6 +476,9 @@ class Source implements RegistryAware return null; } + /** + * @return float|null + */ public function get_longitude() { if ($return = $this->get_source_tags(\SimplePie\SimplePie::NAMESPACE_W3C_BASIC_GEO, 'long')) { @@ -447,6 +492,9 @@ class Source implements RegistryAware return null; } + /** + * @return string|null + */ public function get_image_url() { if ($return = $this->get_source_tags(\SimplePie\SimplePie::NAMESPACE_ITUNES, 'image')) { |
