Skip to content

Commit

Permalink
Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
lireincore committed Mar 22, 2017
1 parent 1dfdb76 commit ce17295
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 51 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@

## About

[YML (Yandex Market Language)](https://yandex.ru/support/partnermarket/yml/about-yml.xml) streaming parser with validation for PHP.
[YML (Yandex Market Language)](https://yandex.ru/support/partnermarket/yml/about-yml.xml) streaming parser with validation.
Based on XMLReader. Suitable for large files.

## Install

Add the `lireincore/ymlparser` package to your `require` section in the `composer.json` file
Add the `"lireincore/ymlparser": "^3.2"` package to your `require` section in the `composer.json` file

or

Expand Down
14 changes: 11 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,19 +1,27 @@
{
"name": "lireincore/ymlparser",
"description": "YML (Yandex Market Language) parser",
"version": "3.2.0",
"version": "3.2.1",
"type": "library",
"keywords": ["yml", "parser", "yandex", "market"],
"homepage": "https://github.com/lireincore/ymlparser",
"license": "MIT",
"support": {
"issues": "https://github.com/lireincore/imgcache/issues",
"source": "https://github.com/lireincore/imgcache"
},
"authors": [
{
"name": "LireinCore",
"name": "lirein",
"email": "[email protected]"
}
],
"require": {
"php": ">=5.5"
"php": ">=5.5",
"ext-xmlreader": "*"
},
"require-dev": {
"phpunit/phpunit": "~5.0"
},
"autoload": {
"psr-4": {
Expand Down
9 changes: 5 additions & 4 deletions src/Offer/AExtOffer.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,9 @@ public function getErrors()
$errors = array_merge($errors, $deliveryOption->getErrors());
}
}
if ($this->age)
if ($this->age) {
$errors = array_merge($errors, $this->age->getErrors());
}

return $errors;
}
Expand All @@ -124,11 +125,11 @@ public function setAttribute(array $attrNode)
foreach ($attrNode['nodes'] as $subNode) {
$this->addDeliveryOption((new DeliveryOption())->setAttributes($subNode['attributes']));
}
}
elseif ($attrNode['name'] == 'age') {
} elseif ($attrNode['name'] == 'age') {
$this->setAge((new Age())->setAttributes($attrNode['attributes'] + ['value' => $attrNode['value']]));
} else {
parent::setAttribute($attrNode);
}
else parent::setAttribute($attrNode);

return $this;
}
Expand Down
12 changes: 4 additions & 8 deletions src/Offer/AOffer.php
Original file line number Diff line number Diff line change
Expand Up @@ -255,17 +255,13 @@ public function setAttribute(array $attrNode)
foreach ($attrNode['nodes'] as $subNode) {
$this->addOutlet((new Outlet())->setAttributes($subNode['attributes']));
}
}
elseif ($attrNode['name'] == 'picture') {
} elseif ($attrNode['name'] == 'picture') {
$this->addPicture($attrNode['value']);
}
elseif ($attrNode['name'] == 'barcode') {
} elseif ($attrNode['name'] == 'barcode') {
$this->addBarcode($attrNode['value']);
}
elseif ($attrNode['name'] == 'param') {
} elseif ($attrNode['name'] == 'param') {
$this->addParam((new Param())->setAttributes($attrNode['attributes'] + ['value' => $attrNode['value']]));
}
else {
} else {
if (!is_null($attrNode['value'])) $this->setField($attrNode['name'], $attrNode['value']);
if (!empty($attrNode['attributes'])) {
foreach ($attrNode['attributes'] as $name => $value) {
Expand Down
3 changes: 1 addition & 2 deletions src/Offer/TourOffer.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,7 @@ public function setAttribute(array $attrNode)
{
if ($attrNode['name'] == 'dataTour') {
$this->addDataTour($attrNode['value']);
}
else {
} else {
parent::setAttribute($attrNode);
}

Expand Down
9 changes: 3 additions & 6 deletions src/Shop.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,18 +189,15 @@ public function setAttribute(array $attrNode)
foreach ($attrNode['nodes'] as $subNode) {
$this->addCurrency((new Currency())->setAttributes($subNode['attributes']));
}
}
elseif ($attrNode['name'] == 'categories') {
} elseif ($attrNode['name'] == 'categories') {
foreach ($attrNode['nodes'] as $subNode) {
$this->addCategory((new Category())->setAttributes($subNode['attributes'] + ['name' => $subNode['value']]));
}
}
elseif ($attrNode['name'] == 'delivery-options') {
} elseif ($attrNode['name'] == 'delivery-options') {
foreach ($attrNode['nodes'] as $subNode) {
$this->addDeliveryOption((new DeliveryOption())->setAttributes($subNode['attributes']));
}
}
else {
} else {
if (!is_null($attrNode['value'])) $this->setField($attrNode['name'], $attrNode['value']);
if (!empty($attrNode['attributes'])) {
foreach ($attrNode['attributes'] as $name => $value) {
Expand Down
18 changes: 6 additions & 12 deletions src/TYML.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,16 @@ protected function toArray()
foreach ($this as $key => $value) {
if (is_object($value) && method_exists($value, 'getData')) {
$array[$key] = $value->getData();
}
elseif (is_array($value)) {
} elseif (is_array($value)) {
if (!empty($value)) {
$array[$key] = $this->getArray($value);
}
}
else {
} else {
$getter = 'get' . $key;
if (method_exists($this, $getter)) {
$getValue = $this->$getter();
if ($getValue !== null) $array[$key] = $getValue;
}
elseif ($value !== null) {
} elseif ($value !== null) {
$array[$key] = $value;
}
}
Expand All @@ -61,17 +58,14 @@ protected function getArray($value)
foreach ($value as $subkey => $subvalue) {
if (is_object($subvalue) && method_exists($subvalue, 'getData')) {
$subarray[$subkey] = $subvalue->getData();
}
elseif (is_array($subvalue)) {
} elseif (is_array($subvalue)) {
$subarray[$subkey] = $this->getArray($subvalue);
}
else {
} else {
$getter = 'get' . $subkey;
if (method_exists($this, $getter)) {
$getValue = $this->$getter();
if ($getValue !== null) $subarray[$subkey] = $getValue;
}
elseif ($subvalue !== null) {
} elseif ($subvalue !== null) {
$subarray[$subkey] = $subvalue;
}
}
Expand Down
27 changes: 13 additions & 14 deletions src/YML.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,17 @@ public function __construct()

/**
* @param string $uri
* @param string|null $schema
* @param string|bool $schema
* @throws \Exception
*/
public function parse($uri, $schema = __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'yml.xsd')
public function parse($uri, $schema = true)
{
$this->uri = $uri;
$this->schema = $schema;
if ($schema === true) {
$this->schema = __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'yml.xsd';
} elseif (is_string($schema)) {
$this->schema = $schema;
}

$this->open();

Expand Down Expand Up @@ -113,8 +117,7 @@ public function getOffers()
while ($this->read()) {
if ($this->path == 'yml_catalog/shop/offers/offer') {
yield $this->parseOffer();
}
elseif ($this->path == 'yml_catalog/shop') {
} elseif ($this->path == 'yml_catalog/shop') {
break;
}
}
Expand All @@ -137,11 +140,9 @@ protected function parseShop()
while ($this->read()) {
if ($this->path == 'yml_catalog/shop/offers') {
$shop->setOffersCount($this->parseOffersCount());
}
elseif ($xml->nodeType == \XMLReader::ELEMENT) {
} elseif ($xml->nodeType == \XMLReader::ELEMENT) {
$nodes[] = $this->parseNode('yml_catalog/shop');
}
elseif ($this->path == 'yml_catalog') {
} elseif ($this->path == 'yml_catalog') {
break;
}
}
Expand Down Expand Up @@ -182,11 +183,9 @@ protected function parseNode($basePath)
while ($this->read()) {
if ($xml->nodeType == \XMLReader::ELEMENT) {
$nodes[] = $this->parseNode($path);
}
elseif (($xml->nodeType == \XMLReader::TEXT || $xml->nodeType == \XMLReader::CDATA) && $xml->hasValue) {
} elseif (($xml->nodeType == \XMLReader::TEXT || $xml->nodeType == \XMLReader::CDATA) && $xml->hasValue) {
$value .= $xml->value;
}
elseif ($this->path == $basePath) {
} elseif ($this->path == $basePath) {
break;
}
}
Expand Down Expand Up @@ -246,7 +245,7 @@ protected function open()
}

if (!empty($this->schema)) {
$schema = (string)$this->schema;
$schema = $this->schema;
if (!$this->XMLReader->setSchema($schema)) {
throw new \Exception("Failed to open XML Schema file '{$schema}'");
}
Expand Down

0 comments on commit ce17295

Please sign in to comment.