定制 shureban/laravel-object-mapper 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

shureban/laravel-object-mapper

最新稳定版本:1.2.0

Composer 安装命令:

composer require shureban/laravel-object-mapper

包简介

Laravel SDK mapping data in object

README 文档

README

Installation

Require this package with composer using the following command:

composer require shureban/laravel-object-mapper

Add the following class to the providers array in config/app.php:

Shureban\LaravelObjectMapper\ObjectMapperServiceProvider::class,

You can also publish the config file to change implementations (ie. interface to specific class).

php artisan vendor:publish --provider="Shureban\LaravelObjectMapper\ObjectMapperServiceProvider"

How to use

You have 3 options to use ObjectMapper

Inheritance

Your mapped object (dto) must inheritance from \Shureban\LaravelObjectMapper\MappableObject

class User extends MappableObject
{
    public int $id; 
}

$user1 = (new User())->mapFromJson('{"id": 10}');
$user2 = (new User())->mapFromArray(['id' => 10]);
$user3 = (new User())->mapFromRequest($formRequest);

Using trait

Your mapped object (dto) must use \Shureban\LaravelObjectMapper\MappableTrait

class User
{
    use MappableTrait;

    public int $id; 
}

$user1 = (new User())->mapFromJson('{"id": 10}');
$user2 = (new User())->mapFromArray(['id' => 10]);
$user3 = (new User())->mapFromRequest($formRequest);

Delegate mapping to ObjectMapper

class User {
    public int $id; 
}

$user1 = (new ObjectMapper(new User()))->mapFromJson('{"id": 10}');
$user2 = (new ObjectMapper(new User()))->mapFromArray(['id' => 10]);
$user3 = (new ObjectMapper(new User()))->mapFromRequest($formRequest);

Mappable cases

Below you will see cases which you can use for mapping data into your object

Simple types

  • mixed
  • string
  • bool, boolean
  • int, integer
  • double, float
  • array
  • object

Box types

  • Carbon
  • DateTime
  • Collection

Custom types

  • CustomClass
  • Enum
  • Eloquent

Array of types

That typo of mapping may be realized only via phpDoc notation

  • int[]
  • int[][]
  • DateTime[]
  • CustomClass[]

Special cases

Constructor

If your type object has 1 required parameter and value is NOT an array, ObjectMapper will build instance of this type via constructor call If your type object has 0 or more than 1 required parameters, it will throw WrongConstructorParametersNumberException exception

Correct case:

class User
{
    public int $id;
    
    public function __construct(int $id) {
        $this->id = $id;
    } 
}

Wrong case:

class User
{
    public int    $id;
    public string $name;
    
    public function __construct(int $id, string $name) {
        $this->id   = $id;
        $this->name = $name;
    } 
}

PhpDoc

PhpDoc type hinting has much more priority than main type.

class User
{
    /**
    * @var int 
    */
    public int $id; 
    /**
    * @var DateTime 
    */
    public $dateOfBirthday; 
    /**
    * @var Address[]
    */
    public array $addresses; 
}

Setters

If you want to realize your own logic for setting value, you may place setter method in your mapped object This setter should start from set word and been in camelCase notation.

class User
{
    public string   $id; 
    public DateTime $dateOfBirthday;
    
    public function setId(int $id, array $rawData = []): void 
    {
        $this->id = Hash::make($id);
    }
    
    public function setDateOfBirthday(string $dateOfBirthday, array $rawData = []): void 
    {
        $this->dateOfBirthday = new DateTime($dateOfBirthday);
    }
}

$user = (new ObjectMapper(new User()))->mapFromArray(['id' => 10, 'dateOfBirthday' => '1991-01-01']);

echo $user->id; // $2y$10$XqHrk0oXa7.9AihthdVxW.dd637zj9EhlTJX0eUEKiV61dbs7a7ZO
echo $user->dateOfBirthday->format('Y'); // 1991

Some words about second parameter $rawData. Value of this parameter depends on method selected for mapping

  • mapFromJson - $rawData will be JSON
  • mapFromArray - $rawData will be Array
  • mapFromRequest - $rawData will be FormRequest object

Readonly parameters

Readonly parameters will always be skipped

class User
{
    public readonly int $id; 
}

$user = (new ObjectMapper(new User()))->mapFromArray(['id' => 10]);

echo $user->id; // 0

Config rewriting

In object_mapper.php config file have been presented all mappable types classes. You have opportunity to rewrite mapping flow or realize you own one.

If you need to create your own type mapping, follow this way:

  • create class inherited from \Shureban\LaravelObjectMapper\Types\Type
  • place you type into config file in type -> box array

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-11-25