Skip to content

Commit

Permalink
Merge pull request #46 from des1roer/master
Browse files Browse the repository at this point in the history
Пользовательский класс для подмены models/Category.php
  • Loading branch information
pastuhov authored Sep 14, 2017
2 parents ab1e03a + b2c3e07 commit 083a596
Show file tree
Hide file tree
Showing 7 changed files with 202 additions and 3 deletions.
11 changes: 11 additions & 0 deletions src/CustomCategoryInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php
namespace pastuhov\ymlcatalog;

/**
* Пользовательская категория.
*
* @package pastuhov\yml
*/
interface CustomCategoryInterface extends CategoryInterface
{
}
14 changes: 12 additions & 2 deletions src/YmlCatalog.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ class YmlCatalog
*/
protected $customOfferClass;

/**
* @var null|string
*/
protected $customCategoryClass;

/**
* @var null|string
*/
Expand All @@ -75,6 +80,7 @@ class YmlCatalog
* @param null|string $date
* @param null|callable $onValidationError
* @param null|string $customOfferClass
* @param null|string $customCategoryClass
*/
public function __construct(
BaseFileStream $handle,
Expand All @@ -86,7 +92,8 @@ public function __construct(
$date = null,
$onValidationError = null,
$customOfferClass = null,
$deliveryOptionClass = null
$deliveryOptionClass = null,
$customCategoryClass = null
) {
$this->handle = $handle;
$this->shopClass = $shopClass;
Expand All @@ -98,6 +105,7 @@ public function __construct(
$this->onValidationError = $onValidationError;
$this->customOfferClass = $customOfferClass;
$this->deliveryOptionClass = $deliveryOptionClass;
$this->customCategoryClass = $customCategoryClass;
}

/**
Expand Down Expand Up @@ -261,6 +269,8 @@ protected function getNewModel($modelClass)

if ($obj instanceof CurrencyInterface) {
$model = new Currency();
} elseif ($obj instanceof CustomCategoryInterface && !empty($this->customCategoryClass) && class_exists($this->customCategoryClass)) {
$model = \Yii::createObject($this->customCategoryClass);
} elseif ($obj instanceof CategoryInterface) {
$model = new Category();
} elseif ($obj instanceof CustomOfferInterface && $this->customOfferClass !== null && class_exists($this->customOfferClass)) {
Expand All @@ -273,7 +283,7 @@ protected function getNewModel($modelClass)

return $model;
}

/**
* Performs PHP memory garbage collection.
*/
Expand Down
8 changes: 7 additions & 1 deletion src/actions/GenerateAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class GenerateAction extends Action

/**
* Publish yml and .gz
*
*
* @var bool
*/
public $keepBoth = false;
Expand Down Expand Up @@ -71,6 +71,11 @@ class GenerateAction extends Action
*/
public $customOfferClass;

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

/**
* @var string
*/
Expand Down Expand Up @@ -111,6 +116,7 @@ public function run()
null,
$this->onValidationError,
$this->customOfferClass,
$this->customCategoryClass,
$this->deliveryOptionClass
);
$generator->generate();
Expand Down
36 changes: 36 additions & 0 deletions tests/YmlCatalogTest.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -204,4 +204,40 @@ public static function setUpBeforeClass()
Yii::$app->clear('db');
}
}

/**
* Тест для пользовательской категории.
*/
public function testCustomCategory()
{
$handle = new BaseFileStream(__DIR__ . '/runtime/yml-catalog.xml');

$generator = new YmlCatalog(
$handle,
'pastuhov\ymlcatalog\Test\models\Shop',
'pastuhov\ymlcatalog\Test\models\Currency',
'pastuhov\ymlcatalog\Test\models\CustomCategory',
null,
[
[
'class' => 'pastuhov\ymlcatalog\Test\models\SimpleOffer',
'findParams' => [
'excluded' => [
13
]
]
]
],
'2015-01-01 14:00',
function () {

},
null,
'pastuhov\ymlcatalog\Test\models\DeliveryOption',
'pastuhov\ymlcatalog\Test\models\SatomCategoryClass'
);
$generator->generate();

$this->assertXmlFileEqualsXmlFile(__DIR__ . '/data/yml-catalog-custom-category.xml', __DIR__ . '/runtime/yml-catalog.xml');
}
}
69 changes: 69 additions & 0 deletions tests/data/yml-catalog-custom-category.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE yml_catalog SYSTEM "shops.dtd">
<yml_catalog date="2015-01-01 14:00">
<shop>
<name>BestShop</name>
<company>Best online seller Inc.</company>
<url>http://best.seller.ru/</url>
<platform>CMS</platform>
<version>2.3</version>
<agency>Agency</agency>
<email>[email protected]</email>

<currencies>
<currency id="RUR" rate="1"/>
<currency id="EUR" rate="CBRF"/>
<currency id="UAH" rate="NBU" plus="10"/>
</currencies>
<categories>
<category id="1" portalid="portal-test">Оргтехника</category>
<category id="2" portalid="portal-test">Фототехника</category>
<category id="3" portalid="portal-test">Книги</category>
<category id="4" portalid="portal-test">Музыка и видеофильмы</category>
<category id="5" portalid="portal-test">Путешествия</category>
<category id="6" portalid="portal-test">Билеты на мероприятия</category>
<category id="10" parentId="1" portalid="portal-test">Принтеры</category>
<category id="11" parentId="2" portalid="portal-test">Фотоаппараты</category>
<category id="12" parentId="2" portalid="portal-test">Объективы</category>
<category id="13" parentId="3" portalid="portal-test">Детективы</category>
<category id="14" parentId="3" portalid="portal-test">Художественная литература</category>
<category id="15" parentId="3" portalid="portal-test">Учебная литература</category>
<category id="16" parentId="3" portalid="portal-test">Детская литература</category>
<category id="17" parentId="4" portalid="portal-test">Музыка</category>
<category id="18" parentId="4" portalid="portal-test">Видеофильмы</category>
<category id="19" parentId="5" portalid="portal-test">Туры</category>
<category id="20" parentId="5" portalid="portal-test">Авиабилеты</category>
<category id="100" parentId="10" portalid="portal-test">Струйные принтеры</category>
<category id="101" parentId="10" portalid="portal-test">Лазерные принтеры</category>
</categories>
<delivery-options>
<option cost="0" days="1-2" />
</delivery-options>
<offers>
<offer id="12" bid="13" cbid="20" available="false"><url>http://magazin.ru/product_page.asp?pid=12</url>
<price>1000</price>
<currencyId>RUR</currencyId>
<categoryId>10</categoryId>
<delivery>true</delivery>
<name>Лазернй принтер 234234234&quot;&amp;&gt;&lt;'</name>
<description>Крутой принтер на лазере.</description>
<picture>http://magazin.ru/img/device12.jpg</picture>
<picture>http://magazin.ru/img/device12_2.jpg</picture>
</offer>
<offer id="12341" bid="13" cbid="20" available="true"><url>http://magazin.ru/product_page.asp?pid=12341</url>
<price>15000</price>
<currencyId>RUR</currencyId>
<categoryId>1</categoryId>
<delivery>true</delivery>
<name>Наручные часы Casio A1234567B</name>
<description>Изящные наручные часы.</description>
<sales_notes>по предоплате</sales_notes>
<picture>http://magazin.ru/img/device12341.jpg</picture>
<delivery-options>
<option cost="123" days="2" />
<option cost="100" days="1" />
</delivery-options>
</offer>
</offers>
</shop>
</yml_catalog>
30 changes: 30 additions & 0 deletions tests/models/CustomCategory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php
namespace pastuhov\ymlcatalog\Test\models;

use pastuhov\ymlcatalog\CustomCategoryInterface;

/**
* @inheritdoc
*
* @property string name
*/
class CustomCategory extends Category implements CustomCategoryInterface
{
/**
* @inheritdoc
*/
public static function tableName()
{
return 'category';
}

/**
* Тестовый пользовательский атрибут.
*
* @return string
*/
public function getPortalid()
{
return 'portal-test';
}
}
37 changes: 37 additions & 0 deletions tests/models/SatomCategoryClass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php
namespace pastuhov\ymlcatalog\Test\models;

use yii\helpers\ArrayHelper;

/**
* Пример пользовательского класса с собственным атрибутом.
*/
class SatomCategoryClass extends \pastuhov\ymlcatalog\models\Category
{
/**
* @inheritdoc
*/
public static $tagProperties = [
'id',
'parentId',
'portalid',
];

/**
* @var Ид категории, сопоставленный категории сайта.
*/
public $portalid;

/**
* @inheritdoc
*/
public function rules()
{
return ArrayHelper::merge(
parent::rules(),
[
['portalid', 'string'],
]
);
}
}

0 comments on commit 083a596

Please sign in to comment.