Skip to content

Commit

Permalink
Добавлены поля для генерации раздела offer, удален файл template, тк …
Browse files Browse the repository at this point in the history
…теперь генерация происходит за счет DomDocument
  • Loading branch information
Svitavsky committed Nov 24, 2020
1 parent 57e3d09 commit 7607791
Show file tree
Hide file tree
Showing 5 changed files with 150 additions and 194 deletions.
180 changes: 138 additions & 42 deletions Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@

class Builder
{
const URL_FIELDS = [
'url',
'picture'
];

// Возможные состояния уцененного товара
const CONDITION_TYPES = [
'likenew', // Как новый, уценен из-за недостатков
Expand All @@ -32,79 +37,68 @@ public function __construct(array $data)
$this->data = $data;
}

/**
* Создание контента из шаблона для YML
* @return string
*/
public function buildOld(): string
{
extract($this->data);

ob_start();
include 'template.php';
return ob_get_clean();
}

public function build()
{
$config = $this->data['config'];
$shop = $this->data['shop'];
$currencies = $this->data['currencies'];
$categories = $this->data['categories'];
$offers = $this->data['offers'];
$date = date("Y-m-d H:i", time());

$xml = new DOMDocument('1.0', 'UTF-8');
$xml->formatOutput = true;

$catalog = $xml->createElement('yml_catalog');
$catalog->setAttribute('date', $config['date']);
$catalog->setAttribute('date', $date);

$shop = $xml->createElement('shop');
$shopEl = $xml->createElement('shop');

$name = $xml->createElement('name', $config['companyName']);
$company = $xml->createElement('company', $config['companyDescription']);
$url = $xml->createElement('url', $config['companyWebsite']);
$shop->appendChild($name);
$shop->appendChild($company);
$shop->appendChild($url);
$name = $xml->createElement('name', $shop['name']);
$company = $xml->createElement('company', $shop['description']);
$url = $xml->createElement('url', rawurlencode(utf8_encode($shop['website'])));
$shopEl->appendChild($name);
$shopEl->appendChild($company);
$shopEl->appendChild($url);

$currencies = $xml->createElement('currencies');
foreach ($config['currencies'] as $id => $rate) {
$currenciesEl = $xml->createElement('currencies');
foreach ($currencies as $id => $rate) {
$currency = $xml->createElement('currency');
$currency->setAttribute('id', $id);
$currency->setAttribute('rate', $rate);
$currencies->appendChild($currency);
$currenciesEl->appendChild($currency);
}
$shop->appendChild($currencies);
$shopEl->appendChild($currenciesEl);

$categoriesEl = $xml->createElement('categories');
foreach ($categories as $category) {
$categoryEl = $xml->createElement('category', $category['name']);
$categoryEl->setAttribute('id', $category['id']);

if(isset($category['parentId'])) {
if (isset($category['parentId'])) {
$categoryEl->setAttribute('parentId', $category['parentId']);
}

$categoriesEl->appendChild($categoryEl);
}
$shop->appendChild($categoriesEl);
$shopEl->appendChild($categoriesEl);

$offersEl = $xml->createElement('offers');
foreach($offers as $offer) {
foreach ($offers as $offer) {
$offerEl = $xml->createElement('offer');
$offerEl->setAttribute('id', $offer['id']);
$offerEl->setIdAttribute('id', true);

if(isset($offer['bid'])) {
if (isset($offer['bid'])) {
$offerEl->setAttribute('bid', $offer['bid']);
}

if($config['simplifiedOffers']) {
$offerEl->setAttribute('type', 'vendor.model');
if (isset($offer['type'])) {
$offerEl->setAttribute('type', $offer['type']);

$name = $xml->createElement('name', $offer['name']);
$offerEl->appendChild($name);

if(isset($offer['vendor'])) {
if (isset($offer['vendor'])) {
$vendor = $xml->createElement('vendor', $offer['vendor']);
$offerEl->appendChild($vendor);
}
Expand All @@ -115,31 +109,133 @@ public function build()
$vendor = $xml->createElement('vendor', $offer['vendor']);
$offerEl->appendChild($vendor);

if(isset($offer['typePrefix'])) {
if (isset($offer['typePrefix'])) {
$typePrefix = $xml->createElement('typePrefix', $offer['typePrefix']);
$offerEl->appendChild($typePrefix);
}
}

if(isset($offer['available'])) {
if (isset($offer['description'])) {
$cdata = $xml->createCDATASection($offer['description']);
$descriptionEl = $xml->createElement('description');
$descriptionEl->appendChild($cdata);

$offerEl->appendChild($descriptionEl);
}

if (isset($offer['barcode'])) {
foreach ($offer['barcode'] as $barcode) {
$barcodeEl = $xml->createElement('barcode', $barcode);
$offerEl->appendChild($barcodeEl);
}
}

if (isset($offer['group_id'])) {
$offerEl->setAttribute('group_id', $offer['group_id']);
}

if (isset($offer['available'])) {
$offerEl->setAttribute('available', $offer['available']);
}

if(isset($offer['simple'])) {
foreach($offer['simple'] as $field => $value) {
$priceEl = $xml->createElement('price', $offer['price']);

if (isset($offer['price_from'])) {
$priceEl->setAttribute('from', 'true');
}

$offerEl->appendChild($priceEl);

if (isset($offer['supplier'])) {
$supplierEl = $xml->createElement('supplier');
$supplierEl->setAttribute('ogrn', $offer['supplier']);
$offerEl->appendChild($supplierEl);
}

if (isset($offer['delivery-options'])) {
$deliveryEl = $xml->createElement('delivery-options');

foreach ($offer['delivery-options'] as $option) {
$optionEl = $xml->createElement('option');
$optionEl->setAttribute('cost', $option['cost']);
$optionEl->setAttribute('days', $option['days']);

if (isset($option['order-before'])) {
$optionEl->setAttribute('order-before', $option['order-before']);
}

$deliveryEl->appendChild($optionEl);
}

$offerEl->appendChild($deliveryEl);
}

if (isset($offer['pickup-options'])) {
$pickupEl = $xml->createElement('pickup-options');

foreach ($offer['pickup-options'] as $option) {
$optionEl = $xml->createElement('option');
$optionEl->setAttribute('cost', $option['cost']);
$optionEl->setAttribute('days', $option['days']);

if (isset($option['order-before'])) {
$optionEl->setAttribute('order-before', $option['order-before']);
}

$pickupEl->appendChild($optionEl);
}

$offerEl->appendChild($pickupEl);
}

if (isset($offer['simple'])) {
foreach ($offer['simple'] as $field => $value) {
if (in_array($field, self::URL_FIELDS)) {
$value = rawurlencode(utf8_encode($value));
}

$element = $xml->createElement($field, $value);
$offerEl->appendChild($element);
}
}

if (isset($offer['condition'])) {
$conditionEl = $xml->createElement('condition');
$conditionEl->setAttribute('type', $offer['condition']['type']);

$reasonEl = $xml->createElement('reason', $offer['condition']['reason']);
$conditionEl->appendChild($reasonEl);

$offerEl->appendChild($conditionEl);
}

if (isset($offer['credit-template'])) {
$creditEl = $xml->createElement('credit-template');
$creditEl->setAttribute('id', $offer['credit-template']);

$offerEl->appendChild($creditEl);
}

if (isset($offer['param'])) {
foreach ($offer['param'] as $param) {
$paramEl = $xml->createElement('param', $param['value']);
$paramEl->setAttribute('name', $param['name']);

if (isset($param['unit'])) {
$paramEl->setAttribute('unit', $param['unit']);
}

$offerEl->appendChild($paramEl);
}
}

$offersEl->appendChild($offerEl);
}
$shop->appendChild($offersEl);

$shopEl->appendChild($offersEl);

$catalog->appendChild($shop);
$catalog->appendChild($shopEl);
$xml->appendChild($catalog);
$done= $xml->saveXML();
return $done;
$done = $xml->saveXML();
return $done;
}
}
5 changes: 1 addition & 4 deletions Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,13 +176,10 @@ private function getParams($data)
$params = [];
foreach ($data->param as $row) {
$param = [
'name' => (string)$row['name'],
'value' => (string)$row
];

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

if (isset($row['unit'])) {
$param['unit'] = (string)$row['unit'];
}
Expand Down
13 changes: 11 additions & 2 deletions Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ class Validator
'price' => 'required|float',
'price_from' => 'boolean',
'oldprice' => 'float|greater[price]',
'purchase_price' => 'float|less[price]',
'enable_auto_discounts' => 'boolean',
'currencyId' => 'required|string|currency',
'categoryId' => 'required|string|length[18]',
Expand Down Expand Up @@ -86,6 +87,7 @@ class Validator
'vendorCode',
'url',
'oldprice',
'purchase_price',
'enable_auto_discounts',
'currencyId',
'categoryId',
Expand Down Expand Up @@ -118,7 +120,7 @@ class Validator
],
'barcode' => 'string',
'param' => [
'name' => 'string',
'name' => 'required|string',
'unit' => 'string',
'value' => 'required|string'
],
Expand Down Expand Up @@ -204,7 +206,7 @@ private function offers(array $offers)
continue 3;
}
}
if(in_array($field, self::SIMPLE_FIELDS)) {
if (in_array($field, self::SIMPLE_FIELDS)) {
$offer['simple'][$field] = $offer[$field];
unset($offer[$field]);
}
Expand Down Expand Up @@ -244,6 +246,8 @@ private function validate($data, $rule, $field)
return $this->image($value);
case 'greater':
return $this->greater($data, $value, $ruleOptions);
case 'less':
return $this->less($data, $value, $ruleOptions);
case 'select':
return $this->select($value, $ruleOptions);
case 'range':
Expand Down Expand Up @@ -357,6 +361,11 @@ private function greater($offer, $value, $greaterThan)
return $value > $offer[$greaterThan];
}

private function less($offer, $value, $lessThan)
{
return $value < $offer[$lessThan];
}

private function select($value, $optionsList)
{
$options = explode(',', $optionsList);
Expand Down
1 change: 0 additions & 1 deletion YMLGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ public function run(array $data)
$data['availableCountries'] = $this->getAvailableCountries();
$validator = new Validator($data);
$result = $validator->validateAll();
$result['date'] = date("Y-m-d H:i", time());

$builder = new Builder($result);
$content = $builder->build();
Expand Down
Loading

0 comments on commit 7607791

Please sign in to comment.