perf/type-validation 问题修复 & 功能扩展

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

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

perf/type-validation

最新稳定版本:4.0.0

Composer 安装命令:

composer require perf/type-validation

包简介

Allows to check if a variable is of the expected type, using a type specification string like the ones in PHPDoc blocks (int, \Foo\Bar[], null|string, etc).

README 文档

README

This package allows to check if a variable is of the expected type, using a type specification string like the ones in PHPDoc blocks.

Type specifications strings can be like the following:

- integer
- scalar
- mixed
- string
- float[]
- int[][]
- {string:float}
- {int:\My\Stuff}
- {int:string}[]
- resource
- int|string
- {string:{int:\My\Stuff|int[]}}[]

etc.

Usage

Static validation with exceptions

<?php

use Jmf\TypeValidation\Type;

// Valid, will not throw an exception.
Type::mustBe('string', 'foo');

// Invalid, will throw an exception.
Type::mustBe('string', 123);

Static validation with booleans

<?php

use Jmf\TypeValidation\Type;

$variable = 'foo';

if (Type::is('string', $variable)) {
	// Valid
} else {
	// Invalid
}

Concrete validation

You can also use a concrete instance of a type validator.

<?php

use Jmf\TypeValidation\TypeValidator;

$validator = new TypeValidator();

$variable = 'foo';

if ($validator->isValid('string', $variable)) {
	// Valid
} else {
	// Invalid
}

Typical use

<?php

namespace App;

use Jmf\TypeValidation\Type;

class PotatoPeeler
{
    /**
     * @param Potato[] $potatoes
     */
    public function peel(array $potatoes): void
    {
        Type::mustBe('\App\Potato[]', $potatoes);

        // ...
    }
}

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-05-12