Skip to content

Commit

Permalink
In the refactored code, I've added the following types:
Browse files Browse the repository at this point in the history
Added return type array to the parseRssFeeds method.
Added return type string to the saveImageToStorage method.
Added return type bool|string to the retrieveFullContent method.
Added return type string|null to the getImageWithSizeGreaterThan method.
Added type annotations to the properties within the foreach loops, such as (string).
  • Loading branch information
zoranbogoevski committed Jun 11, 2023
1 parent fa0992c commit bf2f429
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 29 deletions.
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ This package provides an easy way to parse RSS feeds and save them into your app

## Requirements

* PHP 7.3 or higher
* PHP 7.4 or higher
* Laravel 8.0 or higher
* Composer

Expand Down
48 changes: 20 additions & 28 deletions src/RssFeed.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
class RssFeed
{
/**
* @param array $feedUrls
* @return array
* @throws CantOpenFileFromUrlException
* @throws Exception
*/
public function parseRssFeeds(array $feedUrls): array
Expand All @@ -24,15 +27,15 @@ public function parseRssFeeds(array $feedUrls): array
$xml = file_get_contents($feedUrl);
$xmlObject = new SimpleXMLElement($xml);

$channelTitle = $xmlObject->channel->title;
$channelLink = $xmlObject->channel->link;
$channelDescription = $xmlObject->channel->description;
$channelTitle = (string) $xmlObject->channel->title;
$channelLink = (string) $xmlObject->channel->link;
$channelDescription = (string) $xmlObject->channel->description;

foreach ($xmlObject->channel->item as $item) {
$itemTitle = $item->title;
$itemLink = $item->link;
$itemPubDate = $item->pubDate;
$itemDescription = $item->description;
$itemTitle = (string) $item->title;
$itemLink = (string) $item->link;
$itemPubDate = (string) $item->pubDate;
$itemDescription = (string) $item->description;

// Save the image to storage
$imagePath = $this->saveImageToStorage($itemDescription);
Expand All @@ -59,9 +62,11 @@ public function parseRssFeeds(array $feedUrls): array
}

/**
* @param string $itemDescription
* @return string
* @throws CantOpenFileFromUrlException
*/
public function saveImageToStorage($itemDescription): string
public function saveImageToStorage(string $itemDescription): string
{
$find_img = $this->getImageWithSizeGreaterThan($itemDescription);
$file = UrlUploadedFile::createFromUrl($find_img);
Expand All @@ -70,20 +75,14 @@ public function saveImageToStorage($itemDescription): string
return $imageName;
}

public function retrieveFullContent($postLink): bool|string
public function retrieveFullContent(string $postLink): bool|string
{
// Fetch the HTML content of the post URL
$html = file_get_contents($postLink);

// Convert the HTML content to UTF-8 if needed
if (!mb_check_encoding($html, 'UTF-8')) {
$html = mb_convert_encoding($html, 'UTF-8');
}

// Create a DOMDocument object and load the HTML content
$dom = new DOMDocument();
libxml_use_internal_errors(true);
$dom->loadHTML('<?xml encoding="UTF-8">' . $html);
@$dom->loadHTML($html);

// Create a DOMXPath object
$xpath = new DOMXPath($dom);
Expand All @@ -110,23 +109,16 @@ public function retrieveFullContent($postLink): bool|string
}
}

// Decode HTML entities in the retrieved full post content
$fullContent = html_entity_decode($fullContent, ENT_QUOTES | ENT_HTML5, 'UTF-8');

// Remove all HTML tags from the full post content
$fullContent = strip_tags($fullContent);

// Trim whitespace from the full post content
// Return the retrieved full post content
return trim($fullContent);
return $fullContent;
}

/**
* @param string $html
* @param int $size
* @return mixed
* @return string|null
*/
public static function getImageWithSizeGreaterThan(string $html, int $size = 200): mixed
public static function getImageWithSizeGreaterThan(string $html, int $size = 200): ?string
{
ini_set('allow_url_fopen', 1);

Expand All @@ -147,7 +139,7 @@ public static function getImageWithSizeGreaterThan(string $html, int $size = 200
// Do nothing if image cannot be processed
}
}
return $featured_img;
}

return $featured_img ?: null;
}
}

0 comments on commit bf2f429

Please sign in to comment.