mirror of
https://github.com/wearepixel/laravel-facebook-feed.git
synced 2026-11-14 13:51:42 +00:00
refactor: strict types, typed props, split toXml/generate, custom exception
This commit is contained in:
@@ -0,0 +1,9 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Wearepixel\LaravelFacebookFeed\Exceptions;
|
||||||
|
|
||||||
|
use RuntimeException;
|
||||||
|
|
||||||
|
class MissingRequiredFieldException extends RuntimeException {}
|
||||||
+33
-35
@@ -1,31 +1,33 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
namespace Wearepixel\LaravelFacebookFeed;
|
namespace Wearepixel\LaravelFacebookFeed;
|
||||||
|
|
||||||
|
use Illuminate\Http\Response;
|
||||||
|
use Illuminate\Support\Facades\Response as ResponseFacade;
|
||||||
use Spatie\ArrayToXml\ArrayToXml;
|
use Spatie\ArrayToXml\ArrayToXml;
|
||||||
|
use Wearepixel\LaravelFacebookFeed\Exceptions\MissingRequiredFieldException;
|
||||||
|
|
||||||
class LaravelFacebookFeed
|
class LaravelFacebookFeed
|
||||||
{
|
{
|
||||||
public $title;
|
public readonly string $title;
|
||||||
public $description;
|
public readonly string $description;
|
||||||
public $link;
|
public readonly string $link;
|
||||||
|
|
||||||
protected $xml = [];
|
protected array $products = [];
|
||||||
protected $products = [];
|
|
||||||
protected $currency = "AUD";
|
|
||||||
|
|
||||||
protected $requiredProductFields = [
|
protected const array REQUIRED_PRODUCT_FIELDS = [
|
||||||
'id',
|
'id',
|
||||||
'link',
|
'link',
|
||||||
'title',
|
'title',
|
||||||
'price',
|
'price',
|
||||||
'image_link'
|
'image_link',
|
||||||
];
|
];
|
||||||
|
|
||||||
public static function init($title = null, $description = null, $link = null)
|
public static function init(string $title = '', string $description = '', string $link = ''): static
|
||||||
{
|
{
|
||||||
$feed = new self();
|
$feed = new static;
|
||||||
|
|
||||||
$feed->title = $title;
|
$feed->title = $title;
|
||||||
$feed->description = $description;
|
$feed->description = $description;
|
||||||
$feed->link = $link;
|
$feed->link = $link;
|
||||||
@@ -33,26 +35,20 @@ public static function init($title = null, $description = null, $link = null)
|
|||||||
return $feed;
|
return $feed;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function addItem(array $item): bool
|
public function addItem(array $item): void
|
||||||
{
|
{
|
||||||
foreach ($this->requiredProductFields as $field) {
|
foreach (self::REQUIRED_PRODUCT_FIELDS as $field) {
|
||||||
if (!isset($item[$field])) {
|
if (! isset($item[$field])) {
|
||||||
throw new \Exception("Required field '{$field}' is missing");
|
throw new MissingRequiredFieldException("Required field '{$field}' is missing");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach ($item as $key => $value) {
|
$this->products[] = $item;
|
||||||
$product[$key] = $value;
|
|
||||||
}
|
|
||||||
|
|
||||||
$this->products[] = $product;
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function generate()
|
public function toXml(): string
|
||||||
{
|
{
|
||||||
$this->xml = [
|
$data = [
|
||||||
'rss' => [
|
'rss' => [
|
||||||
'_attributes' => [
|
'_attributes' => [
|
||||||
'xmlns:g' => 'http://base.google.com/ns/1.0',
|
'xmlns:g' => 'http://base.google.com/ns/1.0',
|
||||||
@@ -62,23 +58,25 @@ public function generate()
|
|||||||
'title' => $this->title,
|
'title' => $this->title,
|
||||||
'description' => $this->description,
|
'description' => $this->description,
|
||||||
'link' => $this->link,
|
'link' => $this->link,
|
||||||
]
|
],
|
||||||
]
|
],
|
||||||
];
|
];
|
||||||
|
|
||||||
foreach ($this->products as $key => $product) {
|
foreach ($this->products as $key => $product) {
|
||||||
$this->xml['rss']['channel']['item_'.$key] = $product;
|
$data['rss']['channel']['item_' . $key] = $product;
|
||||||
}
|
}
|
||||||
|
|
||||||
$xml = ArrayToXml::convert($this->xml, '');
|
$xml = ArrayToXml::convert($data, '');
|
||||||
$xml = str_replace([' ', '<root>', '</root>', "\n", "\r", '<remove>remove</remove>'], '', $xml);
|
$xml = str_replace([' ', '<root>', '</root>', "\n", "\r", '<remove>remove</remove>'], '', $xml);
|
||||||
$xml = preg_replace([
|
// spatie/array-to-xml does not support repeated keys, so products are keyed as
|
||||||
"/item_[0-9][0-9][0-9][0-9]/",
|
// item_0, item_1, etc. and renamed back to <item> tags here.
|
||||||
"/item_[0-9][0-9][0-9]/",
|
$xml = (string) preg_replace('/item_\d+/', 'item', $xml);
|
||||||
"/item_[0-9][0-9]/",
|
|
||||||
"/item_[0-9]/",
|
|
||||||
], "item", $xml);
|
|
||||||
|
|
||||||
return response($xml)->header('Content-Type', 'text/xml');
|
return $xml;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function generate(): Response
|
||||||
|
{
|
||||||
|
return ResponseFacade::make($this->toXml(), headers: ['Content-Type' => 'application/rss+xml']);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user