inlm/mappers

Lean Mapper 的映射器

维护者

详细信息

github.com/inlm/mappers

源代码

问题

资助包维护!
其他

v3.0.0 2021-02-21 20:03 UTC

This package is auto-updated.

Last update: 2024-08-27 10:08:07 UTC


README

Build Status Downloads this Month Latest Stable Version License

Lean Mapper 的映射器

Donate

安装

下载最新包下载最新包或使用Composer

composer require inlm/mappers

Inlm\Mappers需要PHP 7.2.0或更高版本。

使用方法

DynamicMapper

Dynamic Mapper 使用显式的实体和表映射。

$mapper = new DynamicMapper;
$mapper->setMapping(
	'order_items', // table name - required
	'OrderItem', // entity class - optional
	'OrderItemRepository', // repository class - optional
	'item_id' // primary key - optional
);

如果没有为实体或表设置映射,则调用传递给回退映射器(默认为LeanMapper\DefaultMapper

$mapper = new DynamicMapper;
$mapper->getTable('OrderItem'); // returns 'orderitem'

$mapper = new DynamicMapper(new Inlm\Mappers\UnderScoreMapper);
$mapper->getTable('OrderItem'); // returns 'order_item'

PrefixMapper

PrefixMapper 为表名添加和删除前缀。

$mapper = new PrefixMapper('prefix_');
$mapper = new PrefixMapper('prefix_', $fallbackMapper);

PrefixMapper 只处理表名中的前缀,其余所有内容都交给回退映射器(默认为LeanMapper\DefaultMapper

$mapper = new PrefixMapper('prefix_');
echo $mapper->getTable('OrderItem'); // prints 'prefix_orderitem'

$mapper = new PrefixMapper('prefix_', new Inlm\Mappers\UnderScoreMapper);
echo $mapper->getTable('OrderItem'); // prints 'prefix_order_item'

RowMapper

RowMapper 将值映射到/从LeanMapper\Row(需要Lean Mapper 3.5+)。

$mapper = new RowMapper;
$mapper = new RowMapper($fallbackMapper);
$mapper->registerFieldMapping($entity, $field, $fromDbValue, $toDbValue);
$mapper->registerFieldMapping(
	Model\Entity\Client::class,
	'website',
	function ($dbValue) {
		return new Website($dbValue);
	},
	function (Website $rowValue) {
		return $rowValue->getUrl();
	}
);


// multi column mapping
$mapper->registerMultiValueMapping(
	Model\OrderItem::class,
	'price',
	function (array $values, $rowField) {
		return new Price($values[$rowField . '_total'], $values[$rowField . '_currency']);
	},
	function (Price $price, $rowField) {
		return [
			$rowField . '_total' => $price->getPrice(),
			$rowField . '_currency' => $price->getCurrency(),
		];
	}
);

StiMapper

StiMapper 简化了与单表继承(STI)的工作。

$mapper = new StiMapper;
$mapper = new StiMapper($fallbackMapper);

STI 类型的注册

$mapper->registerStiType($baseEntity, $typeValue, $entityClass);
$mapper->registerStiType(Entities\Client::class, 'company', Entities\ClientCompany::class);
$mapper->registerStiType(Entities\Client::class, 'individual', Entities\ClientIndividual::class);

默认 STI 类型列名为type,您可以通过以下方式更改它:

$mapper->registerTypeField(Entities\Client::class, 'clientType');

您可以为特定的 STI 类型限制LeanMapper\Fluent

$fluent = $connection->select('*')->from('client');
$mapper->applyStiMapping($fluent, Entities\ClientCompany::class);
echo $fluent; // SELECT * FROM `client` WHERE `client`.`clientType` = 'company'

如何更改默认实体命名空间

$mapper = new Inlm\Mappers\DefaultMapper('App\Entity');
$mapper = new Inlm\Mappers\CamelCaseMapper('App\Entity');
$mapper = new Inlm\Mappers\UnderScoreMapper('App\Entity');

推荐映射器顺序

  • RowMapper
  • StiMapper
  • PrefixMapper
  • DynamicMapper
  • DefaultMapper / CamelCaseMapper / UnderScoreMapper

许可证: 新BSD许可证
作者:Jan Pecha,https://www.janpecha.cz/