← Zurück · Zurück zur README · Weiter →
Voreinstellungen
Mit den sofort einsatzbereiten Voreinstellungen können Sie Feeds schnell erstellen:
DragonCode\LaravelFeed\Presets\InstagramFeedPresetDragonCode\LaravelFeed\Presets\YandexFeedPresetDragonCode\LaravelFeed\Presets\SitemapFeedPresetDragonCode\LaravelFeed\Presets\RssFeedPreset
Erstellen Sie eine Feed-Klasse manuell oder mit dem Konsolenbefehl php artisan make:feed <name> und erweitern Sie die benötigte Voreinstellung. Beispiel:
use DragonCode\LaravelFeed\Presets\InstagramFeedPreset;
use DragonCode\LaravelFeed\Presets\RssFeedPreset;
use DragonCode\LaravelFeed\Presets\SitemapFeedPreset;
use DragonCode\LaravelFeed\Presets\YandexFeedPreset;
class ProductFeed extends InstagramFeedPreset {}
class ProductFeed extends YandexFeedPreset {}
class ProductFeed extends SitemapFeedPreset {}
class ProductFeed extends RssFeedPreset {}
Implementieren Sie anschließend die beiden erforderlichen Methoden builder und item. Beispiel:
namespace App\Feeds;
use App\Models\Product;
use DragonCode\LaravelFeed\Feeds\Items\FeedItem;
use DragonCode\LaravelFeed\Presets\InstagramFeedPreset;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
class InstagramFeed extends InstagramFeedPreset
{
public function builder(): Builder
{
return Product::query();
}
public function item(Model $model): FeedItem
{
return parent::item($model)
->title($model->title)
->description($model->description)
->brand($model->brand) // By default, null
->url($model->url)
->price(price: $model->price, salePrice: $model->price) // By default, salePrice = price
->image($model->images[0])
->images($model->images) // By default, null
->availability($model->quantity > 0 ? 'in stock' : 'out of stock') // By default, 'in stock'
->status($model->quantity > 0 ? 'active' : 'inactive') // By default, 'active'
->condition('new') // By default, 'new'
->group(12345) // By default, null
->googleCategory(123) // By default, null
->facebookCategory(456) // By default, null
->additional([
'g:foo' => 'Some foo',
'g:bar' => 'Some bar',
'g:baz' => [
'@attributes' => ['qwe' => 'rty'],
'@value' => 'Some baz',
],
'@g:arrayable' => [
'a',
'b',
'c',
],
]);
}
}
Technisch entsprechen Voreinstellungsklassen der Basisklasse Feed. Zusätzlich enthalten sie jedoch die dienstspezifische Logik ihres jeweiligen Ziels, einschließlich des Aufbaus von Informationsblöcken und Elementen.
Standardwerte
Instagram
class InstagramFeedItem extends FeedItem
namespace DragonCode\LaravelFeed\Presets\Items;
use DragonCode\LaravelFeed\Feeds\Items\FeedItem;
use function blank;
use function collect;
/** @property-read \Illuminate\Database\Eloquent\Model $model */
class InstagramFeedItem extends FeedItem
{
protected string $title;
protected string $description;
protected string $url;
protected string $image;
protected ?array $images = null;
protected ?string $brand = null;
protected string $condition = 'new';
protected string $availability = 'in stock';
protected float $price;
protected float $salePrice;
protected ?string $groupId = null;
protected ?string $status = 'active';
protected ?int $googleCategory = null;
protected ?int $facebookCategory = null;
protected array $additional = [];
public function name(): string
{
return 'item';
}
public function title(string $title): static
{
$this->title = $title;
return $this;
}
public function description(string $description): static
{
$this->description = $description;
return $this;
}
public function brand(string $name): static
{
$this->brand = $name;
return $this;
}
public function url(string $url): static
{
$this->url = $url;
return $this;
}
public function image(string $url): static
{
$this->image = $url;
return $this;
}
public function images(?array $urls): static
{
$this->images = $urls;
return $this;
}
public function condition(string $condition): static
{
$this->condition = $condition;
return $this;
}
public function availability(string $availability): static
{
$this->availability = $availability;
return $this;
}
public function price(float $price, ?float $salePrice = null): static
{
$this->price = $price;
$this->salePrice = $salePrice ?? $price;
return $this;
}
public function group(int|string|null $id): static
{
$this->groupId = (string) $id ?: null;
return $this;
}
public function status(?string $status): static
{
$this->status = $status;
return $this;
}
public function googleCategory(?int $id): static
{
$this->googleCategory = $id;
return $this;
}
public function facebookCategory(?int $id): static
{
$this->facebookCategory = $id;
return $this;
}
public function additional(array $additional): static
{
$this->additional = $additional;
return $this;
}
public function toArray(): array
{
return collect([
'g:id' => $this->model->getKey(),
'g:title' => ['@cdata' => $this->title],
'g:description' => ['@cdata' => $this->description],
'g:link' => $this->url,
'g:image_link' => $this->image,
'@g:additional_image_link' => $this->images,
'g:brand' => $this->brand,
'g:condition' => $this->condition,
'g:availability' => $this->availability,
'g:price' => $this->price,
'g:sale_price' => $this->salePrice,
'g:item_group_id' => $this->groupId,
'g:status' => $this->status,
'g:google_product_category' => $this->googleCategory,
'g:fb_product_category' => $this->facebookCategory,
])
->merge($this->additional)
->reject(static fn (mixed $value) => blank($value))
->all();
}
}
Yandex
class YandexFeedItem extends FeedItem
namespace DragonCode\LaravelFeed\Presets\Items;
use DragonCode\LaravelFeed\Feeds\Items\FeedItem;
use function blank;
use function collect;
/** @property-read \Illuminate\Database\Eloquent\Model $model */
class YandexFeedItem extends FeedItem
{
protected int|string|null $attributeId = null;
protected bool $attributeAvailable = true;
protected string $attributeType = 'vendor.model';
protected string $url;
protected ?string $barcode = null;
protected string $title;
protected string $description;
protected string $price;
protected string $currencyId = 'RUB';
protected ?string $vendor = null;
protected array $images;
protected array $additional = [];
public function name(): string
{
return 'offer';
}
public function attributeId(int|string|null $id): static
{
$this->attributeId = $id;
return $this;
}
public function attributeAvailable(bool $available): static
{
$this->attributeAvailable = $available;
return $this;
}
public function attributeType(string $type): static
{
$this->attributeType = $type;
return $this;
}
public function url(string $url): static
{
$this->url = $url;
return $this;
}
public function barcode(string $barcode): static
{
$this->barcode = $barcode;
return $this;
}
public function title(string $title): static
{
$this->title = $title;
return $this;
}
public function description(string $description): static
{
$this->description = $description;
return $this;
}
public function price(float|string $price): static
{
$this->price = (string) $price;
return $this;
}
public function currencyId(string $currencyId): static
{
$this->currencyId = $currencyId;
return $this;
}
public function vendor(?string $vendor): static
{
$this->vendor = $vendor;
return $this;
}
public function images(array $images): static
{
$this->images = $images;
return $this;
}
public function additional(array $additional): static
{
$this->additional = $additional;
return $this;
}
public function attributes(): array
{
return [
'id' => $this->attributeId,
'available' => $this->attributeAvailable,
'type' => $this->attributeType,
];
}
public function toArray(): array
{
return collect([
'url' => $this->url,
'barcode' => $this->barcode,
'name' => $this->title,
'description' => $this->description,
'price' => $this->price,
'currencyId' => $this->currencyId,
'vendor' => $this->vendor,
'@picture' => $this->images,
])
->merge($this->additional)
->reject(static fn (mixed $value) => blank($value))
->all();
}
}
Sitemap
class SitemapFeedItem extends FeedItem
namespace DragonCode\LaravelFeed\Presets\Items;
use Carbon\Carbon;
use DragonCode\LaravelFeed\Feeds\Items\FeedItem;
/** @property-read \Illuminate\Database\Eloquent\Model $model */
class SitemapFeedItem extends FeedItem
{
protected string $url;
protected string $modifiedAt;
protected float $priority = 0.9;
public function name(): string
{
return 'url';
}
public function url(string $url): static
{
$this->url = $url;
return $this;
}
public function modifiedAt(Carbon $updatedAt): static
{
$this->modifiedAt = $updatedAt->toIso8601String();
return $this;
}
public function priority(float $priority): static
{
$this->priority = $priority;
return $this;
}
public function toArray(): array
{
return [
'loc' => $this->url,
'lastmod' => $this->modifiedAt,
'priority' => $this->priority,
];
}
}
RSS
class RssFeedItem extends FeedItem
namespace DragonCode\LaravelFeed\Presets\Items;
use Carbon\Carbon;
use DragonCode\LaravelFeed\Feeds\Items\FeedItem;
use function blank;
use function collect;
/** @property-read \Illuminate\Database\Eloquent\Model $model */
class RssFeedItem extends FeedItem
{
protected ?string $guid = null;
protected string $title;
protected string $url;
protected string $description;
protected ?string $category = null;
protected ?Carbon $publishedAt = null;
public array $additional = [];
public function guid(int|string $guid): static
{
$this->guid = (string) $guid;
return $this;
}
public function title(string $title): static
{
$this->title = $title;
return $this;
}
public function url(string $url): static
{
$this->url = $url;
return $this;
}
public function description(string $description): static
{
$this->description = $description;
return $this;
}
public function category(string $category): static
{
$this->category = $category;
return $this;
}
public function publishedAt(Carbon $publishedAt): static
{
$this->publishedAt = $publishedAt;
return $this;
}
public function additional(array $additional): static
{
$this->additional = $additional;
return $this;
}
public function toArray(): array
{
return collect([
'title' => $this->title,
'link' => $this->url,
'guid' => $this->guid,
'description' => ['@cdata' => $this->description],
'category' => $this->category,
'pubDate' => $this->publishedAt->toRfc1123String(),
])
->merge($this->additional)
->reject(static fn (mixed $value) => blank($value))
->all();
}
}