Skip to content

Commit

Permalink
Added validation, fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
lireincore committed Sep 28, 2016
1 parent 8bb76eb commit d95c617
Show file tree
Hide file tree
Showing 24 changed files with 1,056 additions and 454 deletions.
29 changes: 21 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
# YML (Yandex Market Language) parser

[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE)
[![Latest Stable Version](https://poser.pugx.org/lireincore/ymlparser/v/stable)](https://packagist.org/packages/lireincore/ymlparser)
[![Total Downloads](https://poser.pugx.org/lireincore/ymlparser/downloads)](https://packagist.org/packages/lireincore/ymlparser)
[![License](https://poser.pugx.org/lireincore/ymlparser/license)](https://packagist.org/packages/lireincore/ymlparser)

## About

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

## Install

Expand All @@ -26,12 +29,22 @@ try {
$yml->parse($filepath);
$date = $yml->getDate();
$shop = $yml->getShop();
$offersCount = $shop->getOffersCount();
$shopData = $shop->getData();
/**@var \LireinCore\YMLParser\Offer\AOffer $offer*/
foreach ($yml->getOffers() as $offer) {
$offerCategoryHierarchy = $shop->getCategoryHierarchy($offer->getCategoryId);
$offerData = $offer->getData();
if ($shop->isValid) {
$offersCount = $shop->getOffersCount();
$shopData = $shop->getData();
//...
foreach ($yml->getOffers() as $offer) {
if ($offer->isValid) {
$offerCategoryHierarchy = $shop->getCategoryHierarchy($offer->getCategoryId);
$offerData = $offer->getData();
//...
} else {
var_dump($offer->getErrors());
//...
}
}
} else {
var_dump($shop->getErrors());
//...
}
} catch (\Exception $e) {
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "lireincore/ymlparser",
"description": "YML (Yandex Market Language) parser",
"version": "2.1.0",
"version": "3.0.0",
"type": "library",
"keywords": ["yml", "parser", "yandex", "market"],
"homepage": "https://github.com/lireincore/ymlparser",
Expand Down
42 changes: 31 additions & 11 deletions src/Category.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@
class Category
{
use TYML;
use TError;

/**
* @var int
* @var string
*/
protected $id;

/**
* @var int
* @var string
*/
protected $parentId;

Expand All @@ -21,6 +22,25 @@ class Category
*/
protected $name;

/**
* @return bool
*/
public function isValid()
{
if ($this->id === null)
$this->setError("Category: missing required attribute 'id'");
elseif (!is_numeric($this->id) || (int)$this->id <= 0)
$this->setError("Category: incorrect value in attribute 'id'");

if ($this->parentId !== null && (!is_numeric($this->parentId) || (int)$this->parentId <= 0))
$this->setError("Category: incorrect value in attribute 'parentId'");

if (!$this->name)
$this->setError("Category: incorrect value");

return empty($this->errors);
}

/**
* @param array $attributes
* @return $this
Expand All @@ -35,30 +55,30 @@ public function setAttributes($attributes)
}

/**
* @return int
* @return int|null
*/
public function getId()
{
return $this->id;
return $this->id === null ? null : (int)$this->id;
}

/**
* @param int $value
* @param string $value
* @return $this
*/
public function setId($value)
{
$this->id = (int)$value;
$this->id = $value;

return $this;
}

/**
* @return int
* @return int|null
*/
public function getParentId()
{
return $this->parentId;
return $this->parentId === null ? null : (int)$this->parentId;
}

/**
Expand All @@ -67,13 +87,13 @@ public function getParentId()
*/
public function setParentId($value)
{
$this->parentId = (int)$value;
$this->parentId = $value;

return $this;
}

/**
* @return string
* @return string|null
*/
public function getName()
{
Expand All @@ -86,7 +106,7 @@ public function getName()
*/
public function setName($value)
{
$this->name = (string)$value;
$this->name = $value;

return $this;
}
Expand Down
61 changes: 36 additions & 25 deletions src/Currency.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,34 +5,45 @@
class Currency
{
use TYML;
use TError;

const CURRENCY_RUR = 'RUR';
const CURRENCY_RUB = 'RUB';
const CURRENCY_UAH = 'UAH';
const CURRENCY_BYN = 'BYN';
const CURRENCY_KZT = 'KZT';
const CURRENCY_USD = 'USD';
const CURRENCY_EUR = 'EUR';

const EXCHANGE_RATE_CBRF = 'CBRF';
const EXCHANGE_RATE_NBU = 'NBU';
const EXCHANGE_RATE_NBK = 'NBK';
const EXCHANGE_RATE_CB = 'CB';
const DEFAULT_PLUS = 0;

/**
* @var string
*/
protected $id;

/**
* @var float|string
* @var string
*/
protected $rate;

/**
* @var int
* @var string
*/
protected $plus = 0;
protected $plus;

/**
* @return bool
*/
public function isValid()
{
if ($this->id === null)
$this->setError("Currency: missing required attribute 'id'");
elseif (!in_array($this->id, ['RUR', 'RUB', 'UAH', 'BYN', 'BYR', 'KZT', 'USD', 'EUR']))
$this->setError("Currency: incorrect value in attribute 'id'");

if ($this->rate === null)
$this->setError("Currency: missing required attribute 'rate'");
elseif (!(in_array($this->rate, ['CBRF', 'NBU', 'NBK', 'CB']) || (is_numeric($this->rate) && (float)$this->rate > 0)))
$this->setError("Currency: incorrect value in attribute 'rate'");

if ($this->plus !== null && (!is_numeric($this->rate) || (int)$this->plus < 0))
$this->setError("Currency: incorrect value in attribute 'plus'");

return empty($this->errors);
}

/**
* @param array $attributes
Expand All @@ -48,7 +59,7 @@ public function setAttributes($attributes)
}

/**
* @return string
* @return string|null
*/
public function getId()
{
Expand All @@ -61,45 +72,45 @@ public function getId()
*/
public function setId($value)
{
$this->id = (string)$value;
$this->id = $value;

return $this;
}

/**
* @return float|string
* @return float|string|null
*/
public function getRate()
{
return $this->rate;
return is_numeric($this->rate) ? (float)$this->rate : $this->rate;
}

/**
* @param float|string $value
* @param string $value
* @return $this
*/
public function setRate($value)
{
$this->rate = is_numeric($value) ? (float)$value : (string)$value;
$this->rate = $value;

return $this;
}

/**
* @return int
* @return int|null
*/
public function getPlus()
{
return $this->plus;
return $this->plus === null ? null : (int)$this->plus;
}

/**
* @param int $value
* @param string $value
* @return $this
*/
public function setPlus($value)
{
$this->plus = (int)$value;
$this->plus = $value;

return $this;
}
Expand Down
46 changes: 34 additions & 12 deletions src/DeliveryOption.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@
class DeliveryOption
{
use TYML;
use TError;

const DEFAULT_ORDER_BEFORE = 24;

/**
* @var int
* @var string
*/
protected $cost;

Expand All @@ -17,10 +20,29 @@ class DeliveryOption
protected $days;

/**
* @var int
* @var string
*/
protected $orderBefore;

/**
* @return bool
*/
public function isValid()
{
if ($this->cost === null)
$this->setError("DeliveryOption: missing required attribute 'cost'");
elseif (!is_numeric($this->cost) || ((int)$this->cost) < 0)
$this->setError("DeliveryOption: incorrect value in attribute 'cost'");

if ($this->days === null)
$this->setError("DeliveryOption: missing required attribute 'days'");

if ($this->orderBefore !== null && (!is_numeric($this->orderBefore) || (int)$this->orderBefore < 0 || (int)$this->orderBefore > 24))
$this->setError("DeliveryOption: incorrect value in attribute 'order-before'");

return empty($this->errors);
}

/**
* @param array $attributes
* @return $this
Expand All @@ -35,26 +57,26 @@ public function setAttributes($attributes)
}

/**
* @return int
* @return int|null
*/
public function getCost()
{
return $this->cost;
return $this->cost === null ? null : (int)$this->cost;
}

/**
* @param int $value
* @param string $value
* @return $this
*/
public function setCost($value)
{
$this->cost = (int)$value;
$this->cost = $value;

return $this;
}

/**
* @return string
* @return string|null
*/
public function getDays()
{
Expand All @@ -67,26 +89,26 @@ public function getDays()
*/
public function setDays($value)
{
$this->days = (string)$value;
$this->days = $value;

return $this;
}

/**
* @return int
* @return int|null
*/
public function getOrderBefore()
{
return $this->orderBefore;
return $this->orderBefore === null ? null : (int)$this->orderBefore;
}

/**
* @param int $value
* @param string $value
* @return $this
*/
public function setOrderBefore($value)
{
$this->orderBefore = (int)$value;
$this->orderBefore = $value;

return $this;
}
Expand Down
Loading

0 comments on commit d95c617

Please sign in to comment.