定制 fsubal/donphan 二次开发

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

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

fsubal/donphan

Composer 安装命令:

composer require fsubal/donphan

包简介

Utility traits for type-safe & immutable entity in PHP

README 文档

README

Utility traits for type-safe & immutable entity in PHP.

Install

https://packagist.org/packages/fsubal/donphan

composer require fsubal/donphan

Why Donphan ?

In PHP application without certain frameworks, arrays are often used as domain models.

<?php

$user = [
    'user_id' => validateInt($_POST['user_id']),
    'name' => validateString($_POST['name']),
];

...

function doSomethingForUser(array $user)
{
    $name = $user['name'];
    ...
}

When refactoring these mess (or migrating to completely new framework) seems too hard, Donphan may help you.

What Donphan does

It provides two utility traits.

  • \Donphan\Validatable: provides ::validate method, and lifecycle hooks.
  • \Donphan\Immutable: provides ::from factory method.
<?php

final class User
{
    use \Donphan\Immutable;

    const REQUIRED = [
        'user_id' => 'numeric',
        'name' => 'string'
    ];
}

// and then
$user = User::from([
    'user_id' => $_POST['user_id'],
    'name' => $_POST['name']
]);

function doSomethingForUser(User $user)
{
    $name = $user->name;
    ...
}

Not perfect, but now you have type safety with ease.

How to use

  • Define your own model class
  • use \Donphan\Immutable inside
  • Define const REQUIRED = [...], and const OPTIONAL = [...] if you want.
  • If you need, define static functions beforeTypeCheck or afterTypeCheck (See Lifecycle methods)

Then, you can use YourClass::from(array $params) or YourClass::validate(array $params).

Type checking

Donphan supports these types.

  • mixed
  • int
  • float
  • numeric
  • string
  • boolean
  • array
  • and any defined class name.

numeric looks like an original type ? Yes, but it is just validated by is_numeric ( https://secure.php.net/manual/en/function.is-numeric.php ).

It is almost like int|string, useful for some ids in $_GET or $_POST or something.

These types are not supported.

  • null
  • object
  • resource
  • callable
  • Closure
  • Exception
  • Error

Lifecycle methods

If you want to have default value ? Or if you allow additional validation to your Immutable object ?

Then, the lifecycle methods might be needed.

\Donphan\Validatable provides two lifecycle methods.

  • beforeTypeCheck: Executed just before type checking. It gets original params, and you must return an array.
  • afterTypeCheck: Executed just after type checking. It gets original params, and you must NOT return anything.

beforeTypeCheck is useful for mutating the original array, and afterTypeCheck is useful for performing additional validations.

Example below

final class User
{
    use \Donphan\Immutable;

    const REQUIRED = [
        'user_id' => 'numeric',
        'name' => 'string',
        'birthday' => '\DateTimeImmutable'
    ];

    const OPTIONAL = [
        'url' => 'string'
    ];

    public static function beforeTypeCheck(array $params)
    {
        if (!isset($params['url'])) {
            $params['url'] = 'https://example.com';
        }
        return $params;
    }

    public static function afterTypeCheck(array $params)
    {
        if (strlen($params['name']) == 0) {
            throw new \InvalidArgumentException('params[name] must not be empty!');
        }
    }
}

Note that the url added in beforeTypeCheck is also type checked (if it is written in REQUIRED or OPTIONAL).

Requirements

PHP 5.6+ ( Needs to be writable const with array in classes )

LICENSE

This is licensed under MIT License.

统计信息

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

GitHub 信息

  • Stars: 5
  • Watchers: 2
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2018-02-24

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固