mirror of
https://github.com/twisterarmy/twister-php.git
synced 2025-02-06 11:54:27 +00:00
initial commit
This commit is contained in:
parent
2864d0e7d0
commit
aa48d4b4af
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
/vendor/
|
12
composer.json
Normal file
12
composer.json
Normal file
@ -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": {}
|
||||||
|
}
|
119
src/Rss.php
Normal file
119
src/Rss.php
Normal file
@ -0,0 +1,119 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Twisterarmy\Twister;
|
||||||
|
|
||||||
|
class Rss
|
||||||
|
{
|
||||||
|
private int $_length = 256;
|
||||||
|
|
||||||
|
private string $_format = '{title} {link}';
|
||||||
|
|
||||||
|
public function setLength(int $value)
|
||||||
|
{
|
||||||
|
$this->_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;
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user