flashios09/php-union-types
Composer 安装命令:
composer require flashios09/php-union-types
包简介
A php class for union types
README 文档
README
Requirements
- PHP 7.1+
- Composer
Installation
composer require flashios09/php-union-types
Usage
UnionTypes::assert
UnionTypes::assert(mixed $value, string[] $types, array $options = []): void
Throw a TypeError if the given value isn't in the passed union type.
See the full list of the valid types $types string array, e.g. ['int', 'string', Posts::class, ...].
Examples:
-
UnionTypes::assert(1.2, ['int', 'float']);
✓ should pass
-
UnionTypes::assert('1.2', ['int', 'float']);
✖ will throw a TypeError "'1.2' must be of the union type
int|float,stringgiven" -
UnionTypes::assert('1.2', ['int', 'float', 'string']);
✓ should pass
-
Check for
instance of, added on1.0.2use \DateTime; class Time extends DateTime { public static function today() { return new Time(); } } $today = Time::today();
// `instanceOf` check is enabled by default UnionTypes::assert($today, [\DateTime::class, 'string']);
✓ should pass
// disabled using the `instanceOf` option UnionTypes::assert($today, [\DateTime::class, 'string', ['instanceOf' => false]]);
✖ will throw a TypeError "object(Time)" must be of the union type
\DateTime|string,Timegiven"
UnionTypes::is
UnionTypes::is(mixed $value, string[] $types, array $options = []): bool
Check if the value type is one of the passed types.
Works just like UnionTypes::assert($value, $types) but it will return a bool(true/false) instead of throwing a TypeError.
See the full list of the valid types $types string array, e.g. ['int', 'string', Posts::class, ...].
Examples:
-
UnionTypes::is(1.2, ['int', 'float']);
equivalent to
is_int(1.2) || is_float(1.2)✓ return
true -
UnionTypes::is('1.2', ['int', 'float']);
equivalent to
is_int('1.2') || is_float('1.2')✖ return
false -
UnionTypes::is('1.2', ['int', 'float', 'string']);
equivalent to
is_int('1.2') || is_float('1.2') || is_string('1.2')✓ return
true -
Check for
instance of, added on1.0.2use \DateTime; class Time extends DateTime { public static function today() { return new Time(); } } $today = Time::today();
// `instanceOf` check is enabled by default UnionTypes::assert($today, [\DateTime::class, 'string']);
✓ return
true// disabled using the `instanceOf` option UnionTypes::assert($today, [\DateTime::class, 'string', ['instanceOf' => false]]);
✖ return
false
UnionTypes::assertFuncArg
UnionTypes::assertFuncArg(string $argName, string[] $types, array $options = []): void
Throw a TypesError if the value of the argument isn't in the union type.
See the full list of the valid types $types string array, e.g. int, string, Posts::class ...
Examples:
-
function add($a, $b) { UnionTypes::assertFuncArg('a', ['int', 'float']); return $a + $b; } // invocation add(1.2, 1);
✓ should pass
-
class Math { public static function add($a, $b) { UnionTypes::assertFuncArg('a', ['int', 'float']); return $a + $b; } } // invocation Math::add('1.2', 1);
✖ will throw TypeError "Argument
apassed toMath::add(int|float $a, ...)must be of the union typeint|float,stringgiven" -
$closure = function ($a, $b) { UnionTypes::assertFuncArg('a', ['int', 'float', 'string']); return $a + $b; }; // invocation $closure('1.2', 1);
✓ should pass
-
Check for
instance of, added on1.0.2use \DateTime; class Time extends DateTime { public static function today() { return new Time(); } } $today = Time::today();
$closure = function ($today) { // `instanceOf` check is enabled by default UnionTypes::assertFuncArg($today, [\DateTime::class, 'string']); // some logic here return $today; }; // invocation $closure($today);
✓ should
pass$closure = function ($today) { // disabled using the `instanceOf` option UnionTypes::assertFuncArg($today, [\DateTime::class, 'string', ['instanceOf' => false]]); // some logic here return $today; }; // invocation $closure($today);
✖ will throw a TypeError "object(Time)" must be of the union type
\DateTime|string,Timegiven"
Valid types:
'string''int'(not'integer'or'double')'float'(not'double'or'decimal')'bool'(not'boolean')'null'(not'NULL')'array'- Any valid classname string, e.g
Table::classor'Cake\ORM\Table' resource
Configuration
-
By default, every thrown Error or Exception will have a called in at the end of the message
called in #{stackTraceIndex} {file}:{line}, e.g.called in #0 /path/to/app/src/Controller/PostsController.php:126.To have a friendly editor(VSCode, Atom, SublimeText, ...) file path relative to the workspace, you need to define a constant
UnionTypes.PATH_TO_APPsomewhere, e.g:// config/bootstrap.php define('UnionTypes.PATH_TO_APP', '/path/to/app/'); // using `$_SERVER`(isn't available in **php cli**) $PATH_TO_APP = isset($_SERVER['DOCUMENT_ROOT']) ? $_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR : ''; define('UnionTypes.PATH_TO_APP', $PATH_TO_APP); // using `dirname(__FILE__)`, maybe you need to remove/add some parts, depending on the `__FILE__` location. define('UnionTypes.PATH_TO_APP', dirname(__FILE__) . DIRECTORY_SEPARATOR; // using the `PWD` key of the `getEnv()` array define('UnionTypes.PATH_TO_APP', getEnv()['PWD'] . DIRECTORY_SEPARATOR;
Now the
/path/to/app/prefix will be removed from the called in, e.g.called in #0 src/Controller/PostsController.php:126.
Contributing
Installation
git clone git@github.com:flashios09/php-union-types.gitcd php-union-typescomposer install(will install dev dependencies likewhoops,kahlan,var-dumper,cakephp-codesniffer)
Start a local php server with livereload
yarn install(node dependencies)yarn serve(then add your code tophp-union-types/index.phpand open a browser window athttp://localhost:3080)
Start a local kahlan test with livereload
yarn install(node dependencies)yarn test(it will watch any change in thespecfolder and re-launch the test)
Linting
composer cs-checkcomposer cs-fix
Running test
composer test
License
This project is licensed under the MIT License.
flashios09/php-union-types 适用场景与选型建议
flashios09/php-union-types 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 16 次下载、GitHub Stars 达 3, 最近一次更新时间为 2019 年 12 月 12 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「type hinting」 「union types」 「mixed types」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 flashios09/php-union-types 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 flashios09/php-union-types 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 flashios09/php-union-types 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A bundle providing fields for image upload with jquery upload and image cropping with jcrop for symfony2
A simple Symfony bundle that adds boolean form field type.
A PHP Abstract Enum Class
This package provides type-safe extension of the laravel collection, forcing a single type of object.
Compatibility layer for emulating enumerations in PHP < 8.1 and native enumerations in PHP >= 8.1
Tongue is CakePHP controller action **dirty** TypeHinting plugin.
统计信息
- 总下载量: 16
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 3
- 点击次数: 2
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2019-12-12
