Skip to content

Support for creating sitemaps #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"name": "inteve/feed-generator",
"name": "spacek/feed-generator",
"description": "Feed generator",
"type": "library",
"version": "1.2",
"license": "BSD-3-Clause",
"authors": [
{
Expand All @@ -13,7 +14,7 @@
"classmap": ["src/"]
},
"require": {
"php": ">=5.6.0",
"php": ">=7.4",
"nette/utils": "^2.2"
},
"require-dev": {
Expand Down
12 changes: 4 additions & 8 deletions readme.md
Original file line number Diff line number Diff line change
@@ -1,21 +1,16 @@

# Feed Generator

[![Build Status](https://travis-ci.org/inteve/feed-generator.svg?branch=master)](https://travis-ci.org/inteve/feed-generator)

<a href="https://www.patreon.com/bePatron?u=9680759"><img src="https://c5.patreon.com/external/logo/become_a_patron_button.png" alt="Become a Patron!" height="35"></a>
<a href="https://www.paypal.me/janpecha/1eur"><img src="https://buymecoffee.intm.org/img/button-paypal-white.png" alt="Buy me a coffee" height="35"></a>


## Installation

[Download a latest package](https://github.com/inteve/feed-generator/releases) or use [Composer](http://getcomposer.org/):

```
composer require inteve/feed-generator
composer require spacek/feed-generator
```

Feed Generator requires PHP 5.3.0 or later.
Feed Generator requires PHP 7.4.0 or later.


## Usage
Expand Down Expand Up @@ -177,4 +172,5 @@ class FeedPresenter extends Nette\Application\UI\Presenter
------------------------------

License: [New BSD License](license.md)
<br>Author: Jan Pecha, https://www.janpecha.cz/
<br>Original author: Jan Pecha, https://www.janpecha.cz/
<br />Fork author: Jiri Spacek
4 changes: 4 additions & 0 deletions src/Feeds/GoogleMerchant/GoogleMerchantFeed.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ public function generate(IOutput $output)
'g:description' => $item->getDescription(),
'g:link' => $item->getUrl(),
'g:image_link' => $item->getImageUrl(),
'g:gtin' => $item->getEan(),

// price & availability
'g:availability' => $item->getAvailability(),
Expand All @@ -177,6 +178,9 @@ public function generate(IOutput $output)
// group ID
'g:item_group_id' => $item->getGroupId(),

// Google Product Category
'g:google_product_category' => $item->getGoogleProductCategory(),

// shipping
'g:shipping' => $item->getShipping(),
'g:shipping_label' => $item->getShippingLabel(),
Expand Down
45 changes: 45 additions & 0 deletions src/Feeds/GoogleMerchant/GoogleMerchantItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ class GoogleMerchantItem implements IFeedItem
/** @var string|NULL */
private $size;

/** @var string|NULL */
private $ean;

/** @var string|NULL */
private $groupId;

Expand All @@ -68,6 +71,9 @@ class GoogleMerchantItem implements IFeedItem
/** @var string|NULL */
private $shippingLabel;

/** @var string|NULL */
private $googleProductCategory;


/**
* @return string|int
Expand Down Expand Up @@ -306,6 +312,25 @@ public function getSize()
return $this->size;
}

/**
* @return string|int|NULL
*/
public function getEan()
{
return $this->ean;
}


/**
* @param string|int
* @return self
*/
public function setEan($ean)
{
$this->ean = $ean;
return $this;
}


/**
* @param string|NULL
Expand Down Expand Up @@ -378,6 +403,26 @@ public function setShippingLabel($shippingLabel)
}


/**
* @return string|NULL
*/
public function getGoogleProductCategory()
{
return $this->googleProductCategory;
}


/**
* @param string
* @return self
*/
public function setGoogleProductCategory($googleProductCategory)
{
$this->googleProductCategory = $googleProductCategory;
return $this;
}


/**
* @return bool
*/
Expand Down
2 changes: 2 additions & 0 deletions src/Feeds/Heureka/HeurekaFeed.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ public function generate(IOutput $output)

Helpers::writeXml($output, array(
'ITEM_ID' => $item->getId(),
'EAN' => $item->getEan(),
'PRODUCTNAME' => $item->getProductName(),
'MANUFACTURER' => $item->getManufacturer(),
'DESCRIPTION' => $item->getDescription(),
'CATEGORYTEXT' => $item->getCategoryText(),
'URL' => $item->getUrl(),
Expand Down
47 changes: 47 additions & 0 deletions src/Feeds/Heureka/HeurekaItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,15 @@ class HeurekaItem implements IFeedItem
/** @var string|NULL */
private $url;

/** @var string|NULL */
private $ean;

/** @var string|NULL */
private $price;

/** @var string|NULL */
private $manufacturer;

/** @var string|NULL */
private $deliveryDate;

Expand Down Expand Up @@ -58,6 +64,27 @@ public function getId()
public function setId($id)
{
$this->id = $id;

return $this;
}


/**
* @return string|int|NULL
*/
public function getEan()
{
return $this->ean;
}


/**
* @param string|int
* @return self
*/
public function setEan($ean)
{
$this->ean = $ean;
return $this;
}

Expand Down Expand Up @@ -187,6 +214,26 @@ public function setPrice($price)
}


/**
* @return string|NULL
*/
public function getManufacturer()
{
return $this->manufacturer;
}


/**
* @param string|float|int
* @return self
*/
public function setManufacturer($manufacturer)
{
$this->manufacturer = $manufacturer;
return $this;
}


/**
* @return HeurekaItemParameter[]
*/
Expand Down
56 changes: 56 additions & 0 deletions src/Feeds/Sitemap/SitemapFeed.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

namespace Inteve\FeedGenerator\Feeds\Sitemap;

use Inteve\FeedGenerator\Feed;
use Inteve\FeedGenerator\InvalidItemException;
use Inteve\FeedGenerator\IOutput;
use Inteve\FeedGenerator\Utils\Helpers;


class SitemapFeed extends Feed
{
/**
* @return string
*/
public function getContentType()
{
return 'text/xml';
}


/**
* @return void
* @throws InvalidItemException
*/
public function generate(IOutput $output)
{
$output->open();
$output->output('<?xml version="1.0" encoding="utf-8"?>');
$output->output("\n");
$output->output('<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">');
$output->output("\n");

foreach ($this->items as $item) {
if (!($item instanceof SitemapItem)) {
throw new InvalidItemException('Feed item must be instance of SitemapItem.');
}

$item->validate();

$output->output("<url>\n");

Helpers::writeXml($output, array(
'loc' => $item->getLocation(),
'lastmod' => $item->getLastModified(),
'priority' => $item->getPriority()
));

$output->output("</url>\n");
}

$output->output('</urlset>');
$output->output("\n");
$output->close();
}
}
102 changes: 102 additions & 0 deletions src/Feeds/Sitemap/SitemapItem.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<?php

namespace Inteve\FeedGenerator\Feeds\Sitemap;

use Inteve\FeedGenerator\AssertException;
use Inteve\FeedGenerator\IFeedItem;
use Inteve\FeedGenerator\Utils\Helpers;
use Nette\Utils\Validators;


class SitemapItem implements IFeedItem
{

/** @var string|int|NULL */
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Opravdu může být location typu int?

private $location;

/** @var string|NULL */
private $lastModified;

/** @var string|NULL */
private $priority;


/**
* @return string|int|NULL
*/
public function getLocation()
{
return $this->location;
}


/**
* @param string|int
* @return self
*/
public function setLocation($location)
{
$this->location = $location;
return $this;
}


/**
* @return string|NULL
*/
public function getLastModified()
{
return $this->lastModified;
}


/**
* @param string
* @return self
*/
public function setLastModified($lastModified)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Co u lastModified místo stringu vyžadovat DateTimeInterface?

{
$this->lastModified = $lastModified;
return $this;
}


/**
* @return string|NULL
*/
public function getPriority()
{
return $this->priority;
}


/**
* @param string
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Priorita by IMHO mohla akceptovat kromě stringu i float (resp. number)

* @return self
*/
public function setPriority($prioritys)
{
$this->priority = $prioritys;
return $this;
}

/**
* @return void
* @throws AssertException
*/
public function validate()
{
Helpers::assert(isset($this->location), 'Missing item location, call $item->setLocation().');
Helpers::assert(isset($this->lastModified), 'Missing item last modified date, call $item->setLastModified().');
Helpers::assert(isset($this->priority), 'Missing item priority, call $item->setPriority().');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Asi bych nevyžadoval nastavení lastModified a priority - podle https://www.sitemaps.org/protocol.html jsou tyto položky nepovinné. A u priority by asi bylo fajn kontrolovat jestli je hodnota (pokud je nastavena) ve správném rozsahu (od 0.0 do 1.0).

}


/**
* @return static
*/
public static function create()
{
return new static;
}
}
Loading