xtompie/guard
Composer 安装命令:
composer require xtompie/guard
包简介
Guard container object for dealing with optional values or null values
README 文档
README
Guard container object for dealing with optional/null values.
use Xtompie\Guard\Guard; $user = Guard::of(request()->input('id')) ->filter(fn($id) => ctype_digit($id)) ->map(fn($id) => User::find($id)) ->not(fn() => abort(404)) ->is(fn($user) => info("User found {$user->id}")) ->get();
Installation
Using composer
composer require xtompie/guard
Docs
Guard protects for calling methods on nulls values. The value might or might not be present. It has elegant fluent chaining syntax. No need for if/else statments and additional variables. Guard syntax is cleaner and more readable. Guard object is immutable.
Methods:
of- Creates guardofEmpty- Creates empty guardis- Tells if value present or runs callback when value presentnot- Same asisbut negationget- Gets raw valuegetFn- Gets raw value with fallback callbackfilter- Filter the value if presentreject- Same asfilterbut negationmap- Maps value if presentassert- Throw exception if value is not presentblank- Gets guard with provided value if current is not presentblankFn- Same asblankbut with callbacklet- Returns guard capture mechanism for nested properties, calls, array offsets
More info in source Guard.php
Usage
NoValueException
use Xtompie\Guard\Guard; Guard::of(null)->assert()->get(); // NoValueException will be thrown
Default value
use Xtompie\Guard\Guard; echo Guard::of(null)->get('default'); // -> default
Complex type and value
use Xtompie\Guard\Guard; function divide($a, $b) { $b = Guard::of($b) ->map(fn($i) => (int)$i) ->reject(fn($i) => $i === 0) ->assert(\UnexpectedValueException::class) ->get(); return $a / $b; }
Let
let() Returns Let object. Offset get, property get, method call can be called on Let.
After that operation new Guard with operation result will be returned.
When offset, property, method not exist, empty Guard will be returned.
use Xtompie\Guard\Guard; $options = [ 'a' => 'A', 'b' => 'B', ]; $key = 'c'; echo Guard::of($options)->let()[$key]->get();
use Xtompie\Guard\Guard; echo Guard::of(new \stdClass()) ->let()->nonExistingMethod() ->let()->nonExistingProperty ->let()['nonExistingOffset'] ->get('Undefined') ;
Extending
namespace MyApp\Util; use Xtompie\Guard\Guard as BaseGuard; class Guard extends BaseGuard { public function or404() { $this->not(fn() => abort(404)); } public function reject0() { return $this->reject(fn($i) => $i === 0); } } echo gettype(Guard::of(0)->reject0()->get());
xtompie/guard 适用场景与选型建议
xtompie/guard 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 7 次下载、GitHub Stars 达 0, 最近一次更新时间为 2021 年 02 月 27 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「null」 「guard」 「optional」 「nullable」 「nullpointerexception」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 xtompie/guard 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 xtompie/guard 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 xtompie/guard 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Get nested array values and object properties.
This package provides null implementation for interactiv4/checker-contracts package.
php optional
Maybe for PHP
Java9 Optional implementation for PHP
统计信息
- 总下载量: 7
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 3
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-02-27