mirror of
https://github.com/SuiteCRM/SuiteCRM-Core.git
synced 2025-08-29 21:00:39 +08:00
Add support for default mappers in RecordMapperRunner
This commit is contained in:
parent
ca7479fff1
commit
cc3bbd8e08
9 changed files with 180 additions and 48 deletions
|
@ -39,6 +39,12 @@ interface BaseFieldMapperInterface extends BaseModuleMapperInterface
|
|||
*/
|
||||
public function getField(): string;
|
||||
|
||||
/**
|
||||
* Set if mapper overrides type default
|
||||
* @return bool
|
||||
*/
|
||||
public function replaceDefaultTypeMapper(): bool;
|
||||
|
||||
/**
|
||||
* Map value to internal format
|
||||
* @param Record $record
|
||||
|
|
|
@ -28,8 +28,6 @@
|
|||
|
||||
namespace App\Data\Service\Record\Mappers;
|
||||
|
||||
use Traversable;
|
||||
|
||||
class BaseFieldMapperRegistry
|
||||
{
|
||||
use RecordFieldMapperTrait;
|
||||
|
@ -40,19 +38,26 @@ class BaseFieldMapperRegistry
|
|||
protected array $registry = [];
|
||||
|
||||
/**
|
||||
* RecordLevelMapperInterface constructor.
|
||||
* @param Traversable $mappers
|
||||
* @var BaseFieldMapperInterface[]
|
||||
*/
|
||||
public function __construct(Traversable $mappers)
|
||||
protected array $defaultMappers = [];
|
||||
|
||||
/**
|
||||
* @var BaseFieldMapperInterface[]
|
||||
*/
|
||||
protected array $existingTypeDefaultOverrides = [];
|
||||
|
||||
/**
|
||||
* RecordLevelMapperInterface constructor.
|
||||
* @param BaseFieldMapperInterface[] $mappers
|
||||
*/
|
||||
public function __construct(array $mappers)
|
||||
{
|
||||
/**
|
||||
* @var $mappers BaseFieldMapperInterface[]
|
||||
*/
|
||||
$this->addMappers($mappers);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the field type mappers for the module and type
|
||||
* Get the field mappers for the module, field and mode
|
||||
* @param string $module
|
||||
* @param string $field
|
||||
* @param string|null $mode
|
||||
|
@ -65,6 +70,42 @@ class BaseFieldMapperRegistry
|
|||
return $this->filterMappersByModes($mappers, $mode);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get default mapper for module, field and mode
|
||||
* @param string $module
|
||||
* @param string $field
|
||||
* @param string $mode
|
||||
* @return BaseFieldMapperInterface|null
|
||||
*/
|
||||
public function getDefaultMapper(string $module, string $field, string $mode): ?BaseFieldMapperInterface
|
||||
{
|
||||
$moduleDefault = $this->defaultMappers[$module . '-' . $field . '-' . $mode] ?? null;
|
||||
|
||||
if ($moduleDefault !== null) {
|
||||
return $moduleDefault;
|
||||
}
|
||||
|
||||
return $this->defaultMappers['default' . '-' . $field . '-' . $mode] ?? null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get default mapper for module, field and mode
|
||||
* @param string $module
|
||||
* @param string $field
|
||||
* @param string $mode
|
||||
* @return BaseFieldMapperInterface|null
|
||||
*/
|
||||
public function getTypeDefaultOverride(string $module, string $field, string $mode): ?BaseFieldMapperInterface
|
||||
{
|
||||
$moduleDefault = $this->existingTypeDefaultOverrides[$module . '-' . $field . '-' . $mode] ?? null;
|
||||
|
||||
if ($moduleDefault !== null) {
|
||||
return $moduleDefault;
|
||||
}
|
||||
|
||||
return $this->existingTypeDefaultOverrides['default' . '-' . $field . '-' . $mode] ?? null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param BaseFieldMapperInterface[] $mappers
|
||||
* @return void
|
||||
|
@ -76,6 +117,21 @@ class BaseFieldMapperRegistry
|
|||
$module = $handler->getModule() ?? '';
|
||||
$order = $handler->getOrder() ?? 0;
|
||||
$moduleMappers = $this->registry[$module] ?? [];
|
||||
$key = $handler->getKey();
|
||||
|
||||
if ($handler->replaceDefaultTypeMapper()) {
|
||||
$key = 'default';
|
||||
foreach ($handler->getModes() as $mode) {
|
||||
$this->existingTypeDefaultOverrides[$module . '-' . $field . '-' . $mode] = $handler;
|
||||
}
|
||||
}
|
||||
|
||||
if ($key === 'default') {
|
||||
foreach ($handler->getModes() as $mode) {
|
||||
$this->defaultMappers[$module . '-' . $field . '-' . $mode] = $handler;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
$this->addMapper($moduleMappers, $field, $order, $handler);
|
||||
|
||||
|
|
|
@ -34,6 +34,13 @@ class BaseFieldMapperRegistryFactory
|
|||
{
|
||||
public static function create(Traversable $mappers): BaseFieldMapperRegistry
|
||||
{
|
||||
return new BaseFieldMapperRegistry($mappers);
|
||||
/**
|
||||
* @var $mappers BaseFieldMapperInterface[]
|
||||
*/
|
||||
$parsed = [];
|
||||
foreach ($mappers as $mapper) {
|
||||
$parsed[] = $mapper;
|
||||
}
|
||||
return new BaseFieldMapperRegistry($parsed);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -28,8 +28,6 @@
|
|||
|
||||
namespace App\Data\Service\Record\Mappers;
|
||||
|
||||
use Traversable;
|
||||
|
||||
class BaseFieldTypeMapperRegistry
|
||||
{
|
||||
use RecordFieldMapperTrait;
|
||||
|
@ -40,14 +38,16 @@ class BaseFieldTypeMapperRegistry
|
|||
protected array $registry = [];
|
||||
|
||||
/**
|
||||
* BaseFieldTypeMapperRegistry constructor.
|
||||
* @param Traversable $mappers
|
||||
* @var BaseFieldTypeMapperInterface[]
|
||||
*/
|
||||
public function __construct(Traversable $mappers)
|
||||
protected array $defaultMappers = [];
|
||||
|
||||
/**
|
||||
* BaseFieldTypeMapperRegistry constructor.
|
||||
* @param BaseFieldTypeMapperInterface[] $mappers
|
||||
*/
|
||||
public function __construct(array $mappers)
|
||||
{
|
||||
/**
|
||||
* @var $mappers BaseFieldTypeMapperInterface[]
|
||||
*/
|
||||
$this->addMappers($mappers);
|
||||
}
|
||||
|
||||
|
@ -65,6 +65,24 @@ class BaseFieldTypeMapperRegistry
|
|||
return $this->filterMappersByModes($mappers, $mode);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get default mapper for module, field and mode
|
||||
* @param string $module
|
||||
* @param string $fieldType
|
||||
* @param string $mode
|
||||
* @return BaseFieldTypeMapperInterface|null
|
||||
*/
|
||||
public function getDefaultMapper(string $module, string $fieldType, string $mode): ?BaseFieldTypeMapperInterface
|
||||
{
|
||||
$moduleDefault = $this->defaultMappers[$module . '-' . $fieldType . '-' . $mode] ?? null;
|
||||
|
||||
if ($moduleDefault !== null) {
|
||||
return $moduleDefault;
|
||||
}
|
||||
|
||||
return $this->defaultMappers['default' . '-' . $fieldType . '-' . $mode] ?? null;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param BaseFieldTypeMapperInterface[] $mappers
|
||||
|
@ -73,12 +91,20 @@ class BaseFieldTypeMapperRegistry
|
|||
protected function addMappers(iterable $mappers): void
|
||||
{
|
||||
foreach ($mappers as $handler) {
|
||||
$field = $handler->getFieldType();
|
||||
$fieldType = $handler->getFieldType();
|
||||
$module = $handler->getModule();
|
||||
$order = $handler->getOrder() ?? 0;
|
||||
$moduleMappers = $this->registry[$module] ?? [];
|
||||
$key = $handler->getKey();
|
||||
|
||||
$this->addMapper($moduleMappers, $field, $order, $handler);
|
||||
if ($key === 'default') {
|
||||
foreach ($handler->getModes() as $mode) {
|
||||
$this->defaultMappers[$module . '-' . $fieldType . '-' . $mode] = $handler;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
$this->addMapper($moduleMappers, $fieldType, $order, $handler);
|
||||
|
||||
$this->registry[$module] = $moduleMappers;
|
||||
}
|
||||
|
|
|
@ -34,6 +34,13 @@ class BaseFieldTypeMapperRegistryFactory
|
|||
{
|
||||
public static function create(Traversable $mappers): BaseFieldTypeMapperRegistry
|
||||
{
|
||||
return new BaseFieldTypeMapperRegistry($mappers);
|
||||
/**
|
||||
* @var $mappers BaseFieldTypeMapperInterface[]
|
||||
*/
|
||||
$parsed = [];
|
||||
foreach ($mappers as $mapper) {
|
||||
$parsed[] = $mapper;
|
||||
}
|
||||
return new BaseFieldTypeMapperRegistry($parsed);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -28,7 +28,6 @@
|
|||
|
||||
namespace App\Data\Service\Record\Mappers;
|
||||
|
||||
use Traversable;
|
||||
|
||||
class BaseRecordMapperRegistry
|
||||
{
|
||||
|
@ -41,21 +40,15 @@ class BaseRecordMapperRegistry
|
|||
|
||||
/**
|
||||
* BaseRecordMapperRegistry constructor.
|
||||
* @param Traversable $mappers
|
||||
* @param BaseRecordMapperInterface[] $mappers
|
||||
*/
|
||||
public function __construct(Traversable $mappers)
|
||||
public function __construct(array $mappers)
|
||||
{
|
||||
|
||||
/**
|
||||
* @var $mappers BaseRecordMapperInterface[]
|
||||
*/
|
||||
$this->addMappers($mappers);
|
||||
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the field type mappers for the module and type
|
||||
* Get the mappers for the module and mode
|
||||
* @param string $module
|
||||
* @param string|null $mode
|
||||
* @return BaseRecordMapperInterface[]
|
||||
|
|
|
@ -34,6 +34,14 @@ class BaseRecordMapperRegistryFactory
|
|||
{
|
||||
public static function create(Traversable $mappers): BaseRecordMapperRegistry
|
||||
{
|
||||
return new BaseRecordMapperRegistry($mappers);
|
||||
/**
|
||||
* @var $mappers BaseRecordMapperInterface[]
|
||||
*/
|
||||
$parsed = [];
|
||||
foreach ($mappers as $mapper) {
|
||||
$parsed[] = $mapper;
|
||||
}
|
||||
|
||||
return new BaseRecordMapperRegistry($parsed);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -44,6 +44,20 @@ trait ModuleMapperTrait
|
|||
$parentMap[$order] = $mappersByOrder;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $mapperModes
|
||||
* @param string $mode
|
||||
* @return bool
|
||||
*/
|
||||
protected function isModeEnabled(array $mapperModes, string $mode): bool
|
||||
{
|
||||
$modeEnabled = false;
|
||||
if (in_array($mode, $mapperModes, true)) {
|
||||
$modeEnabled = true;
|
||||
}
|
||||
return $modeEnabled;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $mappers
|
||||
* @param string $mode
|
||||
|
@ -68,19 +82,5 @@ trait ModuleMapperTrait
|
|||
return $modeMappers;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $mapperModes
|
||||
* @param string $mode
|
||||
* @return bool
|
||||
*/
|
||||
protected function isModeEnabled(array $mapperModes, string $mode): bool
|
||||
{
|
||||
$modeEnabled = false;
|
||||
if (in_array($mode, $mapperModes, true)) {
|
||||
$modeEnabled = true;
|
||||
}
|
||||
return $modeEnabled;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -31,7 +31,7 @@ use App\Data\Entity\Record;
|
|||
use App\FieldDefinitions\Entity\FieldDefinition;
|
||||
use App\FieldDefinitions\Service\FieldDefinitionsProviderInterface;
|
||||
|
||||
class RecordMapperRunner implements RecordMapperRunnerInterface
|
||||
abstract class RecordMapperRunner implements RecordMapperRunnerInterface
|
||||
{
|
||||
protected BaseFieldMapperRegistry $fieldMapperRegistry;
|
||||
protected BaseFieldTypeMapperRegistry $fieldTypeMapperRegistry;
|
||||
|
@ -120,17 +120,46 @@ class RecordMapperRunner implements RecordMapperRunnerInterface
|
|||
): void {
|
||||
$fieldVardefs = $vardefs ?? [];
|
||||
$type = $fieldVardefs['type'] ?? '';
|
||||
$fieldMappers = $this->fieldMapperRegistry->getMappers($record->getModule(), $field, $mode);
|
||||
|
||||
if ($type !== '') {
|
||||
$this->runDefaultMapper($record, $type, $mode, $field, $direction, $fieldDefinitions);
|
||||
|
||||
$fieldTypeMappers = $this->fieldTypeMapperRegistry->getMappers($record->getModule(), $type, $mode);
|
||||
foreach ($fieldTypeMappers as $fieldTypeMapper) {
|
||||
$fieldTypeMapper->$direction($record, $fieldDefinitions, $field);
|
||||
}
|
||||
}
|
||||
|
||||
$fieldMappers = $this->fieldMapperRegistry->getMappers($record->getModule(), $field, $mode);
|
||||
foreach ($fieldMappers as $fieldMapper) {
|
||||
$fieldMapper->$direction($record, $fieldDefinitions);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Record $record
|
||||
* @param string $type
|
||||
* @param string|null $mode
|
||||
* @param string $field
|
||||
* @param string $direction
|
||||
* @param FieldDefinition $fieldDefinitions
|
||||
* @return void
|
||||
*/
|
||||
protected function runDefaultMapper(
|
||||
Record $record,
|
||||
string $type,
|
||||
?string $mode,
|
||||
string $field,
|
||||
string $direction,
|
||||
FieldDefinition $fieldDefinitions
|
||||
): void {
|
||||
$default = $this->fieldTypeMapperRegistry->getDefaultMapper($record->getModule(), $type, $mode);
|
||||
$defaultOverride = $this->fieldMapperRegistry->getTypeDefaultOverride($record->getModule(), $field, $mode);
|
||||
|
||||
if ($defaultOverride !== null) {
|
||||
$defaultOverride->$direction($record, $fieldDefinitions);
|
||||
} elseif ($default !== null) {
|
||||
$default->$direction($record, $fieldDefinitions, $field);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue