Skip to content

Commit

Permalink
Make mapping more flexible (based on DivanteLtd#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
cewald committed Nov 14, 2019
1 parent e10dd2f commit 2c46678
Show file tree
Hide file tree
Showing 17 changed files with 147 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,24 @@ final public function filterDto($initialDto)
}
}

$this->callMapperExtensions($dto);

return $dto;
}

/**
* Allow to extend the mappers using config.xml
*
* @param array $dto
* @return array
*/
protected function callMapperExtensions(&$dto)
{
/** @var Divante_VueStorefrontBridge_Model_Config_Mapper $model */
$model = Mage::getModel('vsbridge/config_mapper');
return $model->applyExtendedMapping(self::MAPPER_IDENTIFIER, $dto);
}

/**
* Apply custom dto filtering
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@
*/
class Divante_VueStorefrontBridge_Helper_Mapper_Address extends Divante_VueStorefrontBridge_Helper_Mapper_Abstract
{
/**
* Name to address custom mappers via config.xml
*/
const MAPPER_IDENTIFIER = 'address';

/**
* @inheritdoc
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@
*/
class Divante_VueStorefrontBridge_Helper_Mapper_Category extends Divante_VueStorefrontBridge_Helper_Mapper_Abstract
{
/**
* Name to address custom mappers via config.xml
*/
const MAPPER_IDENTIFIER = 'category';

/**
* @inheritdoc
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@
*/
class Divante_VueStorefrontBridge_Helper_Mapper_Customer extends Divante_VueStorefrontBridge_Helper_Mapper_Abstract
{
/**
* Name to address custom mappers via config.xml
*/
const MAPPER_IDENTIFIER = 'customer';

/**
* @inheritdoc
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@
*/
class Divante_VueStorefrontBridge_Helper_Mapper_Order extends Divante_VueStorefrontBridge_Helper_Mapper_Abstract
{
/**
* Name to address custom mappers via config.xml
*/
const MAPPER_IDENTIFIER = 'order';

/**
* @inheritdoc
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@
*/
class Divante_VueStorefrontBridge_Helper_Mapper_OrderItem extends Divante_VueStorefrontBridge_Helper_Mapper_Abstract
{
/**
* Name to address custom mappers via config.xml
*/
const MAPPER_IDENTIFIER = 'order_item';

/**
* @inheritdoc
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,8 @@
*/
class Divante_VueStorefrontBridge_Helper_Mapper_Payment extends Divante_VueStorefrontBridge_Helper_Mapper_Abstract
{
/**
* Name to address custom mappers via config.xml
*/
const MAPPER_IDENTIFIER = 'payment';
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,8 @@
*/
class Divante_VueStorefrontBridge_Helper_Mapper_Product extends Divante_VueStorefrontBridge_Helper_Mapper_Abstract
{
/**
* Name to address custom mappers via config.xml
*/
const MAPPER_IDENTIFIER = 'product';
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@
*/
class Divante_VueStorefrontBridge_Helper_Mapper_ProductAttribute extends Divante_VueStorefrontBridge_Helper_Mapper_Abstract
{
/**
* Name to address custom mappers via config.xml
*/
const MAPPER_IDENTIFIER = 'product_attribute';

/**
* @inheritdoc
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@
*/
class Divante_VueStorefrontBridge_Helper_Mapper_ProductChild extends Divante_VueStorefrontBridge_Helper_Mapper_Abstract
{
/**
* Name to address custom mappers via config.xml
*/
const MAPPER_IDENTIFIER = 'product_child';

/**
* @inheritdoc
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@
*/
class Divante_VueStorefrontBridge_Helper_Mapper_QuoteItem extends Divante_VueStorefrontBridge_Helper_Mapper_Abstract
{
/**
* Name to address custom mappers via config.xml
*/
const MAPPER_IDENTIFIER = 'quote_item';

/**
* @inheritdoc
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@
*/
class Divante_VueStorefrontBridge_Helper_Mapper_Stock extends Divante_VueStorefrontBridge_Helper_Mapper_Abstract
{
/**
* Name to address custom mappers via config.xml
*/
const MAPPER_IDENTIFIER = 'stock';

/**
* @inheritdoc
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@
*/
class Divante_VueStorefrontBridge_Helper_Mapper_Taxrule extends Divante_VueStorefrontBridge_Helper_Mapper_Abstract
{
/**
* Name to address custom mappers via config.xml
*/
const MAPPER_IDENTIFIER = 'taxrule';

/**
* @inheritdoc
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@
*/
class Divante_VueStorefrontBridge_Helper_Mapper_TaxruleRate extends Divante_VueStorefrontBridge_Helper_Mapper_Abstract
{
/**
* Name to address custom mappers via config.xml
*/
const MAPPER_IDENTIFIER = 'taxrule_rate';

/**
* @inheritdoc
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,8 @@
*/
class Divante_VueStorefrontBridge_Helper_Mapper_WhishListItem extends Divante_VueStorefrontBridge_Helper_Mapper_Abstract
{
/**
* Name to address custom mappers via config.xml
*/
const MAPPER_IDENTIFIER = 'whishlist_item';
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

/**
* Class Divante_VueStorefrontBridge_Model_Config_Mapper
*/
class Divante_VueStorefrontBridge_Model_Config_Mapper
{
/**
* Mapper xml config path
*/
const MAPPER_CONFIG_ROOT_NODE = 'global/vsf_bridge/mapper/types';

/**
* @var array
*/
protected $config = array();

/**
* @return array
*/
public function __construct()
{
$mappingConfigTypes = Mage::getConfig()->getNode(self::MAPPER_CONFIG_ROOT_NODE)->asArray();
foreach ($mappingConfigTypes as $mappingIdentifier => $mapper) {
$this->config[$mappingIdentifier] = $mapper['mapper'];
}

return $this->config;
}

/**
* @param string $type
* @param array $initialDto
* @return array
*/
public function applyExtendedMapping($type, &$initialDto)
{
if ($this->config[$type] && !empty($this->config[$type])) {
foreach ($this->config[$type] as $name => $className) {
$model = Mage::getModel($className);
if (!$model || !$model instanceof Divante_VueStorefrontBridge_Helper_Mapper_Abstract) {
$error = 'Class "%s" is not an instance on "Divante_VueStorefrontBridge_Helper_Mapper_Abstract".';
Mage::log(sprintf($error, $className), 'system.log', true);
continue;
}

$initialDto = $model->filterDto($initialDto);
}
}

return $initialDto;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,18 @@
<class>Divante_VueStorefrontBridge_Helper_Mapper</class>
</vsbridge_mapper>
</helpers>
<vsf_bridge>
<mapper>
<types>
<!--address>
<mapper>
<add_custom_attribute_mapping>Divante_VueStorefrontIndexer_Helper_Mapper_CustomClass</add_custom_attribute_mapping>
<filter_another_attribute>Divante_VueStorefrontIndexer_Helper_Mapper_SecondCustomClass</filter_another_attribute>
</mapper>
</address-->
</type>
</mapper>
</vsf_bridge>
</global>
<default>
<vsbridge>
Expand Down

0 comments on commit 2c46678

Please sign in to comment.