承接 luimedi/remap 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

luimedi/remap

最新稳定版本:v0.2.1

Composer 安装命令:

composer require luimedi/remap

包简介

A PHP object mapping library using attributes.

README 文档

README

Remap is a lightweight PHP library for mapping arrays and objects into target objects using PHP attributes.

The mapping rules live in the target class, so the output model decides how data should be read and transformed.

Installation

composer require luimedi/remap

Quick Start

<?php

use Luimedi\Remap\Mapper;
use Luimedi\Remap\Attribute\ConstructorMapper;
use Luimedi\Remap\Attribute\MapProperty;
use Luimedi\Remap\Attribute\Cast\CastDateTime;

class BookEntity
{
	public function __construct(
		public string $title,
		public string $author,
		public DateTimeInterface $publishedAt,
	) {}
}

#[ConstructorMapper]
class BookResource
{
	public function __construct(
		#[MapProperty(source: 'title')]
		public string $title,

		#[MapProperty(source: 'author')]
		public string $author,

		#[MapProperty(source: 'publishedAt')]
		#[CastDateTime]
		public string $publishedAt,
	) {}
}

$mapper = new Mapper();
$mapper->bind(BookEntity::class, BookResource::class);

$resource = $mapper->map(new BookEntity(
	title: 'El Hobbit',
	author: 'J.R.R. Tolkien',
	publishedAt: new DateTimeImmutable('1937-09-21'),
));

How It Works

  1. Define a target class.
  2. Add mapping attributes to its constructor parameters or public properties.
  3. Bind the source type to the target type.
  4. Call map().

Mapping Styles

Constructor Mapping

Use #[ConstructorMapper] when your target is created through its constructor.

#[ConstructorMapper]
class UserResource
{
	public function __construct(
		#[MapProperty(source: 'username')]
		public string $username,

		#[MapProperty(source: 'registeredAt')]
		#[CastDateTime]
		public string $registeredAt,
	) {}
}

Property Mapping

Use #[PropertyMapper] when you prefer assigning public properties.

use Luimedi\Remap\Attribute\PropertyMapper;

#[PropertyMapper]
class ArticleResource
{
	#[MapProperty(source: 'title')]
	public string $title;

	#[MapProperty(source: 'body')]
	public string $body;
}

You can also combine both on the same class.

Mapping Attributes

MapProperty

Reads a value from an array or object property. It supports dot notation.

#[MapProperty(source: 'author.name')]
public string $authorName;

MapGetter

Calls a method on the source object.

use Luimedi\Remap\Attribute\MapGetter;

#[MapGetter(source: 'getType')]
public string $type;

Built-in Casts

CastDateTime

Converts a DateTimeInterface or date string into an ISO-8601 string.

CastDefault

Provides a fallback value when the mapped value is missing or empty.

use Luimedi\Remap\Attribute\Cast\CastDefault;

#[MapProperty(source: 'nickname')]
#[CastDefault(default: 'anonymous')]
public string $nickname;

CastTransformer

Maps nested arrays or objects into another target object.

use Luimedi\Remap\Attribute\Cast\CastTransformer;

#[MapProperty(source: 'profile')]
#[CastTransformer]
public ?ProfileResource $profile = null;

CastIterable

Applies another caster to each item in an iterable.

use Luimedi\Remap\Attribute\Cast\CastIterable;
use Luimedi\Remap\Attribute\Cast\CastTransformer;

#[MapProperty(source: 'items')]
#[CastIterable(class: CastTransformer::class)]
public array $items = [];

Dynamic Binding

Instead of binding a source type to a fixed class, you can bind it to a resolver.

<?php

use Luimedi\Remap\Mapper;

$mapper = new Mapper();

$mapper->bind(UserInput::class, function ($from, $context) {
	return $from->isAdmin() ? AdminResource::class : UserResource::class;
});

You can also pass extra data to a mapping call:

$result = $mapper->map($source, ['force_admin' => true]);

Error Handling

All library-specific exceptions extend Luimedi\Remap\Exception\RemapException.

use Luimedi\Remap\Exception\RemapException;

try {
	$result = $mapper->map($source);
} catch (RemapException $exception) {
	// Handle mapping errors here.
}

Common exceptions:

  • BindingNotFoundException
  • BindingResolutionException
  • InvalidTargetTypeException
  • MapGetterResolutionException
  • MissingMappedValueException

Custom Casts

Create custom casts by implementing CastInterface.

<?php

namespace App\Cast;

use Attribute;
use Luimedi\Remap\Contracts\CastInterface;
use Luimedi\Remap\Contracts\ContextInterface;

#[Attribute(Attribute::TARGET_PARAMETER | Attribute::TARGET_PROPERTY)]
class AppendSuffix implements CastInterface
{
	public function __construct(
		private string $suffix,
	) {}

	public function cast(mixed $value, ContextInterface $context): mixed
	{
		if ($value === null) {
			return null;
		}

		return (string) $value . $this->suffix;
	}
}

License

MIT. See LICENSE.

统计信息

  • 总下载量: 272
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-11-10

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固