rasclatt/smart-dto 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

rasclatt/smart-dto

Composer 安装命令:

composer require rasclatt/smart-dto

包简介

Fill Dtos with data using the mapping. Also allows rendering to different array key formats.

README 文档

README

Create and manipulate Data Transfer Objects

Basic Use

First, create your Dto:

<?php
namespace MyNamespace\Dto;

use \SmartDto\Dto as SDto;

class Get extends SDto
{
    # Type these attribures as required (depending on your PHP version)
    public $id = 0;
    public $description = '';
    public $title = '';
}

Reference your Dto in your model

<?php
namespace MyNamespace;

use Dto\Get;

class MyClass
{
    # You can type your return so your IDE can easily
    # interpret what should be returned for quick reference
    public function get(): Get
    {
        $dataArray = [
            'id' => 123,
            'description' => 'Some kind of example description',
            'tile' => 'Tile Here!'
        ];
        # Return back your Dto
        return new Get($dataArray);
    }
}

Basic Result:

<?php
$MyClass = new MyNamespace\MyClass();
# Any respectable IDE will be able to detect your params and make viewing available attributes easy
# If the data attribute "$dataArray" is missing the "id" key, for example, because of the Dto object
# mapping, this will not throw an undefined index error
# As is, this will write "123"
echo $MyClass->id;

Advanced Example

Revised Dto to include some model-esque features:

<?php
namespace MyNamespace\Dto;

use \SmartDto\Dto as SDto;

class Get extends SDto
{
    public $id = 0;
    public $description = '';
    public $title = '';
    
    # If you create a same-named method as the parameter,
    # the Dto will then call this method post-process
    # Because it's protected, your IDE should not show it as available
    protected function id()
    {
        $this->id += 1;
    }
}

Advanced Result

This will produce a different result than the first example because it will run the "id()" method as well:

<?php
$MyClass = new MyNamespace\MyClass();
# This will result in the value "124" instead of "123"
echo $MyClass->id;

Parameter Conversion

You are able to convert the input array before assigning the keys to your Dto:

<?php
namespace MyNamespace\Dto;

use \SmartDto\Dto as SDto;

class Get extends SDto
{
    # Notice the Camel Case keys
    public $id = 0;
    public $theDescription = '';
    public $myTitle = '';
}

Convert Using A Constant

<?php
namespace MyNamespace;

use Dto\Get;

class MyClass
{
    public function get(): Get
    {
        # Notice these are not formatted the same as the DTO
        $dataArray = [
            'id' => 123,
            'the_description' => 'Some kind of example description',
            'my title' => 'Tile Here!'
        ];
        # Return back your Dto
        return new Get($dataArray, \SmartDto\Dto::CAMEL_CASE);
    }
}

This will map to camel case.

This will then be mapped to your camel cased parameters:

echo $MyClass->theDescription;

If you have a string such as 'thedescription', it will not generate camel case because there is no way to know where to break. You would want to use afterConstruct() method to generate that manually to theDescription or use \SmartDto\Mapper object to map two different arrays.

SmartDto\Mapper

This object will generate a string for copy and pasting to your DTO class by examining your input data:

<?php
use \SmartDto\Mapper;

$dataArray = [
    'id' => 123,
    'description' => 'Some kind of example description',
    'price' => 1.23
];

$Mapper = new Mapper($dataArray);
echo $Mapper->getAttributes();

This will produce a string that you can copy and paste:

public $id = 0;
public $description = '';
public $price = 0.0;

Pass true to the public function getAttributes(true); to add types behind your parameters:

public int $id = 0;
public string $description = '';
public float $price = 0.0;

Map Two Arrays

You might find you need to take one return array and force it into a new array with different keys from the original (like an adapter). This allows you to take two arrays and map the keys and values from each array:

# Original array with undesirable keys
$array = [
    'FIRST' => 'John',
    'LAST' => 'Doe'
];

# This is the array you want to create
$target = [
    'fullName' => '~FIRST~ ~LAST~',
    'firstName' => '~FIRST~',
    'lastName' => '~LAST~'
];

# Map the arrays
print_r(\SmartDto\Mapper::mapster($array, $target));

This would produce:

Array (
    [ fullName ]  => John Doe,
    [ firstName ] => John,
    [ lastName ] => Doe
)

rasclatt/smart-dto 适用场景与选型建议

rasclatt/smart-dto 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 587 次下载、GitHub Stars 达 0, 最近一次更新时间为 2021 年 06 月 05 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「php」 「dto」 「Data Transfer Object」 「Nubersoft」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 rasclatt/smart-dto 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 rasclatt/smart-dto 我们能提供哪些服务?
定制开发 / 二次开发

基于 rasclatt/smart-dto 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 587
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 7
  • 依赖项目数: 6
  • 推荐数: 0

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-06-05