1
0
mirror of synced 2025-02-15 03:33:14 +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,9 +160,15 @@ 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) {
$total = $xml[0]['total'];
foreach ($xml->goodFolder as $goodFolder) { foreach ($xml->goodFolder as $goodFolder) {
$uuid = (string) $goodFolder->uuid; $uuid = (string) $goodFolder->uuid;
$externalCode = (string) $goodFolder->externalcode; $externalCode = (string) $goodFolder->externalcode;
@ -190,8 +201,13 @@ class MoySkladICMLParser
$categories[$uuid] = $category; $categories[$uuid] = $category;
} }
} else {
throw new RuntimeException('No xml');
} }
$start += self::STEP;
} while ($start < $total);
$result = array(); $result = array();
$this->sortGroupTree($result, $categories); $this->sortGroupTree($result, $categories);
@ -207,16 +223,26 @@ 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) {
$total = $xml[0]['total'];
foreach ($xml->company as $c) { foreach ($xml->company as $c) {
$uuid = (string) $c->uuid; $uuid = (string) $c->uuid;
$name = (string) $c[0]['name']; $name = (string) $c[0]['name'];
$vendors[$uuid] = $name; $vendors[$uuid] = $name;
} }
} else {
throw new RuntimeException('No xml');
} }
$start += self::STEP;
} while ($start < $total);
return $vendors; return $vendors;
} }
@ -233,8 +259,13 @@ class MoySkladICMLParser
) { ) {
$products = array(); $products = array();
$xml = $this->requestXml(self::PRODUCT_LIST_URL); $start = 0;
$total = 0;
do {
$xml = $this->requestXml(self::PRODUCT_LIST_URL.'?'.http_build_query(['start' => $start]));
if ($xml) { if ($xml) {
$total = $xml[0]['total'];
foreach ($xml->good as $v) { foreach ($xml->good as $v) {
$parentUuid = isset($v[0]['parentUuid']) ? $parentUuid = isset($v[0]['parentUuid']) ?
@ -259,11 +290,21 @@ class MoySkladICMLParser
'offers' => array(), 'offers' => array(),
); );
} }
} else {
throw new RuntimeException('No xml');
} }
$start += self::STEP;
} while ($start < $total);
$start = 0;
$total = 0;
do {
if (!$this->isIgnoreOffers()) { if (!$this->isIgnoreOffers()) {
$xml = $this->requestXml(self::OFFER_LIST_URL); $xml = $this->requestXml(self::OFFER_LIST_URL.'?'.http_build_query(['start' => $start]));
if ($xml) { if ($xml) {
$total = $xml[0]['total'];
foreach ($xml->consignment as $c) { foreach ($xml->consignment as $c) {
// если нет feature, то товар без торговых предложений // если нет feature, то товар без торговых предложений
if (!isset($c->feature)) { if (!isset($c->feature)) {
@ -283,9 +324,14 @@ class MoySkladICMLParser
// иначе это не товар а услуга (service) // иначе это не товар а услуга (service)
} }
} }
} else {
throw new RuntimeException('No xml');
} }
} }
$start += self::STEP;
} while ($start < $total);
// для товаров без торговых преложений // для товаров без торговых преложений
foreach ($products as $key1 => &$product) { foreach ($products as $key1 => &$product) {
// если нет торговых предложений // если нет торговых предложений