Skip to content
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

Add Promos #52

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Add Promos
  • Loading branch information
Roman authored and Roman committed Sep 4, 2024
commit a3d00e6d65da690003fc4b28e0b6809149916934
187 changes: 115 additions & 72 deletions src/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,27 @@
use Bukashk0zzz\YmlGenerator\Model\Offer\OfferInterface;
use Bukashk0zzz\YmlGenerator\Model\Offer\OfferOutlet;
use Bukashk0zzz\YmlGenerator\Model\Offer\OfferParam;
use Bukashk0zzz\YmlGenerator\Model\Promo;
use Bukashk0zzz\YmlGenerator\Model\ShopInfo;

use function copy;
use function date;

use Exception;

use function is_array;
use function is_bool;

use LogicException;
use RuntimeException;

use function sprintf;
use function sys_get_temp_dir;
use function tempnam;
use function unlink;

use XMLWriter;

/**
* Class Generator
*/
Expand All @@ -32,7 +51,7 @@ class Generator
protected $tmpFile;

/**
* @var \XMLWriter
* @var XMLWriter
*/
protected $writer;

Expand All @@ -44,21 +63,21 @@ class Generator
/**
* Generator constructor.
*
* @param Settings $settings
* @param Settings $settings
*/
public function __construct($settings = null)
{
$this->settings = $settings instanceof Settings ? $settings : new Settings();
$this->writer = new \XMLWriter();
$this->settings = $settings instanceof Settings ? $settings : new Settings;
$this->writer = new XMLWriter;

if ($this->settings->getOutputFile() !== null && $this->settings->getReturnResultYMLString()) {
throw new \LogicException('Only one destination need to be used ReturnResultYMLString or OutputFile');
throw new LogicException('Only one destination need to be used ReturnResultYMLString or OutputFile');
}

if ($this->settings->getReturnResultYMLString()) {
$this->writer->openMemory();
} else {
$this->tmpFile = $this->settings->getOutputFile() !== null ? \tempnam(\sys_get_temp_dir(), 'YMLGenerator') : 'php://output';
$this->tmpFile = $this->settings->getOutputFile() !== null ? tempnam(sys_get_temp_dir(), 'YMLGenerator') : 'php://output';
$this->writer->openURI($this->tmpFile);
}

Expand All @@ -69,15 +88,9 @@ public function __construct($settings = null)
}

/**
* @param ShopInfo $shopInfo
* @param array $currencies
* @param array $categories
* @param array $offers
* @param array $deliveries
*
* @return bool
*/
public function generate(ShopInfo $shopInfo, array $currencies, array $categories, array $offers, array $deliveries = [])
public function generate(ShopInfo $shopInfo, array $currencies, array $categories, array $offers, array $deliveries = [], array $promos = [])
{
try {
$this->addHeader();
Expand All @@ -90,21 +103,25 @@ public function generate(ShopInfo $shopInfo, array $currencies, array $categorie
$this->addDeliveries($deliveries);
}

if (\count($promos) !== 0) {
$this->addPromos($promos);
}

$this->addOffers($offers);
$this->addFooter();

if ($this->settings->getReturnResultYMLString()) {
return $this->writer->flush();
}

if (null !== $this->settings->getOutputFile()) {
\copy($this->tmpFile, $this->settings->getOutputFile());
@\unlink($this->tmpFile);
if ($this->settings->getOutputFile() !== null) {
copy($this->tmpFile, $this->settings->getOutputFile());
@unlink($this->tmpFile);
}

return true;
} catch (\Exception $exception) {
throw new \RuntimeException(\sprintf('Problem with generating YML file: %s', $exception->getMessage()), 0, $exception);
} catch (Exception $exception) {
throw new RuntimeException(sprintf('Problem with generating YML file: %s', $exception->getMessage()), 0, $exception);
}
}

Expand All @@ -117,7 +134,7 @@ protected function addHeader()
$this->writer->startDTD('yml_catalog', null, 'shops.dtd');
$this->writer->endDTD();
$this->writer->startElement('yml_catalog');
$this->writer->writeAttribute('date', \date(DATE_RFC3339));
$this->writer->writeAttribute('date', date(DATE_RFC3339));
$this->writer->startElement('shop');
}

Expand All @@ -133,8 +150,6 @@ protected function addFooter()

/**
* Adds shop element data. (See https://yandex.ru/support/webmaster/goods-prices/technical-requirements.xml#shop)
*
* @param ShopInfo $shopInfo
*/
protected function addShopInfo(ShopInfo $shopInfo)
{
Expand All @@ -145,9 +160,6 @@ protected function addShopInfo(ShopInfo $shopInfo)
}
}

/**
* @param Currency $currency
*/
protected function addCurrency(Currency $currency)
{
$this->writer->startElement('currency');
Expand All @@ -156,9 +168,6 @@ protected function addCurrency(Currency $currency)
$this->writer->endElement();
}

/**
* @param Category $category
*/
protected function addCategory(Category $category)
{
$this->writer->startElement('category');
Expand All @@ -168,7 +177,7 @@ protected function addCategory(Category $category)
$this->writer->writeAttribute('parentId', $category->getParentId());
}

if (!empty($category->getAttributes())) {
if (! empty($category->getAttributes())) {
foreach ($category->getAttributes() as $attribute) {
$this->writer->writeAttribute($attribute['name'], $attribute['value']);
}
Expand All @@ -178,9 +187,6 @@ protected function addCategory(Category $category)
$this->writer->fullEndElement();
}

/**
* @param Delivery $delivery
*/
protected function addDelivery(Delivery $delivery)
{
$this->writer->startElement('option');
Expand All @@ -192,9 +198,50 @@ protected function addDelivery(Delivery $delivery)
$this->writer->endElement();
}

/**
* @param OfferInterface $offer
*/
protected function addPromo(Promo $promo)
{
$this->writer->startElement('promo');
$this->writer->writeAttribute('id', $promo->getId());
$this->writer->writeAttribute('type', $promo->getType());

$this->writer->writeElement('start-date', $promo->getStartDate());
$this->writer->writeElement('end-date', $promo->getEndDate());
$this->writer->writeElement('promo-code', $promo->getPromocode());

$discount = $promo->getDiscount();

$this->writer->startElement('discount');

if (isset($discount['unit']) && $discount['unit'] === 'percent') {

if ($discount['value'] < 5 || $discount['value'] > 95) {
throw new Exception('Percent should be 5-95', 1);
}

$this->writer->writeAttribute('unit', $discount['unit']);
} else {
$this->writer->writeAttribute('unit', $discount['unit']);

if (isset($discount['currency'])) {
$this->writer->writeAttribute('currency', $discount['currency']);
}
}

$this->writer->text($discount['value']);
$this->writer->endElement();
$this->writer->writeElement('url', $promo->getUrl());
$this->writer->startElement('purchase');

collect($promo->getPurchase())->each(function ($purchase) {
$this->writer->startElement('product');
$this->writer->writeAttribute('offer-id', $purchase);
$this->writer->endElement();
});

$this->writer->endElement();
$this->writer->endElement();
}

protected function addOffer(OfferInterface $offer)
{
$this->writer->startElement('offer');
Expand All @@ -210,7 +257,7 @@ protected function addOffer(OfferInterface $offer)
}

foreach ($offer->toArray() as $name => $value) {
if (\is_array($value)) {
if (is_array($value)) {
foreach ($value as $itemValue) {
$this->addOfferElement($name, $itemValue);
}
Expand All @@ -228,8 +275,6 @@ protected function addOffer(OfferInterface $offer)

/**
* Adds <currencies> element. (See https://yandex.ru/support/webmaster/goods-prices/technical-requirements.xml#currencies)
*
* @param array $currencies
*/
private function addCurrencies(array $currencies)
{
Expand All @@ -247,8 +292,6 @@ private function addCurrencies(array $currencies)

/**
* Adds <categories> element. (See https://yandex.ru/support/webmaster/goods-prices/technical-requirements.xml#categories)
*
* @param array $categories
*/
private function addCategories(array $categories)
{
Expand All @@ -266,8 +309,6 @@ private function addCategories(array $categories)

/**
* Adds <delivery-option> element. (See https://yandex.ru/support/partnermarket/elements/delivery-options.xml)
*
* @param array $deliveries
*/
private function addDeliveries(array $deliveries)
{
Expand All @@ -283,10 +324,25 @@ private function addDeliveries(array $deliveries)
$this->writer->fullEndElement();
}

/**
* Adds <promos> element. (See https://yandex.ru/support2/products/ru/promo-common)
*/
private function addPromos(array $promos)
{
$this->writer->startElement('promos');

/** @var Promo $promo */
foreach ($promos as $promo) {
if ($promo instanceof Promo) {
$this->addPromo($promo);
}
}

$this->writer->fullEndElement();
}

/**
* Adds <offers> element. (See https://yandex.ru/support/webmaster/goods-prices/technical-requirements.xml#offers)
*
* @param array $offers
*/
private function addOffers(array $offers)
{
Expand All @@ -302,20 +358,14 @@ private function addOffers(array $offers)
$this->writer->fullEndElement();
}

/**
* @param OfferInterface $offer
*/
private function addOfferDeliveryOptions(OfferInterface $offer)
{
$options = $offer->getDeliveryOptions();
if (!empty($options)) {
if (! empty($options)) {
$this->addDeliveries($options);
}
}

/**
* @param OfferInterface $offer
*/
private function addOfferParams(OfferInterface $offer)
{
/** @var OfferParam $param */
Expand All @@ -334,31 +384,25 @@ private function addOfferParams(OfferInterface $offer)
}
}

/**
* @param OfferInterface $offer
*/
private function addOfferOutlets(OfferInterface $offer)
{
if ($offer->getOutlets() && sizeof($offer->getOutlets())) {
$this->writer->startElement('outlets');
/** @var OfferOutlet $outlet */
foreach ($offer->getOutlets() as $outlet) {
if ($outlet instanceof OfferOutlet) {
$this->writer->startElement('outlet');
if ($offer->getOutlets() && count($offer->getOutlets())) {
$this->writer->startElement('outlets');
/** @var OfferOutlet $outlet */
foreach ($offer->getOutlets() as $outlet) {
if ($outlet instanceof OfferOutlet) {
$this->writer->startElement('outlet');

$this->writer->writeAttribute('id', $outlet->getId());
$this->writer->writeAttribute('instock', $outlet->getInStock());
$this->writer->writeAttribute('id', $outlet->getId());
$this->writer->writeAttribute('instock', $outlet->getInStock());

$this->writer->endElement();
}
$this->writer->endElement();
}
}
$this->writer->fullEndElement();
}
$this->writer->fullEndElement();
}
}

/**
* @param OfferInterface $offer
*/
private function addOfferCondition(OfferInterface $offer)
{
$params = $offer->getCondition();
Expand All @@ -371,9 +415,8 @@ private function addOfferCondition(OfferInterface $offer)
}

/**
* @param string $name
* @param mixed $value
*
* @param string $name
* @param mixed $value
* @return bool
*/
private function addOfferElement($name, $value)
Expand All @@ -390,7 +433,7 @@ private function addOfferElement($name, $value)
return true;
}

if (\is_bool($value)) {
if (is_bool($value)) {
$value = $value ? 'true' : 'false';
}
$this->writer->writeElement($name, $value);
Expand Down
Loading