From a3d00e6d65da690003fc4b28e0b6809149916934 Mon Sep 17 00:00:00 2001 From: Roman <63189592+VitteWeb@users.noreply.github.com> Date: Wed, 4 Sep 2024 17:22:12 +0500 Subject: [PATCH 1/4] Add Promos --- src/Generator.php | 187 ++++++++++++++++++++++-------------- src/Model/Promo.php | 227 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 342 insertions(+), 72 deletions(-) create mode 100644 src/Model/Promo.php diff --git a/src/Generator.php b/src/Generator.php index 12749ad..7c13a92 100644 --- a/src/Generator.php +++ b/src/Generator.php @@ -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 */ @@ -32,7 +51,7 @@ class Generator protected $tmpFile; /** - * @var \XMLWriter + * @var XMLWriter */ protected $writer; @@ -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); } @@ -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(); @@ -90,6 +103,10 @@ public function generate(ShopInfo $shopInfo, array $currencies, array $categorie $this->addDeliveries($deliveries); } + if (\count($promos) !== 0) { + $this->addPromos($promos); + } + $this->addOffers($offers); $this->addFooter(); @@ -97,14 +114,14 @@ public function generate(ShopInfo $shopInfo, array $currencies, array $categorie 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); } } @@ -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'); } @@ -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) { @@ -145,9 +160,6 @@ protected function addShopInfo(ShopInfo $shopInfo) } } - /** - * @param Currency $currency - */ protected function addCurrency(Currency $currency) { $this->writer->startElement('currency'); @@ -156,9 +168,6 @@ protected function addCurrency(Currency $currency) $this->writer->endElement(); } - /** - * @param Category $category - */ protected function addCategory(Category $category) { $this->writer->startElement('category'); @@ -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']); } @@ -178,9 +187,6 @@ protected function addCategory(Category $category) $this->writer->fullEndElement(); } - /** - * @param Delivery $delivery - */ protected function addDelivery(Delivery $delivery) { $this->writer->startElement('option'); @@ -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'); @@ -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); } @@ -228,8 +275,6 @@ protected function addOffer(OfferInterface $offer) /** * Adds element. (See https://yandex.ru/support/webmaster/goods-prices/technical-requirements.xml#currencies) - * - * @param array $currencies */ private function addCurrencies(array $currencies) { @@ -247,8 +292,6 @@ private function addCurrencies(array $currencies) /** * Adds element. (See https://yandex.ru/support/webmaster/goods-prices/technical-requirements.xml#categories) - * - * @param array $categories */ private function addCategories(array $categories) { @@ -266,8 +309,6 @@ private function addCategories(array $categories) /** * Adds element. (See https://yandex.ru/support/partnermarket/elements/delivery-options.xml) - * - * @param array $deliveries */ private function addDeliveries(array $deliveries) { @@ -283,10 +324,25 @@ private function addDeliveries(array $deliveries) $this->writer->fullEndElement(); } + /** + * Adds 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 element. (See https://yandex.ru/support/webmaster/goods-prices/technical-requirements.xml#offers) - * - * @param array $offers */ private function addOffers(array $offers) { @@ -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 */ @@ -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(); @@ -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) @@ -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); diff --git a/src/Model/Promo.php b/src/Model/Promo.php new file mode 100644 index 0000000..9e4fe54 --- /dev/null +++ b/src/Model/Promo.php @@ -0,0 +1,227 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Bukashk0zzz\YmlGenerator\Model; + +use Illuminate\Support\Carbon; + +/** USE*/ +// $promos = collect([ +// (new Promo)->setId(1)->setType('promo code') +// ->setStartDate(now()) +// ->setEndDate(now()->addDays(2)) +// ->setPromocode('PROMOCODE') +// ->setUrl('https://google.com') +// ->setDiscount(['unit' => 'percent', 'value' => 5]) +// ->setPurchase([1, 2, 3]), +// ]); + +/** + * Class Promo + */ +class Promo +{ + /** + * @var string + */ + private $id; + + /** + * @var string + */ + private $type; + + /** + * @var string + */ + private $url; + + /** + * @var string + */ + private $startDate; + + /** + * @var string + */ + private $endDate; + + /** + * @var string + */ + private $promocode; + + /** + * @var array + */ + private $discount = ['unit' => 'percent', 'value' => 5]; + + /** + * @var array + */ + private $purchase; + + /** + * @return string + */ + public function getId() + { + return $this->id; + } + + /** + * @param string $id + * @return Promo + */ + public function setId($id) + { + $this->id = $id; + + return $this; + } + + /** + * @return string + */ + public function getType() + { + return $this->type; + } + + /** + * @param string $type + * @return Promo + */ + public function setType($type) + { + $this->type = $type; + + return $this; + } + + /** + * @return string + */ + public function getStartDate() + { + return $this->startDate; + } + + /** + * @param Carbon $startDate + * @return Promo + */ + public function setStartDate($startDate) + { + $this->startDate = $startDate->format('Y-m-d'); + + return $this; + } + + /** + * @return string + */ + public function getEndDate() + { + return $this->endDate; + } + + /** + * @param Carbon $endDate + * @return Promo + */ + public function setEndDate($endDate) + { + $this->endDate = $endDate->format('Y-m-d'); + + return $this; + } + + /** + * @return string + */ + public function getPromocode() + { + return $this->promocode; + } + + /** + * @param string $promocode + * @return Promo + */ + public function setPromocode($promocode) + { + $this->promocode = $promocode; + + return $this; + } + + /** + * @return string + */ + public function getUrl() + { + return $this->url; + } + + /** + * @param string $url + * @return Promo + */ + public function setUrl($url) + { + $this->url = $url; + + return $this; + } + + /** + * @return array + */ + public function getDiscount() + { + return $this->discount; + } + + /** + * $discount = ['unit' => 'percent' , "value" => 0-95] || ['unit' => 'currency' , 'currency'=> "RUB" , "value" => 0-95] + * + * @param array $discount + * @return Promo + */ + public function setDiscount($discount) + { + $this->discount = $discount; + + return $this; + } + + /** + * @return array + */ + public function getPurchase() + { + return $this->purchase; + } + + /** + * $purchase = [1 ,2 ,3 ] (Ids Offers) + * + * @param array $purchase + * @return Promo + */ + public function setPurchase($purchase) + { + $this->purchase = $purchase; + + return $this; + } +} From 9229b44231b57fc7da13f128ddcbdc78c76e2bd5 Mon Sep 17 00:00:00 2001 From: Roman <63189592+VitteWeb@users.noreply.github.com> Date: Wed, 4 Sep 2024 17:33:34 +0500 Subject: [PATCH 2/4] fix carbon --- src/Model/Promo.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Model/Promo.php b/src/Model/Promo.php index 9e4fe54..b5afe04 100644 --- a/src/Model/Promo.php +++ b/src/Model/Promo.php @@ -121,7 +121,7 @@ public function getStartDate() */ public function setStartDate($startDate) { - $this->startDate = $startDate->format('Y-m-d'); + $this->startDate = $startDate; return $this; } @@ -140,7 +140,7 @@ public function getEndDate() */ public function setEndDate($endDate) { - $this->endDate = $endDate->format('Y-m-d'); + $this->endDate = $endDate; return $this; } From 77dfa0070bd601c5303b1b1725d5035cb7c062fe Mon Sep 17 00:00:00 2001 From: Roman <63189592+VitteWeb@users.noreply.github.com> Date: Wed, 4 Sep 2024 17:37:02 +0500 Subject: [PATCH 3/4] add readme --- README.md | 34 ++++++++++++++++++++++++---------- 1 file changed, 24 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 9211f67..26f45e6 100644 --- a/README.md +++ b/README.md @@ -7,13 +7,14 @@ [![Latest Stable Version](https://img.shields.io/packagist/v/Bukashk0zzz/yml-generator.svg?style=flat-square)](https://packagist.org/packages/Bukashk0zzz/yml-generator) [![Total Downloads](https://img.shields.io/packagist/dt/Bukashk0zzz/yml-generator.svg?style=flat-square)](https://packagist.org/packages/Bukashk0zzz/yml-generator) -About ------ +## About + [YML (Yandex Market Language)](https://yandex.ru/support/webmaster/goods-prices/technical-requirements.xml) generator. -Uses standard XMLWriter for generating YML file. +Uses standard XMLWriter for generating YML file. Not required any other library you just need PHP 5.5.0 or >= version. Generator supports this offer types: + - OfferCustom [(vendor.model)](https://yandex.ru/support/webmaster/goods-prices/technical-requirements.xml#vendor-model) - OfferBook [(book)](https://yandex.ru/support/webmaster/goods-prices/technical-requirements.xml#book) - OfferAudiobook [(audiobook)](https://yandex.ru/support/webmaster/goods-prices/technical-requirements.xml#audiobook) @@ -22,15 +23,14 @@ Generator supports this offer types: - OfferEventTicket [(event-ticket)](https://yandex.ru/support/webmaster/goods-prices/technical-requirements.xml#event-ticket) - OfferSimple [(empty)](https://yandex.ru/support/webmaster/goods-prices/technical-requirements.xml#base) -Installation ------------- +## Installation + Run composer require ```bash composer require bukashk0zzz/yml-generator ``` - Or add this to your `composer.json` file: ```json @@ -39,8 +39,7 @@ Or add this to your `composer.json` file: } ``` -Usage examples -------------- +## Usage examples ```php setName($this->faker->name) ; +// Creating promos array (https://yandex.ru/support2/products/ru/promo-common) +$promos = []; +$promos[] = (new Promo()) + ->setId(1) + ->setType('promo code') + ->setStartDate(carbon('10-07-2024')) + ->setEndDate(carbon('10-07-2028')) + ->setPromocode('YANDEX') + ->setUrl("https://google.com") + ->setDiscount(['unit' => 'percent', 'value' => 5]) + ->setPurchase([1 ,2 ,3]), +; + // Creating offers array (https://yandex.ru/support/webmaster/goods-prices/technical-requirements.xml#offers) $offers = []; $offers[] = (new OfferSimple()) @@ -109,8 +121,11 @@ $deliveries[] = (new Delivery()) $deliveries ); ``` + ### Adding custom elements + if you need additional offers elements in your yml file using method addCustomElement('type','value'). For example: + ```php $offers[] = (new OfferSimple()) ->setId(12346) @@ -125,7 +140,6 @@ $offers[] = (new OfferSimple()) ; ``` -Copyright / License -------------------- +## Copyright / License See [LICENSE](https://github.com/bukashk0zzz/YmlGenerator/blob/master/LICENSE) From b01ece545699d4e7bf05408d23765d395f4e9075 Mon Sep 17 00:00:00 2001 From: Roman <63189592+VitteWeb@users.noreply.github.com> Date: Wed, 4 Sep 2024 17:53:25 +0500 Subject: [PATCH 4/4] fix type --- src/Model/Promo.php | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/Model/Promo.php b/src/Model/Promo.php index b5afe04..dd6347b 100644 --- a/src/Model/Promo.php +++ b/src/Model/Promo.php @@ -11,8 +11,6 @@ namespace Bukashk0zzz\YmlGenerator\Model; -use Illuminate\Support\Carbon; - /** USE*/ // $promos = collect([ // (new Promo)->setId(1)->setType('promo code') @@ -116,7 +114,7 @@ public function getStartDate() } /** - * @param Carbon $startDate + * @param string $startDate * @return Promo */ public function setStartDate($startDate) @@ -135,7 +133,7 @@ public function getEndDate() } /** - * @param Carbon $endDate + * @param string $endDate * @return Promo */ public function setEndDate($endDate)