Skip to content

Commit

Permalink
Merge branch 'pr/12'
Browse files Browse the repository at this point in the history
  • Loading branch information
martinknor committed Jun 26, 2020
2 parents d189c9c + 1b8e603 commit 05a7d4e
Show file tree
Hide file tree
Showing 59 changed files with 4,274 additions and 117 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

/.idea/
11 changes: 6 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@
}
],
"require": {


"php": ">=5.6",
"nette/di": "~2.4.0",
"nette/utils": "~2.4.0",
"latte/latte": "~2.4.0",
"kdyby/console": "~2.6.0",
"sergiors/importing": "1.0.1"
"nette/di": "~2.4.0 || ^2.5.0 || ^3.0.0",
"nette/utils": "~2.4.0 || ^2.5.0 || ^3.0.0",
"latte/latte": "^2.4 || ~2.6.0",
"contributte/console": "^0.8.0"
},
"require-dev": {
"nette/tester": "@dev"
Expand Down
50 changes: 47 additions & 3 deletions src/Generators/BaseGenerator.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?php

namespace Mk\Feed\Generators;

use Latte\Engine;
use Mk\Feed\Storage;
use Nette\Object;
use Mk\Feed\FileEmptyException;
use Mk\Feed\ItemIncompletedException;

Expand All @@ -12,7 +12,10 @@
* @author Martin Knor <[email protected]>
* @package Mk\Feed\Generators
*/
abstract class BaseGenerator extends Object implements IGenerator {
abstract class BaseGenerator implements IGenerator {

/* Použití smartobject viz php 7.2 to nette 2.4 */
use \Nette\SmartObject;

/** @var bool true if some products added */
private $prepared = false;
Expand All @@ -23,6 +26,18 @@ abstract class BaseGenerator extends Object implements IGenerator {
/** @var \Mk\Feed\Storage */
private $storage;

/** @var array */
private $config;

/** @var string */
private $link;

/** @var string */
private $description;

/** @var string */
private $storeName;

/**
* BaseGenerator constructor.
* @param \Mk\Feed\Storage $storage
Expand All @@ -32,6 +47,30 @@ public function __construct(Storage $storage)
$this->storage = $storage;
}

/**
* @param string $link
*/
public function setLink(string $link): void {
$this->link = $link;
}

/**
* @param string $description
*/
public function setDescription(string $description): void {
$this->description = $description;
}

/**
* @param string $storeName
*/
public function setStoreName(string $storeName): void {
$this->storeName = $storeName;
}




/**
* @param $name
* @return string path to template
Expand All @@ -45,6 +84,7 @@ abstract protected function getTemplate($name);
protected function prepare()
{
$this->handle = tmpfile();

$this->prepareTemplate('header');
$this->prepared = true;
}
Expand Down Expand Up @@ -103,11 +143,15 @@ public function save($filename)
*/
protected function prepareTemplate($template)
{

$latte = new Engine;
$content = $latte->renderToString($this->getTemplate($template), array('storeName' => $this->storeName, 'description' => $this->description, 'link' => $this->link));
$file = $this->getTemplate($template);
$footerHandle = fopen('safe://' . $file, 'r');
$footer = fread($footerHandle, filesize($file));
fclose($footerHandle);
fwrite($this->handle, $footer);
fwrite($this->handle, $content);
// fwrite($this->handle, $footer);
}

}
8 changes: 6 additions & 2 deletions src/Generators/BaseItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,18 @@
* @author Martin Knor <[email protected]>
* @package Mk\Feed\Generators\Zbozi
*/
abstract class BaseItem extends Nette\Object implements Mk\Feed\Generators\IItem
abstract class BaseItem implements Mk\Feed\Generators\IItem
{

/* Použití smartobject viz php 7.2 to nette 2.4 */
use \Nette\SmartObject;
/**
* Validate item
* @return bool return true if item is valid
*/
public function validate() {
$reflection = $this->getReflection();

$reflection = new Nette\Reflection\ClassType(get_called_class());

foreach ($reflection->getProperties(\ReflectionProperty::IS_PUBLIC) as $v) {
if ($v->getAnnotation('required')) {
Expand All @@ -30,4 +33,5 @@ public function validate() {

return TRUE;
}

}
56 changes: 56 additions & 0 deletions src/Generators/Custom/CategoriesHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

namespace Mk\Feed\Generators\Custom;

use Nette\Caching\Cache;
use Nette\Caching\IStorage;

class CategoriesHelper {

CONST CATEGORY_URL = 'http://www.heureka.cz/direct/xml-export/shops/heureka-sekce.xml';

/** @var \Nette\Caching\Cache */
private $cache;

function __construct(IStorage $storage = null)
{
if ($storage) {
$this->cache = new Cache($storage, __CLASS__);
}
}

public function getCategories()
{
$categories = array();
if (!$this->cache || !($categories = $this->cache->load('categories'))) {
$xml = file_get_contents(self::CATEGORY_URL);
$dom = new \DOMDocument();

$dom->loadXML($xml);
$xpath = new \DOMXPath($dom);
/** @var \DOMElement[] $_categories */
$_categories = $xpath->query(".//CATEGORY");

foreach ($_categories as $category) {
$categoryIdElement = $xpath->query($category->getNodePath().'/CATEGORY_ID');
$id = isset($categoryIdElement[0]) ? (int)$categoryIdElement[0]->nodeValue : null;

$categoryFullNameElement = $xpath->query($category->getNodePath().'/CATEGORY_FULLNAME');
$_cat = isset($categoryFullNameElement[0]) ? (string)$categoryFullNameElement[0]->nodeValue : null;

if($id && $_cat) {
$_cat = str_replace('Heureka.cz | ', '', $_cat);
$categories[$id] = $_cat;
}
}

asort($categories);

if ($this->cache) {
$this->cache->save('categories', $categories);
}
}

return $categories;
}
}
116 changes: 116 additions & 0 deletions src/Generators/Custom/Delivery.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
<?php

namespace Mk\Feed\Generators\Custom;

use Mk, Nette;

/**
* Class Delivery
* @author Martin Knor <[email protected]>
* @package Mk\Feed\Generators\Heureka
*/
class Delivery{

/* Použití smartobject viz php 7.2 to nette 2.4 */
use \Nette\SmartObject;

CONST CESKA_POSTA = 'CESKA_POSTA',
CESKA_POSTA_NA_POSTU = 'CESKA_POSTA_NA_POSTU',
CSAD_LOGISTIK_OSTRAVA = 'CSAD_LOGISTIK_OSTRAVA',
DPD = 'DPD',
DHL = 'DHL',
DSV = 'DSV',
EMS = 'EMS',
FOFR = 'FOFR',
GEBRUDER_WEISS = 'GEBRUDER_WEISS',
GEIS = 'GEIS',
GENERAL_PARCEL = 'GENERAL_PARCEL',
GLS = 'GLS',
HDS = 'HDS',
HEUREKAPOINT = 'HEUREKAPOINT',
INTIME = 'INTIME',
PPL = 'PPL',
RADIALKA = 'RADIALKA',
SEEGMULLER = 'SEEGMULLER',
TNT = 'TNT',
TOPTRANS = 'TOPTRANS',
UPS = 'UPS',
ULOZENKA = 'ULOZENKA',
VLASTNI_PREPRAVA = 'VLASTNI_PREPRAVA',
ZASILKOVNA = 'ZASILKOVNA';

static $ids = array(
self::CESKA_POSTA,
self::CESKA_POSTA_NA_POSTU,
self::CSAD_LOGISTIK_OSTRAVA,
self::DPD,
self::DHL,
self::DSV,
self::EMS,
self::FOFR,
self::GEBRUDER_WEISS,
self::GEIS,
self::GENERAL_PARCEL,
self::GLS,
self::HDS,
self::HEUREKAPOINT,
self::INTIME,
self::PPL,
self::RADIALKA,
self::SEEGMULLER,
self::TNT,
self::TOPTRANS,
self::UPS,
self::ULOZENKA,
self::VLASTNI_PREPRAVA,
self::ZASILKOVNA,
);

/** @var string */
private $id;
/** @var float */
private $price;
/** @var float|null */
private $priceCod;

/**
* Delivery constructor.
* @param $id
* @param $price
* @param null $priceCod
*/
public function __construct($id, $price, $priceCod = null)
{
if (!in_array($id, self::$ids)) {
throw new \InvalidArgumentException("Delivery with id $id doesn\t exist");
}
$this->id = (string) $id;
$this->price = (float) $price;
$this->priceCod = isset($priceCod) ? (float) $priceCod : null;
}

/**
* @return string
*/
public function getId()
{
return $this->id;
}

/**
* @return float
*/
public function getPrice()
{
return $this->price;
}

/**
* @return float|null
*/
public function getPriceCod()
{
return $this->priceCod;
}

}
24 changes: 24 additions & 0 deletions src/Generators/Custom/Generator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php
namespace Mk\Feed\Generators\Custom;

use Mk\Feed\Generators\BaseGenerator;

/**
* Class HeurekaGenerator
* @author Martin Knor <[email protected]>
* @package Mk\Feed\Generators
* @see http://sluzby.heureka.cz/napoveda/xml-feed/ Documentation
*/
abstract class Generator extends BaseGenerator {

/**
* @param $name
* @return string
*/
protected function getTemplate($name)
{
$reflection = new \ReflectionClass(__CLASS__);
return dirname($reflection->getFileName()) . '/latte/' . $name . '.latte';
}

}
38 changes: 38 additions & 0 deletions src/Generators/Custom/Gift.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace Mk\Feed\Generators\Custom;

use Mk, Nette;

/**
* Class Gift
* @author Martin Knor <[email protected]>
* @package Mk\Feed\Generators\Heureka
*/
class Gift {

/* Použití smartobject viz php 7.2 to nette 2.4 */
use \Nette\SmartObject;

/** @var string */
protected $name;

/**
* @return string
*/
public function getName()
{
return $this->name;
}

/**
* Gift constructor.
* @param $name
*/
public function __construct($name)
{

$this->name = (string)$name;
}

}
Loading

0 comments on commit 05a7d4e

Please sign in to comment.