Skip to content

Commit

Permalink
Убрал использование параметра SimplifiedOffers, теперь тип будет опре…
Browse files Browse the repository at this point in the history
…деляться через параметр type
  • Loading branch information
Svitavsky committed Nov 21, 2020
1 parent cc11798 commit 7c607e6
Show file tree
Hide file tree
Showing 3 changed files with 145 additions and 53 deletions.
115 changes: 99 additions & 16 deletions Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,22 @@ public function parse()
{
$xml = simplexml_load_file($this->filename);

$data = [
return [
'date' => (string)$xml['date'],
'name' => (string)$xml->shop->name,
'url' => (string)$xml->shop->url,
'shop' => $this->shop($xml->shop),
'currencies' => $this->currencies($xml->shop->currencies->currency),
'categories' => $this->categories($xml->shop->categories->category),
'offers' => $this->offers($xml->shop->offers->offer)
];
}

return $data;
private function shop($data)
{
return [
'name' => (string)$data->name,
'description' => (string)$data->company,
'website' => rawurldecode(utf8_decode((string)$data->url)),
];
}

private function currencies($data)
Expand Down Expand Up @@ -56,30 +62,67 @@ private function offers($data)
{
$offers = [];
foreach ($data as $row) {
$offer['id'] = (string)$row['id'];
$offer['available'] = (string)$row['available'];
$offer = [
'id' => (string)$row['id']
];

if (isset($row['available'])) {
$offer['available'] = (string)$row['available'];
}

if (isset($row['bid'])) {
$offer['bid'] = (string)$row['bid'];
}

if (isset($row['group_id'])) {
$offer['group_id'] = (string)$row['group_id'];
}

if (isset($row['type'])) {
$offer['type'] = (string)$row['type'];
}

foreach ($row as $subRow) {
$field = $subRow->getName();

switch ($field) {
case 'price':
if (isset($row->price['from'])) {
$offer['price_from'] = true;
}

$value = (string)$subRow;
break;
case 'barcode':
if (isset($offer[$field]) || empty($data->{$field})) {
if (isset($offer[$field]) || empty($row->{$field})) {
continue 2;
}

$value = $this->getMultiple($field, $data);
break;
case 'url':
case 'picture':
$value = $this->getUrl((string)$subRow);
break;
case 'param':
if (isset($offer[$field]) || empty($data->{$field})) {
if (isset($offer['param']) || empty($row->param)) {
continue 2;
}

$value = $this->getParams($data);
$value = $this->getParams($row);
break;
case 'delivery-options':
case 'pickup-options':

$value = $this->getTransportOptions($row->{$field});
break;
case 'supplier':
$value = isset($row->supplier['ogrn']) ? (string)$row->supplier['ogrn'] : '';
break;
case 'condition':
$value = $this->getConditions($subRow);
break;
case 'credit-template':
$value = (string)$row->{"credit-template"}['id'];
break;
default:
$value = (string)$subRow;
Expand All @@ -95,6 +138,29 @@ private function offers($data)
return $offers;
}

private function getTransportOptions($data)
{
$options = [];
foreach ($data->option as $row) {
$option = [
'cost' => (string)$row['cost'],
'days' => (string)$row['days']
];

if (isset($row['order-before'])) {
$option['order-before'] = (string)$row['order-before'];
}

$options[] = $option;
}
return $options;
}

private function getUrl(string $url)
{
return rawurldecode(utf8_decode($url));
}

private function getMultiple(string $field, $data)
{
$array = [];
Expand All @@ -107,14 +173,31 @@ private function getMultiple(string $field, $data)

private function getParams($data)
{
$subData = [];
$params = [];
foreach ($data->param as $row) {
$name = (string)$row['name'];
$value = (string)$row;
$unit = isset($row['unit']) ? (string)$row['unit'] : '';;
$subData[] = compact('name', 'value', 'unit');
$param = [
'value' => (string)$row
];

if (isset($row['name'])) {
$param['name'] = (string)$row['name'];
}

if (isset($row['unit'])) {
$param['unit'] = (string)$row['unit'];
}

$params[] = $param;
}

return $subData;
return $params;
}

private function getConditions($row)
{
return [
'type' => (string)$row['type'],
'reason' => (string)$row->reason,
];
}
}
38 changes: 23 additions & 15 deletions Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ class Validator
// Формат записи для поля dimensions
const DIMENSIONS_REGEX = '/[0-9]*\.?[0-9]*\/[0-9]*\.?[0-9]*\/[0-9]*\.?[0-9]*/';

// Замена кода валюты, чтобы во всей выгрузке был 1 код
const CURRENCY_REPLACEMENTS = [
'RUR' => 'RUB',
'RUB' => 'RUR'
];

// Поля для упрощенного типа
const SIMPLIFIED_TYPE_FIELDS = [
'name' => 'required|string',
Expand Down Expand Up @@ -67,7 +73,7 @@ class Validator
'expiry' => 'string',
'weight' => 'float',
'dimensions' => 'dimensions',
'downloadable' => 'float',
'downloadable' => 'boolean',
'age_month' => 'range[0-12]',
'age_year' => 'select[0,6,12,16,18]',
];
Expand All @@ -86,7 +92,7 @@ class Validator
],
'barcode' => 'string',
'param' => [
'name' => 'required|string',
'name' => 'string',
'unit' => 'string',
'value' => 'required|string'
],
Expand All @@ -96,12 +102,6 @@ class Validator
]
];

/**
* Поля, зависимые от типа вывода
* @var array
*/
private $typeFields;

/**
* Конфигурация модуля
* @var array
Expand All @@ -111,7 +111,6 @@ class Validator
public function __construct(array $config)
{
$this->config = $config;
$this->typeFields = $config['simplifiedOffers'] ? self::SIMPLIFIED_TYPE_FIELDS : self::CUSTOM_TYPE_FIELDS;
}

public function validateAll(array $data)
Expand All @@ -124,10 +123,9 @@ public function validateAll(array $data)

private function offers(array $offers)
{
$allRules = self::FIELDS_RULES + $this->typeFields;

$validated = [];
foreach ($offers as $offer) {
$allRules = self::FIELDS_RULES + (isset($offer['type']) ? self::CUSTOM_TYPE_FIELDS : self::SIMPLIFIED_TYPE_FIELDS);
foreach ($allRules as $field => $rulesList) {
$rules = explode('|', $rulesList);
foreach ($rules as $rule) {
Expand Down Expand Up @@ -218,8 +216,8 @@ private function string($value)

private function boolean($value)
{
$isTrue = in_array($value, self::BOOLEAN_TRUE);
$isFalse = in_array($value, self::BOOLEAN_FALSE);
$isTrue = $value === true || in_array($value, self::BOOLEAN_TRUE);
$isFalse = $value === false || in_array($value, self::BOOLEAN_FALSE);

return $isTrue || $isFalse;
}
Expand Down Expand Up @@ -265,8 +263,8 @@ private function simpleArray($data, $field)
}

$rules = explode('|', self::ARRAY_FIELDS_RULES[$field]);
foreach($data[$field] as $value) {
foreach($rules as $rule) {
foreach ($data[$field] as $value) {
foreach ($rules as $rule) {
$result = $this->validate($value, $rule, $field);

if ($result === false) {
Expand Down Expand Up @@ -322,6 +320,16 @@ private function length($value, $length)
private function currency($value)
{
$currencies = array_keys($this->config['currencies']);

// В выгрузке можно применять оба кода рубля, используем тот, который указан в конфиге
if (in_array('RUR', $currencies) && $value === 'RUB') {
$value = 'RUR';
}

if (in_array('RUB', $currencies) && $value === 'RUR') {
$value = 'RUB';
}

return in_array($value, $currencies);
}

Expand Down
45 changes: 23 additions & 22 deletions template.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<shop>
<name><?= $config['companyName'] ?></name>
<company><?= $config['companyDescription'] ?></company>
<url><?= urlencode(utf8_encode($config['companyWebsite'])) ?></url>
<url><?= rawurlencode(utf8_encode($config['companyWebsite'])) ?></url>
<currencies>
<?php foreach ($config['currencies'] as $code => $rate): ?>
<currency id="<?= $code ?>" rate="<?= $rate ?>"/>
Expand All @@ -24,9 +24,10 @@
<?php foreach ($offers as $offer): ?>
<offer id="<?= $offer['id'] ?>"
<?= isset($offer['bid']) ? "bid=\"{$offer['bid']}\"" : '' ?>
<?= $config['simplifiedOffers'] ? 'type="vendor.model"' : '' ?>
<?= isset($offer['group_id']) ? "group_id=\"{$offer['group_id']}\"" : '' ?>
<?= isset($offer['type']) ? "type=\"{$offer['type']}\"" : '' ?>
<?= isset($offer['available']) ? "available=\"{$offer['available']}\"" : '' ?> >
<?php if ($config['simplifiedOffers']): ?>
<?php if (!isset($offer['type'])): ?>
<name><?= $offer['name'] ?></name>
<?php if (isset($offer['vendor'])): ?>
<vendor><?= $offer['vendor'] ?></vendor>
Expand All @@ -41,18 +42,22 @@
<?php if (isset($offer['vendorCode'])): ?>
<vendorCode><?= $offer['vendorCode'] ?></vendorCode>
<?php endif; ?>
<url><?= urlencode(utf8_encode($offer['url'])) ?></url>
<price><?= $offer['price'] ?></price>
<url><?= rawurlencode(utf8_encode($offer['url'])) ?></url>
<?php $priceFrom = isset($offer['price_from']) ? 'from="true"' : '' ?>
<price <?= $priceFrom ?>><?= $offer['price'] ?></price>
<?php if (isset($offer['oldprice'])): ?>
<oldprice><?= $offer['oldprice'] ?></oldprice>
<?php endif; ?>
<?php if (isset($offer['purchase_price'])): ?>
<purchase_price><?= $offer['purchase_price'] ?></purchase_price>
<?php endif; ?>
<?php if (isset($offer['enable_auto_discounts'])): ?>
<enable_auto_discounts><?= $offer['enable_auto_discounts'] ?></enable_auto_discounts>
<?php endif; ?>
<currencyId><?= $offer['currencyId'] ?></currencyId>
<categoryId><?= $offer['categoryId'] ?></categoryId>
<?php if (isset($offer['picture']) && strlen($offer['picture']) < 512): ?>
<picture><?= urlencode(utf8_encode($offer['picture'])) ?></picture>
<picture><?= rawurlencode(utf8_encode($offer['picture'])) ?></picture>
<?php endif; ?>
<?php if (isset($offer['supplier'])): ?>
<supplier ogrn="<?= $offer['supplier'] ?>"/>
Expand Down Expand Up @@ -85,7 +90,10 @@
<store><?= $offer['store'] ?></store>
<?php endif; ?>
<?php if (isset($offer['description'])): ?>
<description><?= $offer['description'] ?></description>
<description><![CDATA[<?= $offer['description'] ?>]]></description>
<?php endif; ?>
<?php if (isset($offer['sales_notes'])): ?>
<sales_notes><?= $offer['sales_notes'] ?></sales_notes>
<?php endif; ?>
<?php if (isset($offer['manufacturer_warranty'])): ?>
<manufacturer_warranty><?= $offer['manufacturer_warranty'] ?></manufacturer_warranty>
Expand All @@ -98,20 +106,21 @@
<?php endif; ?>
<?php if (isset($offer['barcode']) && is_array($offer['barcode'])): ?>
<?php foreach ($offer['barcode'] as $barcode): ?>
<barcode><?= $offer['barcode'] ?></barcode>
<barcode><?= $barcode ?></barcode>
<?php endforeach; ?>
<?php endif; ?>
<?php if (isset($offer['param']) && is_array($offer['param'])): ?>
<?php foreach ($offer['param'] as $param): ?>
<?php $unit = isset($param['unit']) ? "unit=\"{$param['unit']}\"" : ''; ?>
<param name="<?= $param['name'] ?>" <?= $unit ?>><?= $param['value'] ?></param>
<?php $name = isset($param['name']) ? "name=\"{$param['name']}\"" : ''; ?>
<param <?= $name ?> <?= $unit ?>><?= $param['value'] ?></param>
<?php endforeach; ?>
<?php endif; ?>
<?php if (isset($offer['condition'], $offer['condition_description']) &&
in_array($offer['condition'], Builder::CONDITION_TYPES) &&
strlen($offer['condition_description']) <= Builder::CONDITION_DESCRIPTION_LENGTH): ?>
<condition type="<?= $offer['condition'] ?>">
<reason><?= $offer['condition_description'] ?></reason>
<?php if (isset($offer['condition'], $offer['condition']['reason'], $offer['condition']['type']) &&
in_array($offer['condition']['type'], Builder::CONDITION_TYPES) &&
strlen($offer['condition']['reason']) <= Builder::CONDITION_DESCRIPTION_LENGTH): ?>
<condition type="<?= $offer['condition']['type'] ?>">
<reason><?= $offer['condition']['reason'] ?></reason>
</condition>
<?php endif; ?>
<?php if (isset($offer['credit-template'])): ?>
Expand All @@ -129,14 +138,6 @@
<?php if (isset($offer['downloadable'])): ?>
<downloadable><?= $offer['downloadable'] ?></downloadable>
<?php endif; ?>
<?php if (isset($offer['age'])): ?>
<?php if ($offer['age_type'] === 'year' && in_array($offer['age_type'], Builder::AGE_YEAR)): ?>
<param unit="year"><?= $offer['age'] ?></param>
<?php endif; ?>
<?php if ($offer['age_type'] === 'month' && in_array($offer['age_type'], Builder::AGE_MONTH)): ?>
<param unit="month"><?= $offer['age'] ?></param>
<?php endif; ?>
<?php endif; ?>
</offer>
<?php endforeach; ?>
</offers>
Expand Down

0 comments on commit 7c607e6

Please sign in to comment.