refactor: strict types, typed props, split toXml/generate, custom exception

This commit is contained in:
Joel Male
2026-04-11 08:09:43 +10:00
parent 8f0ae03c4b
commit 1c426d0d1b
2 changed files with 34 additions and 31 deletions
@@ -0,0 +1,9 @@
<?php
declare(strict_types=1);
namespace Wearepixel\LaravelGoogleShoppingFeed\Exceptions;
use RuntimeException;
class MissingRequiredFieldException extends RuntimeException {}
+25 -31
View File
@@ -1,24 +1,23 @@
<?php <?php
declare(strict_types=1);
namespace Wearepixel\LaravelGoogleShoppingFeed; namespace Wearepixel\LaravelGoogleShoppingFeed;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Response as ResponseFacade;
use Spatie\ArrayToXml\ArrayToXml; use Spatie\ArrayToXml\ArrayToXml;
use Wearepixel\LaravelGoogleShoppingFeed\Exceptions\MissingRequiredFieldException;
class LaravelGoogleShoppingFeed class LaravelGoogleShoppingFeed
{ {
public $title; public string $title = '';
public string $description = '';
public string $link = '';
public $description; protected array $products = [];
public $link; protected array $requiredProductFields = [
protected $xml = [];
protected $products = [];
protected $currency = 'AUD';
protected $requiredProductFields = [
'id', 'id',
'link', 'link',
'title', 'title',
@@ -26,10 +25,9 @@ class LaravelGoogleShoppingFeed
'g:image_link', 'g: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;
@@ -41,22 +39,18 @@ public function addItem(array $item): bool
{ {
foreach ($this->requiredProductFields as $field) { foreach ($this->requiredProductFields 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; 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',
@@ -71,18 +65,18 @@ public function generate()
]; ];
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([ $xml = (string) preg_replace('/item_\d+/', 'item', $xml);
'/item_[0-9][0-9][0-9][0-9]/',
'/item_[0-9][0-9][0-9]/',
'/item_[0-9][0-9]/',
'/item_[0-9]/',
], 'item', $xml);
return response($xml, 200, ['Content-Type' => 'text/xml']); return $xml;
}
public function generate(): Response
{
return ResponseFacade::make($this->toXml(), 200, ['Content-Type' => 'text/xml']);
} }
} }