diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..57872d0 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/vendor/ diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..cec43eb --- /dev/null +++ b/composer.json @@ -0,0 +1,12 @@ +{ + "name": "twisterarmy/twister", + "description": "PHP 8 / Composer Tools for Twister API", + "type": "library", + "license": "MIT", + "autoload": { + "psr-4": { + "Twisterarmy\\Twister\\": "src/" + } + }, + "require": {} +} diff --git a/src/Rss.php b/src/Rss.php new file mode 100644 index 0000000..80c8d69 --- /dev/null +++ b/src/Rss.php @@ -0,0 +1,119 @@ +_length = $value; + } + + public function setFormat(string $value) + { + $this->_format = $value; + } + + public function get(string $url, array &$error = []): ?array + { + if (empty($url)) + { + $error[] = _('RSS address required!'); + + return null; + } + + if (false === (bool) filter_var($url, FILTER_VALIDATE_URL)) + { + $error[] = _('Valid RSS address required!'); + + return null; + } + + if (!$xml = simplexml_load_file($url)) + { + $error[] = sprintf( + 'Could not open RSS feed "%s"', + $url + ); + + return null; + } + + if (empty($xml->channel)) + { + $error[] = _('RSS channel not found!'); + + return null; + } + + if (empty($xml->channel->item)) + { + $error[] = _('RSS channel item not found!'); + + return null; + } + + $messages = []; + + foreach ($xml->channel->item as $item) + { + if (empty($item->link)) + { + $error[] = _('RSS channel item does not contain link!'); + + continue; + } + + if (empty($item->title)) + { + $error[] = _('RSS channel item does not contain title!'); + + continue; + } + + $link = trim( + (string) $item->link + ); + + $title = trim( + strip_tags( + html_entity_decode( + (string) $item->title + ) + ) + ); + + $message = str_replace( + [ + '{link}', + '{title}', + // .. + ], + [ + $link, + $title, + // .. + ], + $this->_format + ); + + if (mb_strlen($message) > $this->_length) + { + $error[] = _('Message does not correspond twister protocol length!'); + + continue; + } + + $messages[mb_strlen($message)] = $message; + } + + return $messages; + } +} \ No newline at end of file