承接 joshbrw/type-enforcement 相关项目开发

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

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

joshbrw/type-enforcement

Composer 安装命令:

composer require joshbrw/type-enforcement

包简介

Enforce types and throw InvalidArgumentExceptions upon invalid types.

README 文档

README

If you've ever come across a time where you're writing loosely-coupled code but want to check a parameters type at the start of a method, then this library is for you.

How you're probably doing it

class UserRegistrar {
    public function register(array $usersDetails, $author)
    {
        if (!$author instanceof SomeAuthor) {
            throw new \InvalidArgumentException('The author must be an instance of SomeAuthor');
        }
    }
}

Let's refactor!

use Joshbrw\TypeEnforcement\Type;

class UserRegistrar {
    public function register(array $usersDetails, $author)
    {
        /* Throws \InvalidArgumentException on invalid input */
        Type::enforce($author, SomeAuthor::class);
    }
}

Custom Exception Messages

By default the package will provide a useful exception message, stating which type is expected and which type has been provided, i.e:

Expected [Tests\NonExistentClass], [array '["array"]'] provided.

This exception message can be switched out by providing a custom message as the third parameter, i.e.:

Type::enforce($variable, Type::class, 'The variable must be a Type!');

Not a fan of static method calls?

This package also includes a helper method that accepts the same parameters:

enforce_type($variable, Type::class, 'The variable must be a Type!');

统计信息

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

GitHub 信息

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

其他信息

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

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固