1
0
mirror of synced 2025-02-12 02:09:23 +03:00

fix parser (#38)

This commit is contained in:
Sergey 2017-12-20 11:20:47 +03:00 committed by Alex Lushpai
parent 50fe957a3a
commit c45ee4f80d

View File

@ -89,16 +89,18 @@ class MoySkladICMLParser
{ {
$assortiment = $this->parseAssortiment(); $assortiment = $this->parseAssortiment();
$countAssortiment = count($assortiment);
if (count($assortiment) > 0) {
if ($countAssortiment > 0) {
$categories = $this->parserFolder(); $categories = $this->parserFolder();
} else { } else {
$categories = array(); $categories = array();
} }
$icml = $this->ICMLCreate($categories, $assortiment); $icml = $this->ICMLCreate($categories, $assortiment);
$countCategories = count($categories);
if (count($categories) > 0 && count($assortiment) > 0) { if ($countCategories > 0 && $countAssortiment > 0) {
$icml->asXML($this->getFilePath()); $icml->asXML($this->getFilePath());
} }
@ -110,6 +112,7 @@ class MoySkladICMLParser
*/ */
protected function requestJson($url) protected function requestJson($url)
{ {
$downloadImage = strripos($url, 'download');
$curlHandler = curl_init(); $curlHandler = curl_init();
curl_setopt($curlHandler, CURLOPT_USERPWD, $this->login . ':' . $this->pass); curl_setopt($curlHandler, CURLOPT_USERPWD, $this->login . ':' . $this->pass);
@ -121,26 +124,25 @@ class MoySkladICMLParser
curl_setopt($curlHandler, CURLOPT_TIMEOUT, self::TIMEOUT); curl_setopt($curlHandler, CURLOPT_TIMEOUT, self::TIMEOUT);
curl_setopt($curlHandler, CURLOPT_CONNECTTIMEOUT, 60); curl_setopt($curlHandler, CURLOPT_CONNECTTIMEOUT, 60);
if (strripos($url, 'download')) { if ($downloadImage) {
curl_setopt($curlHandler, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($curlHandler, CURLOPT_FOLLOWLOCATION, 1);
} }
curl_setopt($curlHandler, CURLOPT_HTTPHEADER, array( curl_setopt($curlHandler, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json' 'Content-Type: application/json'
)); ));
$responseBody = curl_exec($curlHandler); $responseBody = curl_exec($curlHandler);
$statusCode = curl_getinfo($curlHandler, CURLINFO_HTTP_CODE); $statusCode = curl_getinfo($curlHandler, CURLINFO_HTTP_CODE);
$errno = curl_errno($curlHandler); $errno = curl_errno($curlHandler);
$error = curl_error($curlHandler); $error = curl_error($curlHandler);
curl_close($curlHandler); curl_close($curlHandler);
if (strripos($url, 'download')) {
if ($downloadImage) {
return $responseBody; return $responseBody;
} }
$result = json_decode($responseBody, true); $result = json_decode($responseBody, true);
if ($statusCode >= 400) { if ($statusCode >= 400) {
@ -467,8 +469,9 @@ class MoySkladICMLParser
$date = new DateTime(); $date = new DateTime();
$xmlstr = '<yml_catalog date="'.$date->format('Y-m-d H:i:s').'"><shop><name>'.$this->shop.'</name></shop></yml_catalog>'; $xmlstr = '<yml_catalog date="'.$date->format('Y-m-d H:i:s').'"><shop><name>'.$this->shop.'</name></shop></yml_catalog>';
$xml = new SimpleXMLElement($xmlstr); $xml = new SimpleXMLElement($xmlstr);
$countCategories = count($categories);
if (count($categories) > 0) {
if ($countCategories > 0) {
$categoriesXml = $this->icmlAdd($xml->shop, 'categories', ''); $categoriesXml = $this->icmlAdd($xml->shop, 'categories', '');
foreach ($categories as $category) { foreach ($categories as $category) {
$categoryXml = $this->icmlAdd($categoriesXml, 'category', htmlspecialchars($category['name'])); $categoryXml = $this->icmlAdd($categoriesXml, 'category', htmlspecialchars($category['name']));
@ -481,7 +484,9 @@ class MoySkladICMLParser
} }
$offersXml = $this->icmlAdd($xml->shop, 'offers', ''); $offersXml = $this->icmlAdd($xml->shop, 'offers', '');
if (count($products) > 0) { $countProducts = count($products);
if ($countProducts > 0) {
foreach ($products as $product) { foreach ($products as $product) {
$offerXml = $offersXml->addChild('offer'); $offerXml = $offersXml->addChild('offer');
$offerXml->addAttribute('id', $product['id']); $offerXml->addAttribute('id', $product['id']);
@ -578,8 +583,9 @@ class MoySkladICMLParser
$path = isset($this->options['directory']) && $this->options['directory'] ? $path = isset($this->options['directory']) && $this->options['directory'] ?
$this->options['directory'] : __DIR__; $this->options['directory'] : __DIR__;
$endPath = substr($path, -1);
if (substr($path, -1) === '/') {
if ($endPath === '/') {
$path = substr($path, 0, -1); $path = substr($path, 0, -1);
} }
@ -621,12 +627,16 @@ class MoySkladICMLParser
$root = __DIR__; $root = __DIR__;
$imgDirrectory = $this->options['imageDownload']['pathToImage']; $imgDirrectory = $this->options['imageDownload']['pathToImage'];
if (substr($imgDirrectory, 0, 1) === '/') { $startPathDirrectory = substr($imgDirrectory,0, 1);
if ($startPathDirrectory == '/') {
$imgDirrectory = substr($imgDirrectory, 1); $imgDirrectory = substr($imgDirrectory, 1);
} }
$endPathDirrectory = substr($imgDirrectory, -1);
if (substr($imgDirrectory, -1) === '/') { if ($endPathDirrectory == '/') {
$imgDirrectory = substr($imgDirrectory, 0, -1); $imgDirrectory = substr($imgDirrectory, 0, -1);
} }
@ -636,7 +646,7 @@ class MoySkladICMLParser
if (file_exists($root . '/' . $imgDirrectory) === false) { if (file_exists($root . '/' . $imgDirrectory) === false) {
@mkdir($root . '/' . $imgDirrectory); @mkdir($root . '/' . $imgDirrectory);
} }
if (file_exists($root . $imgDirrectory . '/' . $image['name']) === false) { if (file_exists($root . $imgDirrectory . '/' . $image['name']) === false) {
$content = $this->requestJson($image['imageUrl']); $content = $this->requestJson($image['imageUrl']);
@ -648,8 +658,8 @@ class MoySkladICMLParser
$imageUrl = $this->linkGeneration($image['name']); $imageUrl = $this->linkGeneration($image['name']);
return $imageUrl; return $imageUrl;
} }
/** /**
* Генерация ссылки на изображение * Генерация ссылки на изображение
* *
@ -663,11 +673,15 @@ class MoySkladICMLParser
} }
$path = $this->options['imageDownload']['pathToImage']; $path = $this->options['imageDownload']['pathToImage'];
if (substr($path, 0, 1) === '/') { $startPath = substr($path, 0, 1);
if ($startPath === '/') {
$path = substr($path, 1); $path = substr($path, 1);
} }
if (substr($path, -1) === '/') { $endPath = substr($path, -1);
if ($endPath === '/') {
$path = substr($path, 0, -1); $path = substr($path, 0, -1);
} }
@ -722,7 +736,8 @@ class MoySkladICMLParser
} }
unset($value); unset($value);
$error = trim($error, ' /');
if (!empty($error)) { if (!empty($error)) {
return $error; return $error;
} }