Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add mapper to fit response with needed schema and add a way to extend it #49

Closed
wants to merge 17 commits into from
Closed
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
<?php
/**
* Divante VueStorefrontBridge AbstractMapper Class
*
* @category Divante
* @package VueStorefrontBridge
* @author Mathias Arlaud <[email protected]>
* @copyright Copyright (C) 2019
* @license MIT License
*/
abstract class Divante_VueStorefrontBridge_Helper_Mapper_Abstract extends Mage_Core_Helper_Abstract
{
/**
* Name to address custom mappers via config.xml
* @var string $_mapperIdentifier
*/
protected $_mapperIdentifier = '';

/**
* Get and process Dto from entity
*
* @param array $initialDto
* @param bool $callMapperExtensions
*
* @return array
*/
final public function filterDto($initialDto, $callMapperExtensions = true)
{
$dto = $this->customDtoFiltering($initialDto);

foreach ($dto as $key => $value) {
$isCustom = false;

if (in_array($key, $this->getBlacklist())) {
unset($dto[$key]);
continue;
}

if (in_array($key, $this->getAttributesToCastInt())) {
$dto[$key] = $dto[$key] !== null ? intval($dto[$key]) : null;
$isCustom = true;
}

if (in_array($key, $this->getAttributesToCastBool())) {
$dto[$key] = $dto[$key] !== null ? boolval($dto[$key]) : null;
$isCustom = true;
}

if (in_array($key, $this->getAttributesToCastStr())) {
$dto[$key] = $dto[$key] !== null ? strval($dto[$key]) : null;
$isCustom = true;
}

if (in_array($key, $this->getAttributesToCastFloat())) {
$dto[$key] = $dto[$key] !== null ? floatval($dto[$key]) : null;
$isCustom = true;
}

if (!$isCustom) {
// If beginning by "use", "has", ... , then must be a boolean (can be overriden using cast array)
if (preg_match('#^(use|has|is|enable|disable)_.*$#', $key)) {
$dto[$key] = $dto[$key] !== null ? boolval($value) : null;
}

// If ending by "id", then must be an integer (can be overriden using cast array)
if (preg_match('#^.*_?id$#', $key)) {
$dto[$key] = $dto[$key] !== null ? intval($value) : null;
}
}
}

if ($callMapperExtensions) {
$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($this->_mapperIdentifier, $dto);
}

/**
* Apply custom dto filtering
*
* @param array $dto
*
* @return array
*/
protected function customDtoFiltering($dto)
{
return $dto;
}

/**
* Get Dto attribute blacklist
*
* @return array
*/
protected function getBlacklist()
{
return [];
}

/**
* Get attribute list to cast to integer
*
* @return array
*/
protected function getAttributesToCastInt()
{
return [];
}

/**
* Get attribute list to cast to boolean
*
* @return array
*/
protected function getAttributesToCastBool()
{
return [];
}

/**
* Get attribute list to cast to string
*
* @return array
*/
protected function getAttributesToCastStr()
{
return [];
}

/**
* Get attribute list to cast to float
*
* @return array
*/
protected function getAttributesToCastFloat()
{
return [];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

/**
* Divante VueStorefrontBridge AddressMapper Class
*
* @category Divante
* @package VueStorefrontBridge
* @author Mathias Arlaud <[email protected]>
* @copyright Copyright (C) 2019
* @license MIT License
*/
class Divante_VueStorefrontBridge_Helper_Mapper_Address extends Divante_VueStorefrontBridge_Helper_Mapper_Abstract
{
/**
* Name to address custom mappers via config.xml
* @var string $_mapperIdentifier
*/
protected $_mapperIdentifier = 'address';

/**
* @inheritdoc
*/
protected function customDtoFiltering($dto)
{
if (!is_array($dto['street'])) {
$dto['street'] = explode("\n", $dto['street']);
}

return $dto;
}

/**
* @inheritdoc
*/
protected function getAttributesToCastStr()
{
return [
'country_id'
];
}

/**
* @inheritdoc
*/
protected function getAttributesToCastBool()
{
return [
'default_billing',
'default_shipping'
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

/**
* Divante VueStorefrontBridge CategoryMapper Class
*
* @category Divante
* @package VueStorefrontBridge
* @author Mathias Arlaud <[email protected]>
* @copyright Copyright (C) 2019
* @license MIT License
*/
class Divante_VueStorefrontBridge_Helper_Mapper_Category extends Divante_VueStorefrontBridge_Helper_Mapper_Abstract
{
/**
* Name to address custom mappers via config.xml
* @var string $_mapperIdentifier
*/
protected $_mapperIdentifier = 'category';

/**
* @inheritdoc
*/
protected function getBlacklist()
{
return [
'entity_id',
'path'
];
}

/**
* @inheritdoc
*/
protected function getAttributesToCastInt()
{
return [
'position',
'level',
'children_count'
];
}

/**
* @inheritdoc
*/
protected function getAttributesToCastBool()
{
return [
'include_in_menu'
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

/**
* Divante VueStorefrontBridge CustomerMapper Class
*
* @category Divante
* @package VueStorefrontBridge
* @author Mathias Arlaud <[email protected]>
* @copyright Copyright (C) 2019
* @license MIT License
*/
class Divante_VueStorefrontBridge_Helper_Mapper_Customer extends Divante_VueStorefrontBridge_Helper_Mapper_Abstract
{
/**
* Name to address custom mappers via config.xml
* @var string $_mapperIdentifier
*/
protected $_mapperIdentifier = 'customer';

/**
* @inheritdoc
*/
protected function getBlacklist()
{
return [
'password',
'password_hash',
'password_confirmation',
'password_created_at',
'confirmation',
'entity_type_id'
];
}

/**
* @inheritdoc
*/
protected function getAttributesToCastInt()
{
return [
'default_billing',
'default_shipping'
];
}
}
Loading