Skip to content

Commit 130fd0d

Browse files
authored
Add sets for performers (#54)
* add Sets * add set to generate function * add set to generate function * fix variable type * fix set-ids * Update doc comment - add sets variable * fix code style * update tests
1 parent 78107aa commit 130fd0d

File tree

14 files changed

+304
-12
lines changed

14 files changed

+304
-12
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,14 @@ $categories[] = (new Category())
7878
->setName($this->faker->name)
7979
;
8080

81+
// Creating sets array
82+
$categories = [];
83+
$categories[] = (new Set())
84+
->setId('s1')
85+
->setName('Set name')
86+
->setUrl('https://google.com')
87+
;
88+
8189
// Creating offers array (https://yandex.ru/support/webmaster/goods-prices/technical-requirements.xml#offers)
8290
$offers = [];
8391
$offers[] = (new OfferSimple())

src/Generator.php

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use Bukashk0zzz\YmlGenerator\Model\Offer\OfferInterface;
2020
use Bukashk0zzz\YmlGenerator\Model\Offer\OfferOutlet;
2121
use Bukashk0zzz\YmlGenerator\Model\Offer\OfferParam;
22+
use Bukashk0zzz\YmlGenerator\Model\Set;
2223
use Bukashk0zzz\YmlGenerator\Model\ShopInfo;
2324

2425
/**
@@ -74,10 +75,11 @@ public function __construct($settings = null)
7475
* @param iterable $categories
7576
* @param iterable $offers
7677
* @param iterable $deliveries
78+
* @param iterable $sets
7779
*
7880
* @return bool
7981
*/
80-
public function generate(ShopInfo $shopInfo, iterable $currencies, iterable $categories, iterable $offers, iterable $deliveries = [])
82+
public function generate(ShopInfo $shopInfo, iterable $currencies, iterable $categories, iterable $offers, iterable $deliveries = [], iterable $sets = [])
8183
{
8284
try {
8385
$this->addHeader();
@@ -86,6 +88,10 @@ public function generate(ShopInfo $shopInfo, iterable $currencies, iterable $cat
8688
$this->addCurrencies($currencies);
8789
$this->addCategories($categories);
8890

91+
if (\count($sets) !== 0) {
92+
$this->addSets($sets);
93+
}
94+
8995
if (\count($deliveries) !== 0) {
9096
$this->addDeliveries($deliveries);
9197
}
@@ -156,6 +162,25 @@ protected function addCurrency(Currency $currency)
156162
$this->writer->endElement();
157163
}
158164

165+
/**
166+
* @param Set $set
167+
*/
168+
protected function addSet(Set $set)
169+
{
170+
$this->writer->startElement('set');
171+
$this->writer->writeAttribute('id', $set->getId());
172+
173+
if ($set->getName() !== null) {
174+
$this->writer->writeElement('name', $set->getName());
175+
}
176+
177+
if ($set->getUrl() !== null) {
178+
$this->writer->writeElement('url', $set->getUrl());
179+
}
180+
181+
$this->writer->fullEndElement();
182+
}
183+
159184
/**
160185
* @param Category $category
161186
*/
@@ -245,6 +270,25 @@ private function addCurrencies(iterable $currencies)
245270
$this->writer->fullEndElement();
246271
}
247272

273+
/**
274+
* Adds <sets> element.
275+
*
276+
* @param iterable $sets
277+
*/
278+
private function addSets(iterable $sets)
279+
{
280+
$this->writer->startElement('sets');
281+
282+
/** @var Set $set */
283+
foreach ($sets as $set) {
284+
if ($set instanceof Set) {
285+
$this->addSet($set);
286+
}
287+
}
288+
289+
$this->writer->fullEndElement();
290+
}
291+
248292
/**
249293
* Adds <categories> element. (See https://yandex.ru/support/webmaster/goods-prices/technical-requirements.xml#categories)
250294
*

src/Model/Offer/AbstractOffer.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,16 @@ abstract class AbstractOffer implements OfferInterface
6363
*/
6464
private $categoriesId = [];
6565

66+
/**
67+
* @var string
68+
*/
69+
private $setId;
70+
71+
/**
72+
* @var array
73+
*/
74+
private $setsId = [];
75+
6676
/**
6777
* @var string
6878
*/
@@ -329,6 +339,46 @@ public function setCurrencyId($currencyId)
329339
return $this;
330340
}
331341

342+
/**
343+
* @return string
344+
*/
345+
public function getSetId()
346+
{
347+
return $this->setId;
348+
}
349+
350+
/**
351+
* @param string $setId
352+
*
353+
* @return $this
354+
*/
355+
public function setSetId($setId)
356+
{
357+
$this->setId = $setId;
358+
359+
return $this;
360+
}
361+
362+
/**
363+
* @return array
364+
*/
365+
public function getSetsId()
366+
{
367+
return $this->setsId;
368+
}
369+
370+
/**
371+
* @param array $setsId
372+
*
373+
* @return $this
374+
*/
375+
public function setSetsId(array $setsId)
376+
{
377+
$this->setsId = $setsId;
378+
379+
return $this;
380+
}
381+
332382
/**
333383
* @return int
334384
*/
@@ -927,6 +977,11 @@ abstract protected function getOptions();
927977
*/
928978
private function getHeaderOptions()
929979
{
980+
$sets = \array_unique(\array_merge(
981+
!empty($this->getSetId()) ? [$this->getSetId()] : [],
982+
$this->getSetsId()
983+
));
984+
930985
return [
931986
'url' => $this->getUrl(),
932987
'price' => $this->getPrice(),
@@ -937,6 +992,7 @@ private function getHeaderOptions()
937992
[$this->getCategoryId()],
938993
$this->getCategoriesId()
939994
),
995+
'set-ids' => empty($sets) ? null : \implode(',', $sets),
940996
'market_category' => $this->getMarketCategory(),
941997
'picture' => $this->getPictures(),
942998
'pickup' => $this->isPickup(),

src/Model/Set.php

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Bukashk0zzzYmlGenerator
5+
*
6+
* (c) Denis Golubovskiy <bukashk0zzz@gmail.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Bukashk0zzz\YmlGenerator\Model;
13+
14+
/**
15+
* Class Set
16+
*/
17+
class Set
18+
{
19+
/**
20+
* @var string
21+
*/
22+
private $id;
23+
24+
/**
25+
* @var string
26+
*/
27+
private $name;
28+
29+
/**
30+
* @var string
31+
*/
32+
private $url;
33+
34+
/**
35+
* @return string
36+
*/
37+
public function getId()
38+
{
39+
return $this->id;
40+
}
41+
42+
/**
43+
* @param string $id
44+
*
45+
* @return Set
46+
*/
47+
public function setId($id)
48+
{
49+
$this->id = $id;
50+
51+
return $this;
52+
}
53+
54+
/**
55+
* @return string
56+
*/
57+
public function getUrl()
58+
{
59+
return $this->url;
60+
}
61+
62+
/**
63+
* @param string $url
64+
*
65+
* @return Set
66+
*/
67+
public function setUrl($url)
68+
{
69+
$this->url = $url;
70+
71+
return $this;
72+
}
73+
74+
/**
75+
* @return string
76+
*/
77+
public function getName()
78+
{
79+
return $this->name;
80+
}
81+
82+
/**
83+
* @param string $name
84+
*
85+
* @return Set
86+
*/
87+
public function setName($name)
88+
{
89+
$this->name = $name;
90+
91+
return $this;
92+
}
93+
}

tests/AbstractGeneratorTest.php

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Bukashk0zzz\YmlGenerator\Model\Category;
1616
use Bukashk0zzz\YmlGenerator\Model\Currency;
1717
use Bukashk0zzz\YmlGenerator\Model\Delivery;
18+
use Bukashk0zzz\YmlGenerator\Model\Set;
1819
use Bukashk0zzz\YmlGenerator\Model\ShopInfo;
1920
use Bukashk0zzz\YmlGenerator\Settings;
2021
use Faker\Factory as Faker;
@@ -45,6 +46,11 @@ abstract class AbstractGeneratorTest extends TestCase
4546
*/
4647
protected $currencies;
4748

49+
/**
50+
* @var array
51+
*/
52+
protected $sets;
53+
4854
/**
4955
* @var array
5056
*/
@@ -72,6 +78,7 @@ protected function setUp(): void
7278
$this->currencies = $this->createCurrencies();
7379
$this->categories = $this->createCategories();
7480
$this->deliveries = $this->createDeliveries();
81+
$this->sets = $this->createSets();
7582
}
7683

7784
/**
@@ -89,7 +96,8 @@ protected function generateFile()
8996
$this->currencies,
9097
$this->categories,
9198
$this->createOffers(),
92-
$this->deliveries
99+
$this->deliveries,
100+
$this->sets
93101
));
94102
}
95103

@@ -125,6 +133,7 @@ protected function createOffers()
125133
)
126134
->setCurrencyId('UAH')
127135
->setCategoryId($id)
136+
->setSetsId(['setId1', 'setId2'])
128137
->setDelivery($this->faker->boolean)
129138
->setLocalDeliveryCost($this->faker->numberBetween(1, 9999))
130139
->setDescription($this->faker->sentence)
@@ -213,6 +222,27 @@ private function createCurrencies()
213222
return $currencies;
214223
}
215224

225+
/**
226+
* @return array
227+
*/
228+
private function createSets()
229+
{
230+
$sets = [];
231+
$sets[] = (new Set())
232+
->setId('setId1')
233+
->setName($this->faker->name)
234+
->setUrl($this->faker->url)
235+
;
236+
237+
$sets[] = (new Set())
238+
->setId('setId2')
239+
->setName($this->faker->name)
240+
->setName($this->faker->url)
241+
;
242+
243+
return $sets;
244+
}
245+
216246
/**
217247
* @return array
218248
*/

tests/dtd/ArtistTitle.dtd

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<!ATTLIST yml_catalog
33
date CDATA #REQUIRED>
44

5-
<!ELEMENT shop (name, company, url, phone?, platform?, version?, agency?, email*, enable_auto_discounts?, currencies, categories, delivery-options, store?, pickup?, delivery?, deliveryIncluded?, local_delivery_cost?, adult?, offers)>
5+
<!ELEMENT shop (name, company, url, phone?, platform?, version?, agency?, email*, enable_auto_discounts?, currencies, categories, sets?, delivery-options, store?, pickup?, delivery?, deliveryIncluded?, local_delivery_cost?, adult?, offers)>
66
<!ELEMENT company (#PCDATA)>
77
<!ELEMENT phone (#PCDATA)>
88

@@ -13,6 +13,10 @@
1313
rate CDATA "1"
1414
plus CDATA "0">
1515

16+
<!ELEMENT sets (set+)>
17+
<!ELEMENT set (name?, url?)>
18+
<!ATTLIST set
19+
id CDATA #REQUIRED>
1620

1721
<!ELEMENT categories (category+)>
1822
<!ELEMENT category (#PCDATA)>
@@ -40,6 +44,7 @@
4044
currencyId,
4145
xCategory?,
4246
categoryId+,
47+
set-ids?,
4348
market_category?,
4449
delivery?,
4550
local_delivery_cost?,
@@ -143,6 +148,7 @@
143148
<!ELEMENT description (#PCDATA)>
144149
<!ELEMENT sales_notes (#PCDATA)>
145150
<!ELEMENT promo (#PCDATA)>
151+
<!ELEMENT set-ids (#PCDATA)>
146152
<!ELEMENT aliases (#PCDATA)>
147153
<!ELEMENT provider (#PCDATA)>
148154
<!ELEMENT tarifplan (#PCDATA)>

0 commit comments

Comments
 (0)