forked from limion/YmlGenerator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
YmlGenerator.php
244 lines (214 loc) · 7.8 KB
/
YmlGenerator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
<?php
/**
* YmlGenerator class file.
*
* @author Vladislav Holovko <vlad.holovko@gmail.com>
* @link http://www.yiiframework.com/extensions/
* @copyright Copyright © 2013
* @license http://www.yiiframework.com/license/
*
* Inspired by http://p2k.ru/archives/52
*/
/**
* YmlGenerator class is a Yandex.Market YML generator.
* Supports output to a file or browser.
* Uses XMLWriter as xml engine.
*
* For generating xml data you need to create your own generator class and implement four methods:
* - shopInfo()
* - currencies()
* - categories()
* - offers()
*
* For example:
* class MyYmlGenerator extends YmlGenerator {
* protected function shopInfo() {...}
* protected function currencies() {...}
* protected function categories() {...}
* protected function offers() {...}
* }
* How to implement these methods see the sample class MyYmlGenerator and definition of add* methods below
*/
abstract class YmlGenerator extends CApplicationComponent {
/**
* Xml file encoding
* @var string
*/
public $encoding = 'windows-1251';
/**
* Output file name. If null 'php://output' is used.
* @var string
*/
public $outputFile;
/**
* Indent string in xml file. False or null means no indent;
* @var string
*/
public $indentString = "\t";
/**
* An array of element names used to describe your web-shop according to YML standart
* @var array
*/
public $shopInfoElements = array('name','company','url','platform','version','agency','email');
/**
* An array of element names used to create an offer according to YML standart
* @var array
*/
public $offerElements = array('url', 'price', 'currencyId', 'categoryId', 'market_category',
'picture', 'store', 'pickup', 'delivery', 'local_delivery_cost','typePrefix',
'vendor', 'vendorCode', 'name' ,'model', 'description', 'sales_notes', 'manufacturer_warranty',
'seller_warranty','country_of_origin', 'downloadable', 'age','barcode','cpa',
'rec','expiry','weight','dimensions','param');
protected $_dir;
protected $_file;
protected $_tmpFile;
protected $_engine;
public function run() {
$this->beforeWrite();
$this->writeShopInfo();
$this->writeCurrencies();
$this->writeCategories();
$this->writeOffers();
$this->afterWrite();
}
protected function beforeWrite() {
if ($this->outputFile !== null) {
$slashPos = strrpos($this->outputFile, DIRECTORY_SEPARATOR);
if (false !== $slashPos) {
$this->_file = substr($this->outputFile, $slashPos);
$this->_dir = substr($this->outputFile, 0, $slashPos);
}
else {
$this->_dir = ".";
}
$this->_tmpFile = $this->_dir.DIRECTORY_SEPARATOR.md5($this->_file);
}
else {
$this->_tmpFile = 'php://output';
}
$engine = $this->getEngine();
$engine->openURI($this->_tmpFile);
if ($this->indentString) {
$engine->setIndentString($this->indentString);
$engine->setIndent(true);
}
$engine->startDocument('1.0',$this->encoding);
$engine->startElement('yml_catalog');
$engine->writeAttribute('date', date('Y-m-d H:i'));
$engine->startElement('shop');
}
protected function afterWrite() {
$engine = $this->getEngine();
$engine->fullEndElement();
$engine->fullEndElement();
$engine->endDocument();
if (null !== $this->outputFile)
rename($this->_tmpFile, $this->outputFile);
}
protected function getEngine() {
if (null === $this->_engine) {
$this->_engine = new XMLWriter();
}
return $this->_engine;
}
protected function writeShopInfo() {
$engine = $this->getEngine();
foreach($this->shopInfo() as $elm=>$text) {
if (in_array($elm,$this->shopInfoElements)) {
$engine->writeElement($elm, $text);
}
}
}
protected function writeCurrencies() {
$engine = $this->getEngine();
$engine->startElement('currencies');
$this->currencies();
$engine->fullEndElement();
}
protected function writeCategories() {
$engine = $this->getEngine();
$engine->startElement('categories');
$this->categories();
$engine->fullEndElement();
}
protected function writeOffers() {
$engine = $this->getEngine();
$engine->startElement('offers');
$this->offers();
$engine->fullEndElement();
}
/**
* Adds <currency> element. (See http://help.yandex.ru/partnermarket/currencies.xml)
* @param string $id "id" attribute
* @param mixed $rate "rate" attribute
*/
protected function addCurrency($id,$rate = 1) {
$engine = $this->getEngine();
$engine->startElement('currency');
$engine->writeAttribute('id', $id);
$engine->writeAttribute('rate', $rate);
$engine->endElement();
}
/**
* Adds <category> element. (See http://help.yandex.ru/partnermarket/categories.xml)
* @param string $name category name
* @param int $id "id" attribute
* @param int $parentId "parentId" attribute
*/
protected function addCategory($name,$id,$parentId = null) {
$engine = $this->getEngine();
$engine->startElement('category');
$engine->writeAttribute('id', $id);
if ($parentId)
$engine->writeAttribute('parentId', $parentId);
$engine->text($name);
$engine->fullEndElement();
}
/**
* Adds <offer> element. (See http://help.yandex.ru/partnermarket/offers.xml)
* @param int $id "id" attribute
* @param array $data array of subelements as elementName=>value pairs
* @param array $params array of <param> elements. Every element is an array: array(NAME,UNIT,VALUE) (See http://help.yandex.ru/partnermarket/param.xml)
* @param boolean $available "available" attribute
* @param string $type "type" attribute
* @param numeric $bid "bid" attribute
* @param numeric $cbid "cbid" attribute
*/
protected function addOffer($id,$data, $params = array(), $available=true, $type = 'vendor.model', $bid = null, $cbid = null) {
$engine = $this->getEngine();
$engine->startElement('offer');
$engine->writeAttribute('id', $id);
if ($type)
$engine->writeAttribute('type', $type);
$engine->writeAttribute('available', $available ? 'true' : 'false');
if ($bid) {
$engine->writeAttribute('bid', $bid);
if ($cbid)
$engine->writeAttribute('cbid', $cbid);
}
foreach($data as $elm=>$val) {
if (in_array($elm,$this->offerElements)) {
if (!is_array($val)) {
$val = array($val);
}
foreach($val as $value) {
$engine->writeElement($elm, $value);
}
}
}
foreach($params as $param) {
$engine->startElement('param');
$engine->writeAttribute('name', $param[0]);
if ($param[1])
$engine->writeAttribute('unit', $param[1]);
$engine->text($param[2]);
$engine->endElement();
}
$engine->fullEndElement();
}
/* Methods that must be implemented in your custom derived class */
abstract protected function shopInfo();
abstract protected function currencies();
abstract protected function categories();
abstract protected function offers();
}