xp-lang/php-is-operator
Composer 安装命令:
composer require xp-lang/php-is-operator
包简介
Is operator for PHP
README 文档
README
Plugin for the XP Compiler which adds an is operator to the PHP language compatible with the PHP pattern matching RFC.
Before
A mix of operators, functions, syntax and XP core functionality is():
is_string($value) // for primitives, use is_[T]() is_callable($value) // for pseudo types callable, array, object is_array($value) || $value instanceof \Traversable // no is_iterable in PHP 5 and 7.0 $value instanceof Date // for value types null === $value || is_int($value) // nullable types cannot be tested directly is('[:string]', $value) // for types beyond PHP type system is('string|util.URI', $value) // for types beyond PHP type system
After
Anything that works as a parameter, property or return type can be used with the is operator.
$value is string $value is callable $value is iterable $value is Date $value is ?int $value is array<string, string> $value is string|URI
Literal patterns
Tests using the identity comparison:
$value is 'test'; $value is 5; $value is 3|5|null; $value is 'heart'|'spade'; $value is self::Wild; $value is 'heart'|'spade'|self::Wild;
Numeric comparison patterns
With greater than (or equal) as well as less than (or equal) operators.
$value is <10; $value is >=5; $value is >5 & <10;
Structural patterns
Objects:
$value is Point(x: 3); // Matches any Point whose $x property is 3 $value is Point(x: 4|5); // Matches any Point whose $x property is 4 or 5 $value is Point(y: 3)|null; // Matches any Point whose $y property is 3, allowing `null` $value is Point(y: >0); // Matches any Point whose $x property is greater than 0
Arrays:
$value is [1, 2, 3, 4]; // Exact match $value is [1, 2, 3, ...]; // Begins with 1, 2, 3, but may have other entries $value is [1, 2, mixed, 4]; // Allows any value in the 3rd position $value is [1, 2, 3|4, 5]; // 3rd value may be 3 or 4
Maps:
$value is ['a' => 'A', 'b' => 'B']; // Exact key/value match, but order doesn't matter $value is ['b' => 'B', ...]; // Must have a 'b' key with value 'B', and more $value is ['b' => mixed, ...]; // Must have a 'b' key with any value, and more
Capturing values
Binding variables to object properties as well as array and map values:
$value is Point(x: 3, y: $y); // If $p->x === 3, bind $p->y to $y $value is ['a' => 'A', 'b' => $b]; // Bind value of key 'b' to $b $value is ['op' => 'drop', ... $items]; // Bind rest of array to $items
Match statement
The operator can be used in the cases of a match statement:
$result= match ($value) { is int => $value, is string => '"'.strtr($value, ['"' => '\\"']).'"', null => 'null', };
Installation
After installing the XP Compiler into your project, also include this plugin.
$ composer require xp-framework/compiler # ... $ composer require xp-lang/php-is-operator # ...
No further action is required.
See also
- https://wiki.php.net/rfc/pattern-matching
- https://docs.hhvm.com/hack/expressions-and-operators/is
- https://kotlinlang.org/docs/reference/typecasts.html
- https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/is
- https://externals.io/message/120655 (Proposal to introduce is operator)
xp-lang/php-is-operator 适用场景与选型建议
xp-lang/php-is-operator 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 51 次下载、GitHub Stars 达 1, 最近一次更新时间为 2019 年 09 月 09 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「module」 「language」 「xp」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 xp-lang/php-is-operator 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 xp-lang/php-is-operator 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 xp-lang/php-is-operator 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Texy converts plain text in easy to read Texy syntax into structurally valid (X)HTML. It supports adding of images, links, nested lists, tables and has full support for CSS. Texy supports hyphenation of long words (which reflects language rules), clickable emails and URL (emails are obfuscated again
Easy to use i18n translation PHP class for multi-language websites
Package for convenient work with Laravel's localization features
Store your language lines in the database, yaml or other sources
Set Links with a specific language parameter
Assign a language to a module
统计信息
- 总下载量: 51
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: BSD-3-Clause
- 更新时间: 2019-09-09

