Skip to content

Commit

Permalink
Merge pull request #3 from Svitavsky/parser
Browse files Browse the repository at this point in the history
Создан парсер из YML файлов в PHP
  • Loading branch information
Svitavsky committed Nov 22, 2020
2 parents a9ed1ae + 25d1c04 commit 9c38f26
Show file tree
Hide file tree
Showing 4 changed files with 229 additions and 26 deletions.
203 changes: 203 additions & 0 deletions Parser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,203 @@
<?php

namespace src;

class Parser
{
private $filename;

public function __construct(string $filename)
{
$this->filename = $filename;
}

public function parse()
{
$xml = simplexml_load_file($this->filename);

return [
'date' => (string)$xml['date'],
'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)
];
}

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

private function currencies($data)
{
$currencies = [];
foreach ($data as $row) {
$id = (string)$row['id'];
$rate = (string)$row['rate'];
$currencies[$id] = $rate;
}

return $currencies;
}

private function categories($data)
{
$categories = [];
foreach ($data as $row) {
$id = (string)$row['id'];
$name = (string)$row;
$parentId = isset($row['parentId']) ? (string)$row['parentId'] : '';
$categories[] = compact('id', 'name', 'parentId');
}

return $categories;
}

private function offers($data)
{
$offers = [];
foreach ($data as $row) {
$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($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['param']) || empty($row->param)) {
continue 2;
}

$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;
break;
}

$offer[$field] = $value;
}

$offers[] = $offer;
}

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 = [];
foreach ($data->{$field} as $row) {
$array[] = (string)$row;
}

return $array;
}

private function getParams($data)
{
$params = [];
foreach ($data->param as $row) {
$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 $params;
}

private function getConditions($row)
{
return [
'type' => (string)$row['type'],
'reason' => (string)$row->reason,
];
}
}
6 changes: 3 additions & 3 deletions Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class Validator
'pickup' => 'boolean',
'pickup-options' => 'array',
'store' => 'boolean',
'description' => 'required|string|length[3000]',
'description' => 'string|length[3000]',
'sales_notes' => 'string|length[50]',
'min-quantity' => 'int',
'manufacturer_warranty' => 'boolean',
Expand Down Expand Up @@ -295,8 +295,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
Empty file added config.example.php
Empty file.
46 changes: 23 additions & 23 deletions template.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,19 @@
use src\Builder;

?>

<?= '<?xml version="1.0" encoding="UTF-8"?>' . PHP_EOL ?>
<yml_catalog date="<?= $date ?>">
<shop>
<name><?= $shop['name'] ?></name>
<company><?= $shop['description'] ?></company>
<url><?= $shop['website'] ?></url>
<url><?= rawurlencode(utf8_encode($shop['website'])) ?></url>
<currencies>
<?php foreach ($currencies as $code => $rate): ?>
<currency id="<?= $code ?>" rate="<?= $rate ?>"/>
<?php endforeach; ?>
</currencies>
<categories>
<?php foreach ($categories as $category): ?>
<?php if (!is_int($category['id']) || $category['id'] <= 0): ?>
<?php continue; ?>
<?php endif; ?>
<?php $parentId = isset($category['parentId']) ? "parentId=\"{$category['parentId']}\"" : '' ?>
<category id="<?= $category['id'] ?>" <?= $parentId ?>><?= $category['name'] ?></category>
<?php endforeach; ?>
Expand All @@ -28,6 +24,7 @@
<?php foreach ($offers as $offer): ?>
<offer id="<?= $offer['id'] ?>"
<?= isset($offer['bid']) ? "bid=\"{$offer['bid']}\"" : '' ?>
<?= isset($offer['group_id']) ? "group_id=\"{$offer['group_id']}\"" : '' ?>
<?= isset($offer['type']) ? "type=\"{$offer['type']}\"" : '' ?>
<?= isset($offer['available']) ? "available=\"{$offer['available']}\"" : '' ?> >
<?php if (!isset($offer['type'])): ?>
Expand All @@ -45,16 +42,22 @@
<?php if (isset($offer['vendorCode'])): ?>
<vendorCode><?= $offer['vendorCode'] ?></vendorCode>
<?php endif; ?>
<url><?= $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; ?>
<enable_auto_discounts><?= $offer['enable_auto_discounts'] ?></enable_auto_discounts>
<?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><?= $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 @@ -86,7 +89,12 @@
<?php if (isset($offer['store'])): ?>
<store><?= $offer['store'] ?></store>
<?php endif; ?>
<description><?= $offer['description'] ?></description>
<?php if (isset($offer['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>
<?php endif; ?>
Expand All @@ -108,11 +116,11 @@
<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 @@ -130,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 9c38f26

Please sign in to comment.