1
0
mirror of synced 2025-02-16 04:03:16 +03:00

Add pagination handle to xml requests

This commit is contained in:
Chernyavtsev Ivan 2015-01-15 12:43:36 +03:00
parent 16c45753f5
commit 7f3a11c796

View File

@ -12,6 +12,11 @@ class MoySkladICMLParser
*/ */
const TIMEOUT = 20; const TIMEOUT = 20;
/**
* Шаг для выгрузки элементов в API
*/
const STEP = 1000;
/** /**
* Адрес для запроса товарных групп * Адрес для запроса товарных групп
*/ */
@ -155,42 +160,53 @@ class MoySkladICMLParser
$ignoreInfo = $this->getIgnoreProductGroupsInfo(); $ignoreInfo = $this->getIgnoreProductGroupsInfo();
$ignoreUuids = $ignoreInfo[self::UUIDS]; // сюда будут агрегироваться uuid для игнора с учетом вложеностей $ignoreUuids = $ignoreInfo[self::UUIDS]; // сюда будут агрегироваться uuid для игнора с учетом вложеностей
$xml = $this->requestXml(self::GROUP_LIST_URL); $start = 0;
$total = 0;
do {
$xml = $this->requestXml(self::GROUP_LIST_URL.'?'.http_build_query(['start' => $start]));
if ($xml) { if ($xml) {
foreach ($xml->goodFolder as $goodFolder) {
$uuid = (string) $goodFolder->uuid;
$externalCode = (string) $goodFolder->externalcode;
$parentUuid = isset($goodFolder[0]['parentUuid']) ?
(string) $goodFolder[0]['parentUuid'] : null;
// смотрим игноры $total = $xml[0]['total'];
if (in_array($uuid, $ignoreInfo[self::UUIDS])) {
continue; foreach ($xml->goodFolder as $goodFolder) {
} elseif (in_array($externalCode, $ignoreInfo[self::EXTERNAL_CODES])) { $uuid = (string) $goodFolder->uuid;
$ignoreUuids[] = $uuid; $externalCode = (string) $goodFolder->externalcode;
continue; $parentUuid = isset($goodFolder[0]['parentUuid']) ?
} elseif ( (string) $goodFolder[0]['parentUuid'] : null;
$parentUuid
&& in_array($parentUuid, $ignoreUuids) // смотрим игноры
) { if (in_array($uuid, $ignoreInfo[self::UUIDS])) {
$ignoreUuids[] = $uuid; continue;
continue; } elseif (in_array($externalCode, $ignoreInfo[self::EXTERNAL_CODES])) {
$ignoreUuids[] = $uuid;
continue;
} elseif (
$parentUuid
&& in_array($parentUuid, $ignoreUuids)
) {
$ignoreUuids[] = $uuid;
continue;
}
$category = array(
'uuid' => $uuid,
'name' => (string) $goodFolder[0]['name'],
'externalCode' => $externalCode,
);
if (isset($goodFolder[0]['parentUuid'])) {
$category['parentUuid'] = (string) $goodFolder[0]['parentUuid'];
}
$categories[$uuid] = $category;
} }
} else {
$category = array( throw new RuntimeException('No xml');
'uuid' => $uuid,
'name' => (string) $goodFolder[0]['name'],
'externalCode' => $externalCode,
);
if (isset($goodFolder[0]['parentUuid'])) {
$category['parentUuid'] = (string) $goodFolder[0]['parentUuid'];
}
$categories[$uuid] = $category;
} }
}
$start += self::STEP;
} while ($start < $total);
$result = array(); $result = array();
$this->sortGroupTree($result, $categories); $this->sortGroupTree($result, $categories);
@ -207,15 +223,25 @@ class MoySkladICMLParser
{ {
$vendors = array(); $vendors = array();
$xml = $this->requestXml(self::COMPANY_LIST_URL); $start = 0;
$total = 0;
do {
$xml = $this->requestXml(self::COMPANY_LIST_URL.'?'.http_build_query(['start' => $start]));
if ($xml) { if ($xml) {
foreach ($xml->company as $c) { $total = $xml[0]['total'];
$uuid = (string) $c->uuid;
$name = (string) $c[0]['name']; foreach ($xml->company as $c) {
$vendors[$uuid] = $name; $uuid = (string) $c->uuid;
$name = (string) $c[0]['name'];
$vendors[$uuid] = $name;
}
} else {
throw new RuntimeException('No xml');
} }
}
$start += self::STEP;
} while ($start < $total);
return $vendors; return $vendors;
} }
@ -233,58 +259,78 @@ class MoySkladICMLParser
) { ) {
$products = array(); $products = array();
$xml = $this->requestXml(self::PRODUCT_LIST_URL); $start = 0;
if ($xml) { $total = 0;
foreach ($xml->good as $v) { do {
$xml = $this->requestXml(self::PRODUCT_LIST_URL.'?'.http_build_query(['start' => $start]));
$parentUuid = isset($v[0]['parentUuid']) ?
(string) $v[0]['parentUuid'] : null;
$categoryId = $parentUuid && isset($categories[$parentUuid]) ?
$categories[$parentUuid]['externalCode'] : '';
$vendorUuid = isset($v[0]['supplierUuid']) ?
(string) $v[0]['supplierUuid'] : null;
$uuid = (string) $v->uuid;
$exCode = (string) $v->externalcode;
$products[$uuid] = array(
'id' => $exCode, // тут либо externalcode либо uuid товара
'exCode' => $exCode, // сюда пишем externalcode
'name' => (string) $v[0]['name'],
'price' => ((int) $v[0]['salePrice']) / 100,
'purchasePrice' => ((int) $v[0]['buyPrice']) / 100,
'article' => (string) $v[0]['productCode'],
'vendor' => $vendorUuid && isset($vendors[$vendorUuid]) ?
$vendors[$vendorUuid] : '',
'categoryId' => $categoryId,
'offers' => array(),
);
}
}
if (!$this->isIgnoreOffers()) {
$xml = $this->requestXml(self::OFFER_LIST_URL);
if ($xml) { if ($xml) {
foreach ($xml->consignment as $c) { $total = $xml[0]['total'];
// если нет feature, то товар без торговых предложений
if (!isset($c->feature)) {
continue;
}
$exCode = (string)$c->feature->externalcode; foreach ($xml->good as $v) {
$name = (string)$c[0]['name'];
$pid = (string)$c[0]['goodUuid'];
if (isset($products[$pid])) { $parentUuid = isset($v[0]['parentUuid']) ?
$products[$pid]['offers'][$exCode] = array( (string) $v[0]['parentUuid'] : null;
'id' => $products[$pid]['exCode'] . '#' . $exCode, $categoryId = $parentUuid && isset($categories[$parentUuid]) ?
'name' => $name, $categories[$parentUuid]['externalCode'] : '';
); $vendorUuid = isset($v[0]['supplierUuid']) ?
} else { (string) $v[0]['supplierUuid'] : null;
// иначе это не товар а услуга (service)
$uuid = (string) $v->uuid;
$exCode = (string) $v->externalcode;
$products[$uuid] = array(
'id' => $exCode, // тут либо externalcode либо uuid товара
'exCode' => $exCode, // сюда пишем externalcode
'name' => (string) $v[0]['name'],
'price' => ((int) $v[0]['salePrice']) / 100,
'purchasePrice' => ((int) $v[0]['buyPrice']) / 100,
'article' => (string) $v[0]['productCode'],
'vendor' => $vendorUuid && isset($vendors[$vendorUuid]) ?
$vendors[$vendorUuid] : '',
'categoryId' => $categoryId,
'offers' => array(),
);
}
} else {
throw new RuntimeException('No xml');
}
$start += self::STEP;
} while ($start < $total);
$start = 0;
$total = 0;
do {
if (!$this->isIgnoreOffers()) {
$xml = $this->requestXml(self::OFFER_LIST_URL.'?'.http_build_query(['start' => $start]));
if ($xml) {
$total = $xml[0]['total'];
foreach ($xml->consignment as $c) {
// если нет feature, то товар без торговых предложений
if (!isset($c->feature)) {
continue;
}
$exCode = (string)$c->feature->externalcode;
$name = (string)$c[0]['name'];
$pid = (string)$c[0]['goodUuid'];
if (isset($products[$pid])) {
$products[$pid]['offers'][$exCode] = array(
'id' => $products[$pid]['exCode'] . '#' . $exCode,
'name' => $name,
);
} else {
// иначе это не товар а услуга (service)
}
} }
} else {
throw new RuntimeException('No xml');
} }
} }
}
$start += self::STEP;
} while ($start < $total);
// для товаров без торговых преложений // для товаров без торговых преложений
foreach ($products as $key1 => &$product) { foreach ($products as $key1 => &$product) {