Skip to content

Commit

Permalink
Added getData functions
Browse files Browse the repository at this point in the history
  • Loading branch information
lireincore committed Sep 16, 2016
1 parent 303183b commit 8bb76eb
Show file tree
Hide file tree
Showing 11 changed files with 111 additions and 54 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@

## Install

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

or

``` bash
$ composer require lireincore/ymlparser
Expand All @@ -24,9 +26,12 @@ 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();
//...
}
} catch (\Exception $e) {
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"name": "lireincore/ymlparser",
"type": "library",
"description": "YML (Yandex Market Language) parser",
"version": "2.1.0",
"type": "library",
"keywords": ["yml", "parser", "yandex", "market"],
"homepage": "https://github.com/lireincore/ymlparser",
"license": "MIT",
Expand Down
7 changes: 3 additions & 4 deletions src/Category.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

class Category
{
use TYML;

/**
* @var int
*/
Expand All @@ -26,10 +28,7 @@ class Category
public function setAttributes($attributes)
{
foreach ($attributes as $name => $value) {
$setter = 'set' . str_replace(['-', '_'], '', $name);
if (method_exists($this, $setter)) {
$this->$setter($value);
}
$this->setField($name, $value);
}

return $this;
Expand Down
7 changes: 3 additions & 4 deletions src/Currency.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

class Currency
{
use TYML;

const CURRENCY_RUR = 'RUR';
const CURRENCY_RUB = 'RUB';
const CURRENCY_UAH = 'UAH';
Expand Down Expand Up @@ -39,10 +41,7 @@ class Currency
public function setAttributes($attributes)
{
foreach ($attributes as $name => $value) {
$setter = 'set' . str_replace(['-', '_'], '', $name);
if (method_exists($this, $setter)) {
$this->$setter($value);
}
$this->setField($name, $value);
}

return $this;
Expand Down
7 changes: 3 additions & 4 deletions src/DeliveryOption.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

class DeliveryOption
{
use TYML;

/**
* @var int
*/
Expand All @@ -26,10 +28,7 @@ class DeliveryOption
public function setAttributes($attributes)
{
foreach ($attributes as $name => $value) {
$setter = 'set' . str_replace(['-', '_'], '', $name);
if (method_exists($this, $setter)) {
$this->$setter($value);
}
$this->setField($name, $value);
}

return $this;
Expand Down
19 changes: 4 additions & 15 deletions src/Offer/AOffer.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@

namespace LireinCore\YMLParser\Offer;

use LireinCore\YMLParser\TYML;

abstract class AOffer
{
use TYML;

/**
* @var string
*/
Expand Down Expand Up @@ -183,21 +187,6 @@ public function setAttribute(array $attrNode)
return $this;
}

/**
* @param string $name
* @param mixed $value
* @return $this
*/
public function setField($name, $value)
{
$setter = 'set' . str_replace(['-', '_'], '', $name);
if (method_exists($this, $setter)) {
$this->$setter($value);
}

return $this;
}

/**
* @return string
*/
Expand Down
13 changes: 7 additions & 6 deletions src/Offer/Outlet.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,19 @@

namespace LireinCore\YMLParser\Offer;

use LireinCore\YMLParser\TYML;

class Outlet
{
use TYML;

/**
* @var string //todo int?
* @var string //todo: int?
*/
protected $id;

/**
* @var int //todo out of range?
* @var int //todo: out of range?
*/
protected $instock = 0;

Expand All @@ -26,10 +30,7 @@ class Outlet
public function setAttributes($attributes)
{
foreach ($attributes as $name => $value) {
$setter = 'set' . str_replace(['-', '_'], '', $name);
if (method_exists($this, $setter)) {
$this->$setter($value);
}
$this->setField($name, $value);
}

return $this;
Expand Down
9 changes: 5 additions & 4 deletions src/Offer/Param.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@

namespace LireinCore\YMLParser\Offer;

use LireinCore\YMLParser\TYML;

class Param
{
use TYML;

/**
* @var string
*/
Expand All @@ -26,10 +30,7 @@ class Param
public function setAttributes($attributes)
{
foreach ($attributes as $name => $value) {
$setter = 'set' . str_replace(['-', '_'], '', $name);
if (method_exists($this, $setter)) {
$this->$setter($value);
}
$this->setField($name, $value);
}

return $this;
Expand Down
18 changes: 9 additions & 9 deletions src/Shop.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

class Shop
{
use TYML {
getData as private getDataT;
}

/**
* @var string
*/
Expand Down Expand Up @@ -130,18 +134,14 @@ public function setAttribute(array $attrNode)
}

/**
* @param string $name
* @param mixed $value
* @return $this
* @return array
*/
public function setField($name, $value)
public function getData()
{
$setter = 'set' . str_replace(['-', '_'], '', $name);
if (method_exists($this, $setter)) {
$this->$setter($value);
}
$data = $this->getDataT();
unset($data['offersCount']);

return $this;
return $data;
}

/**
Expand Down
69 changes: 69 additions & 0 deletions src/TYML.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php
namespace LireinCore\YMLParser;

trait TYML
{
/**
* @param string $name
* @param mixed $value
* @return $this
*/
public function setField($name, $value)
{
$setter = 'set' . str_replace(['-', '_'], '', $name);
if (method_exists($this, $setter)) {
$this->$setter($value);
}

return $this;
}

/**
* @return array
*/
public function getData()
{
$array = [];

foreach ($this as $key => $value) {
if (!is_null($value)) {
if (is_object($value) && method_exists($value, 'getData')) {
$array[$key] = $value->getData();
}
elseif (is_array($value)) {
if (!empty($value)) {
$array[$key] = $this->getArray($value);
}
}
else {
$array[$key] = $value;
}
}
}

return $array;
}

/**
* @param array $value
* @return array
*/
private function getArray($value)
{
$subarray = [];

foreach ($value as $subkey => $subvalue) {
if (is_object($subvalue) && method_exists($subvalue, 'getData')) {
$subarray[$subkey] = $subvalue->getData();
}
elseif (is_array($subvalue)) {
$subarray[$subkey] = $this->getArray($subvalue);
}
else {
$subarray[$subkey] = $subvalue;
}
}

return $subarray;
}
}
6 changes: 0 additions & 6 deletions src/YML.php
Original file line number Diff line number Diff line change
Expand Up @@ -228,12 +228,6 @@ protected function open($file)
throw new \Exception("File '{$file}' not found");
}

$dom = new \DOMDocument();
if(!@$dom->load($file)) {
throw new \Exception("'{$file}' is invalid XML document");
}
unset($dom);

if (!$this->XMLReader->open($file)) {
throw new \Exception("Failed to open file '{$file}'");
}
Expand Down

0 comments on commit 8bb76eb

Please sign in to comment.