Skip to content

Commit

Permalink
Merge pull request #51 from pamkil/master
Browse files Browse the repository at this point in the history
fix YmlAttributes name
  • Loading branch information
pastuhov authored Jun 14, 2018
2 parents 232d410 + 819e726 commit 6687bab
Show file tree
Hide file tree
Showing 9 changed files with 180 additions and 40 deletions.
17 changes: 14 additions & 3 deletions src/DeliveryOptionInterface.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

namespace pastuhov\ymlcatalog;

/**
Expand All @@ -11,17 +12,27 @@ interface DeliveryOptionInterface
{
/**
* Стоимость доставки в рублях.
*
* Для указания бесплатной доставки используйте значение 0.
*
* @return int
*/
public function getCost();

/**
* Срок доставки в рабочих днях.
*
* Можно указать как конкретное количество дней, так и период «от — до». Например, срок доставки от 2 до 4 дней описывается следующим образом: days="2-4".
* Можно указать как конкретное количество дней, так и период «от — до». Например, срок доставки от 2 до 4 дней
* описывается следующим образом: days="2-4".
*
* @return string
*/
public function getDays();

/**
* Время, до которого нужно сделать заказ, чтобы получить его в этот срок.
* Укажите местное время (в часовом поясе магазина), при заказе до которого действует срок доставки. В качестве
* значения используйте только целое число от 0 до 24.
*
* @return int
*/
public function getOrderBefore();
}
33 changes: 33 additions & 0 deletions src/ParamOfferInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace pastuhov\ymlcatalog;

/**
* Элемент param предназначен для описания характеристик и параметров товара.
*
* @link https://yandex.ru/support/partnermarket/param.html
* @package pastuhov\yml
*/
interface ParamOfferInterface
{
/**
* Название параметра (обязательно).
*
* @return string
*/
public function getName();

/**
* Значение параметра.
*
* @return string
*/
public function getValue();

/**
* Единицы измерения (для числовых параметров, опционально).
*
* @return string
*/
public function getUnit();
}
8 changes: 6 additions & 2 deletions src/models/BaseModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class BaseModel extends Model
public static $tag;

/**
* 'key' => 'value'. key = Attribute name in model is optional. `value` tag name in feed
* @var string[]
*/
public static $tagProperties = [];
Expand Down Expand Up @@ -157,8 +158,11 @@ protected function getYmlTagProperties()
$string = '';
$properties = static::$tagProperties;

foreach ($properties as $property) {
$value = $this->getAttributeValue($property);
foreach ($properties as $name => $property) {
if (is_numeric($name)) {
$name = $property;
}
$value = $this->getAttributeValue($name);
if ($value !== null) {
$string .= ' ' . $property . '="' . $value . '"';
}
Expand Down
6 changes: 5 additions & 1 deletion src/models/DeliveryOption.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class DeliveryOption extends BaseModel
public static $tagProperties = [
'cost',
'days',
'orderBefore' => 'order-before'
];

/** @var int Стоимость доставки в рублях. */
Expand All @@ -28,6 +29,9 @@ class DeliveryOption extends BaseModel
/** @var string Срок доставки в рабочих днях. */
public $days;

/** @var int Время, до которого нужно сделать заказ, чтобы получить его в этот срок. */
public $orderBefore;

/**
* @inheritdoc
*/
Expand All @@ -39,7 +43,7 @@ public function rules()
'required',
],
[
['cost'],
['cost', 'orderBefore'],
'integer',
],
[
Expand Down
56 changes: 56 additions & 0 deletions src/models/ParamOffer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

namespace pastuhov\ymlcatalog\models;

/**
* Class ParamOffer
* Модель все важные характеристики товара — цвет, размер, объем, материал, вес, возраст, пол, и т. д.
*
* @package pastuhov\ymlcatalog\models
*/
class ParamOffer extends BaseModel
{
/**
* @inheritdoc
*/
public static $tag = 'param';

/**
* @inheritdoc
*/
public static $tagProperties = [
'name',
'unit',
];

/** @var string Название параметра. */
public $name;

/** @var string Единицы измерения. */
public $unit;

/** @var string Значение параметра. */
public $value;

/**
* @inheritdoc
*/
public function rules()
{
return [
[
['name', 'value'],
'required',
],
[
['name', 'value', 'unit'],
'string',
],
];
}

protected function getYmlBody()
{
return $this->value;
}
}
73 changes: 45 additions & 28 deletions src/models/SimpleOffer.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace pastuhov\ymlcatalog\models;

use pastuhov\ymlcatalog\ParamOfferInterface;
use yii\base\Exception;
use yii\base\InvalidParamException;

Expand Down Expand Up @@ -43,18 +44,18 @@ class SimpleOffer extends BaseModel
public $oldprice;
public $currencyId;
public $categoryId;
public $market_Category;
public $market_category;
public $store;
public $pickup;
public $delivery;
public $local_Delivery_Cost;
public $local_delivery_cost;
public $name;
public $vendor;
public $vendorCode;
public $description;
public $sales_notes;
public $manufacturer_Warranty;
public $country_Of_Origin;
public $manufacturer_warranty;
public $country_of_origin;
public $adult;
public $age;
public $barcode;
Expand All @@ -79,18 +80,18 @@ public function getYmlAttributes()
'oldprice',
'currencyId',
'categoryId',
'market_Category',
'market_category',
'store',
'pickup',
'delivery',
'local_Delivery_Cost',
'local_delivery_cost',
'name',
'vendor',
'vendorCode',
'description',
'sales_notes',
'manufacturer_Warranty',
'country_Of_Origin',
'manufacturer_warranty',
'country_of_origin',
'adult',
'age',
'barcode',
Expand Down Expand Up @@ -124,7 +125,7 @@ public function rules()
'max' => static::$maxLengthName,
],
[
['delivery', 'pickup', 'store', 'manufacturer_Warranty', 'adult'],
['delivery', 'pickup', 'store', 'manufacturer_warranty', 'adult'],
'boolean',
'trueValue' => 'true',
'falseValue' => 'false',
Expand All @@ -139,15 +140,15 @@ public function rules()
'integer',
],
[
['name', 'market_Category', 'vendorCode', 'country_Of_Origin', 'barcode'],
['name', 'market_category', 'vendorCode', 'country_of_origin', 'barcode'],
'string',
],
[
['url'],
'url',
],
[
['price', 'oldprice', 'local_Delivery_Cost'],
['price', 'oldprice', 'local_delivery_cost'],
'double',
],
[
Expand Down Expand Up @@ -177,11 +178,6 @@ function ($attribute, $params) {
}
}
],
[
['params'],
'each',
'rule' => ['string']
],
[
['pictures'],
'each',
Expand All @@ -208,12 +204,11 @@ public function setPictures(array $pictures)

/**
* @param array $options
*
* @throws Exception
*/
public function setDeliveryOptions(array $options)
{
if(count($options) > 5) {
if (count($options) > 5) {
throw new InvalidParamException('Maximum count of delivery options array is 5');
}
$this->deliveryOptions = $options;
Expand All @@ -230,41 +225,37 @@ protected function getYmlBody()
$string .= $this->getYmlAttribute($attribute);
}

foreach ($this->params as $name => $value) {
$string .= $this->getYmlParamTag($name, $value);
}

foreach ($this->pictures as $picture) {
$string .= $this->getYmlPictureTag($picture);
}

$this->appendParamTags($string);
$this->appendDeliveryOptions($string);

return $string;
}

/**
* Добавляет теги ддля опций доставки
* Добавляет теги для опций доставки
*
* @param $string
*
* @throws Exception
*/
protected function appendDeliveryOptions(&$string) {
if(count($this->deliveryOptions) < 1) {
protected function appendDeliveryOptions(&$string)
{
if (count($this->deliveryOptions) < 1) {
return;
}
$string .= '<delivery-options>' . PHP_EOL;
$deliveryOptionBase = new DeliveryOption();

foreach($this->deliveryOptions as $deliveryOption) {
foreach ($this->deliveryOptions as $deliveryOption) {
$deliveryOptionBase->loadModel($deliveryOption);
$string .= $deliveryOptionBase->getYml();
}
$string .= '</delivery-options>' . PHP_EOL;
}


/**
* @param string $attribute
* @param string $value
Expand All @@ -281,6 +272,32 @@ protected function getYmlParamTag($attribute, $value)
return $string;
}

/**
* Добавляет теги для описания характеристик и параметров товара.
*
* @param string $string
* @throws Exception
*/
protected function appendParamTags(&$string)
{
if (count($this->params) < 1) {
return;
}

$paramOfferBase = new ParamOffer();

foreach ($this->params as $name => $param) {
if (!($param instanceof ParamOfferInterface)) {
if (is_string($param)) {
$string .= $this->getYmlParamTag($name, $param);
}
continue;
}
$paramOfferBase->loadModel($param);
$string .= $paramOfferBase->getYml();
}
}

/**
* @param string $value
* @return string
Expand Down
6 changes: 3 additions & 3 deletions tests/data/yml-catalog-custom-category.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
<category id="101" parentId="10" portalid="portal-test">Лазерные принтеры</category>
</categories>
<delivery-options>
<option cost="0" days="1-2" />
<option cost="0" days="1-2" order-before="13"/>
</delivery-options>
<offers>
<offer id="12" bid="13" cbid="20" available="false"><url>http://magazin.ru/product_page.asp?pid=12</url>
Expand All @@ -60,8 +60,8 @@
<sales_notes>по предоплате</sales_notes>
<picture>http://magazin.ru/img/device12341.jpg</picture>
<delivery-options>
<option cost="123" days="2" />
<option cost="100" days="1" />
<option cost="123" days="2" order-before="13"/>
<option cost="100" days="1" order-before="13"/>
</delivery-options>
</offer>
</offers>
Expand Down
Loading

0 comments on commit 6687bab

Please sign in to comment.