laragear / compare
一个小型实用特质,用于流畅地比较对象值。
Requires
- php: 8.*
- illuminate/support: 9.*|10.*
Requires (Dev)
- orchestra/testbench: ^7.22|8.*
README
一个小型实用特质,用于流畅地比较对象值。
if ($comparable->is('wallet.available')->aboveZero()) { return 'You can buy with your wallet credits.'; } return 'No credits? Buy some in the store!';
成为赞助商
您的支持使我能够保持此包免费、更新和可维护。或者,您可以 传播信息!
要求
- Laravel 9.x 或更高版本
- PHP 8.0 或更高版本
安装
启动 Composer 并在您的项目中要求此包。
composer require laragear/comparable
这就完成了。
用法
将 Comparable
特质添加到您的对象中,然后开始使用 is()
与键名进行比较,除非您想比较整个对象。
use Laragear\Compare\Comparable; class Wallet { use Comparable; // ... }
完成之后,您可以使用 is()
开始比较。
$wallet = new Wallet(['credits' => 1000]); if ($wallet->is('credits')->aboveZero()) { return 'You have credits available, so go and spend some!'; } return 'Your wallet is empty. Add some credits!';
如果不指定键,则将使用整个对象进行比较。
在幕后,它使用 Laravel 辅助函数中的 data_get()
通过“点”符号从您的对象中检索值。
if ($wallet->is('pending.total')->aboveZero()) { return "You have {$wallet->pending->total} pending."; }
要否定一个条件,只需发出 not()
或 not
属性。
if ($wallet->is('pending.total')->not->belowZero()) { return 'If you dont have credits, we can lend you some.'; }
可用条件
aboveZero()
检查数值是否大于零。
if ($wallet->is('amount')->aboveZero()) { return 'You still have credits left.'; }
belowZero()
检查数值是否小于零。
if ($wallet->is('amount')->belowZero()) { return 'The Wallet is empty.'; }
between()
检查数值是否介于两个数之间。
if ($product->is('weight')->between(10, 20)) { return 'This product can be picked up at the store.'; }
将 false
作为第三个参数发出将使比较 排他性。
if ($product->is('weight')->between(10, 20, false)) { return 'The weight of the product is between 10.1 and 19.9 lbs.'; }
blank()
检查值是否为 “blank”。
if ($wallet->is('name')->blank()) { return 'Default Wallet'; }
counting()
检查列表是否包含确切的项数。
if ($cart->is('items')->counting(10)) { return 'You are elegible for a discount for exactly 10 items.'; }
containing()
检查字符串是否包含字符串,或者列表是否包含项。
if ($product->is('name')->containing('discounted')) { return 'Discount are not applied to already discounted items.'; }
containingOneItem()
检查值是否为列表且只包含一个项。
if ($cart->is('items')->containingOneItem()) { returns 'For free delivery, you need to add more than one item.'; }
equalOrGreaterThan()
检查数值或值列表计数是否等于或大于数值。
if ($cart->is('items')->equalOrGreaterThan(10)) { return 'For more than 10 items, you will need to pick up them in the store.' } if ($cart->is('total')->equalOrGreaterThan(1000)) { return 'Your cart qualifies for free delivery'; }
exactly()
检查值是否严格等于给定的值。
if ($product->is('name')->exactly('shoes')) { return 'Welp, these are shoes.'; }
false()
检查值是否严格等于 false
。
if ($product->is('can_deliver')->false()) { return 'The product cannot be delivered.'; }
falsy()
检查值是否 评估 为 false
。
if ($product->is('address')->false()) { return 'The product cannot be delivered without an address.'; }
equalOrLessThan()
检查数值或值列表计数是否等于或小于数值。
if ($cart->is('items')->equalOrLessThan(1)) { return 'Add more items to qualify for delivery.' } if ($cart->is('total')->equalOrLessThan(1000)) { return 'Your cart does not qualify for free delivery'; }
filled()
检查值是否为 “filled”。
if ($wallet->is('name')->blank()) { return 'Default Wallet'; }
greaterThan()
检查数值或列表项计数是否大于指定的数值。
if ($product->is('weight')->greaterThan(100)) { return 'This product is too heavy to be sent. You have to pick it up.'; }
lessThan()
检查数值或列表项计数是否小于指定的数值。
if ($product->is('weight')->greaterThan(100)) { return 'This product is too heavy to be sent. You have to pick it up.'; } if ($cary->is('items')->greaterThan(10)) { return 'We wil divide your order on multiple deliveries of 10 items'; }
null()
检查值是否为 null。
if ($cart->is('promo_code')->null()) { return 'You can add a promo code to your code.'; }
true()
检查值是否严格等于 true
。
if ($product->is('can_deliver')->true()) { return 'The product can be delivered.'; }
truthy()
检查值是否 评估 为 true
。
if ($product->is('address')->true()) { return 'The products will be delivered to the issued address.'; }
zero()
检查数值或列表项计数是否正好为零。
if ($cart->is('delivery_total')->zero()) { return 'This order has no cost of delivery. Enjoy!'; } if ($cart->is('items')->zero()) { return 'Your cart is empty.'; }
高阶比较
您可以使用动态属性访问比较结果。
Laravel Octane 兼容性
- 没有单例使用过时的应用程序实例。
- 没有单例使用过时的配置实例。
- 没有单例使用过时的请求实例。
- 没有编写静态属性。
应该没有问题与 Laravel Octane 一起使用此包。
安全
如果您发现任何与安全相关的问题,请通过电子邮件 darkghosthunter@gmail.com 反馈,而不是使用问题跟踪器。
许可证
本特定软件包版本在发布时遵循 MIT 许可证 的条款。
Laravel 是 Taylor Otwell 的商标。版权所有 © 2011-2022 Laravel LLC。