Skip to content

Commit

Permalink
Расширил модель Product (для хранения дополнительных полей)
Browse files Browse the repository at this point in the history
  • Loading branch information
dkushnikov committed May 24, 2015
1 parent e82fce0 commit 6dc2a2e
Showing 1 changed file with 44 additions and 27 deletions.
71 changes: 44 additions & 27 deletions src/Model/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,16 @@ class Product extends Model
*/
public $name;

/**
* @var string $sku
*/
public $sku;

/**
* @var string $unit
*/
public $unit;

/**
* @var string $description
*/
Expand All @@ -27,22 +37,22 @@ class Product extends Model
/**
* @var array $price
*/
public $price = array();
public $price = [];

/**
* @var array $categories
*/
public $categories = array();
public $categories = [];

/**
* @var array $requisites
*/
public $requisites = array();
public $requisites = [];

/**
* @var array $properties
*/
public $properties = array();
public $properties = [];

/**
* Class constructor.
Expand All @@ -52,78 +62,84 @@ class Product extends Model
*/
public function __construct($importXml = null, $offersXml = null)
{
$this->name = '';
$this->quantity = 0;
$this->name = '';
$this->quantity = 0;
$this->description = '';

if (! is_null($importXml)) {
if (!is_null($importXml)) {
$this->loadImport($importXml);
}

if (! is_null($offersXml)) {
if (!is_null($offersXml)) {
$this->loadOffers($offersXml);
}
}

/**
* Load primary data from import.xml.
*
* @param SimpleXMLElement $xml
* @param \SimpleXMLElement $xml
*
* @return void
*/
public function loadImport($xml)
{
$this->id = (string) $xml->Ид;
$this->id = trim($xml->Ид);

$this->name = (string) $xml->Наименование;
$this->name = trim($xml->Наименование);
$this->description = trim($xml->Описание);

$this->description = (string) $xml->Описание;
$this->sku = trim($xml->Артикул);
$this->unit = trim($xml->БазоваяЕдиница);

if ($xml->Группы) {
foreach ($xml->Группы->Ид as $categoryId) {
$this->categories[] = (string) $categoryId;
}
$this->categories[] = (string)$categoryId;
}
}

if ($xml->ЗначенияРеквизитов) {
foreach ($xml->ЗначенияРеквизитов->ЗначениеРеквизита as $value) {
$name = (string) $value->Наименование;
$this->requisites[$name] = (string) $value->Значение;
$name = (string)$value->Наименование;
$this->requisites[$name] = (string)$value->Значение;
}
}

if ($xml->ЗначенияСвойств) {
foreach ($xml->ЗначенияСвойств->ЗначенияСвойства as $prop) {
$id = (string) $prop->Ид;

$this->properties[$id] = array(
'valueId' => (string) $prop->Значение
);
$id = (string)$prop->Ид;
$value = (string)$prop->Значение;

if ($value) {
$this->properties[$id] = $value;
}
}
}
}

/**
* Load primary data form offers.xml.
*
* @param SimpleXMLElement $xml
* @param \SimpleXMLElement $xml
*
* @return void
*/
public function loadOffers($xml)
{
if ($xml->Количество) {
$this->quantity = (int) $xml->Количество;
$this->quantity = (int)$xml->Количество;
}

if ($xml->Цены) {
foreach ($xml->Цены->Цена as $price) {
$id = (string) $price->ИдТипаЦены;
$id = (string)$price->ИдТипаЦены;

$this->price[$id] = array(
$this->price[$id] = [
'type' => $id,
'currency' => (string) $price->Валюта,
'value' => (float) $price->ЦенаЗаЕдиницу
);
'currency' => (string)$price->Валюта,
'value' => (float)$price->ЦенаЗаЕдиницу
];
}
}
}
Expand All @@ -132,6 +148,7 @@ public function loadOffers($xml)
* Get price by type.
*
* @param string $type
*
* @return float
*/
public function getPrice($type)
Expand Down

0 comments on commit 6dc2a2e

Please sign in to comment.