digital-creative/fakeable-data-transfer-object 问题修复 & 功能扩展

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

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

digital-creative/fakeable-data-transfer-object

最新稳定版本:1.0.2

Composer 安装命令:

composer require digital-creative/fakeable-data-transfer-object

包简介

README 文档

README

Latest Version on Packagist Total Downloads License

This package extends the https://github.com/spatie/data-transfer-object by only including a new static method ::fake(), therefore expects no difference from the original package.

Installation

You can install the package via composer:

composer require digital-creative/fakeable-data-transfer-object

Dependencies:

Usage

use DigitalCreative\FakeableDataTransferObject\DataTransferObject;

class SomeObject extends DataTransferObject
{
    public string $name;
    public int $age;
}

then

$dto1 = SomeObject::fake();
$dto2 = SomeObject::fake(age: 18);

echo $dto1->name; // random word
echo $dto1->age; // random int

echo $dto2->name; // random word
echo $dto2->age; // 18

Register Custom Types

You can register any custom type by adding the following on the boot method of your AppServiceProvider:

<?php

namespace App\Providers;

use BenSampo\Enum\Enum;
use DigitalCreative\FakeableDataTransferObject\FakerRegistrar;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
    public function boot(): void
    {
        FakerRegistrar::register(Enum::class, function (string $class, mixed $value = null) {
            return blank($value) ? $class::getRandomInstance() : $class::fromValue($value);
        });
        
        FakerRegistrar::register(UploadedFile::class, function (string $class, mixed $value = null) {
            /** @var UploadedFile $class */
            return blank($value) ? $class::fake()->create('file.png') : $value;
        });

    }
}

and use like:

use DigitalCreative\FakeableDataTransferObject\DataTransferObject;

class SomeObject extends DataTransferObject
{
    public GenderEnum $gender;
    public UploadedFile $attachment;
}

License

The MIT License (MIT). Please see License File for more information.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-07-09