KT-personal-website/app/Fetching/Shaarli.php

77 lines
2.2 KiB
PHP
Raw Normal View History

2024-10-22 12:26:08 +02:00
<?php
namespace App\Fetching;
use App\Fetching\RssFeed;
use App\Utils\Debug;
class Shaarli {
private $bookmarkList = [];
private string $shaarliUrl;
private $shaarliFeed;
public $shaarliFeedUpdate;
private $numberEntry;
/**
* Constructor for initializing the feed and configuration
*
* @param array $params Configuration parameters
* @return self
*/
function __construct(array $params) {
$feed = new RssFeed();
$this->shaarliFeed = $feed->load($params['config']['fetching']['Shaarli']);
$this->shaarliFeedUpdate = $feed->lastUpdate();
$this->numberEntry = $params['config']['numberOfLastItem'];
return $this;
}
/**
* Retrieves the latest images from the Shaarli feed
*
* @return array $bookmakList Array of last bookmark
*/
public function getLastBookmark(): array {
$totalEntry = count($this->shaarliFeed->xml->entry);
if ($totalEntry <= $this->numberEntry) {
$needItem = $totalEntry;
} else {
$needItem = $this->numberEntry;
}
for ($i = 0; $i < $needItem; $i++) {
$bookmarkList[] = [
'title' => (string)$this->shaarliFeed->xml->entry[$i]->title[0],
'link' => (string)$this->shaarliFeed->xml->entry[$i]->link[0]->attributes()[0],
'permalink' => (string)$this->shaarliFeed->xml->entry[$i]->id[0],
];
}
$this->bookmarkList = $bookmarkList;
return $bookmarkList;
}
/**
* Generates an HTML list of images from the images list
*
* @return string|null Returns the generated HTML string of images
*/
public function makeList(): ?string {
if (!empty($this->bookmarkList)) {
$htmlBookmark = '<ul data-lastUpdate="' . date("Y-m-d") . '">';
foreach ($this->bookmarkList as $value) {
$htmlBookmark .=
'
<li>
<a href="' . $value['permalink'] . '">' . $value['title'] . '</a>
</li>';
}
$htmlBookmark .= '
</ul>';
return $htmlBookmark;
}
return null;
}
}