pixelbrackets/not-empty

not empty() - 添加方法以确定变量是否为空或存在 - 受 Ruby on Rails 启发

1.1.0 2021-12-06 14:23 UTC

This package is auto-updated.

Last update: 2024-09-07 01:02:57 UTC


README

Version Build Status Made With License

添加方法以确定变量是否为空或存在。

受 Ruby on Rails 启发。

想法

此软件包是黑客马拉松的产品。想法是将Ruby on Rails 方法 blankpresent 移植到 PHP。

PHP 有 empty 方法,但更常见的情况是,我需要检查变量是否不为空。这就是为什么还有一个 notEmpty 方法。

请参阅以下列表以比较这些方法的不同行为。

┌─────────────────────────┬─────────┬────────────┬─────────┬───────────┐
│          VALUE          │ EMPTY() │ NOTEMPTY() │ BLANK() │ PRESENT() │
├─────────────────────────┼─────────┼────────────┼─────────┼───────────┤
│ string ''               │ true    │            │ true    │           │
│ string 'acme'           │         │ true       │         │ true      │
│ string ' '              │         │ true       │ true    │           │
│ string '   '            │         │ true       │ true    │           │
│ string "\t\n"           │         │ true       │ true    │           │
│ int 0                   │ true    │            │ true    │           │
│ float 0.0               │ true    │            │ true    │           │
│ int 42                  │         │ true       │         │ true      │
│ float 3.14              │         │ true       │         │ true      │
│ string '0'              │ true    │            │ true    │           │
│ string '1337'           │         │ true       │         │ true      │
│ null                    │ true    │            │ true    │           │
│ bool true               │         │ true       │         │ true      │
│ bool false              │ true    │            │ true    │           │
│ array []                │ true    │            │ true    │           │
│ array ['acme']          │         │ true       │         │ true      │
│ object {}               │         │ true       │ true    │           │
│ object {"foo" => "bar"} │         │ true       │         │ true      │
└─────────────────────────┴─────────┴────────────┴─────────┴───────────┘

需求

  • PHP

安装

Packagist 条目 https://packagist.org.cn/packages/pixelbrackets/not-empty/

源代码

https://gitlab.com/pixelbrackets/not-empty/

镜像 https://github.com/pixelbrackets/not-empty/

使用方法

请参阅 tests/demo.php

use Pixelbrackets\NotEmpty\Blank;
use Pixelbrackets\NotEmpty\NotEmpty;
use Pixelbrackets\NotEmpty\Present;

# Is a string empty? → Use PHPs »empty«
if (empty('')) { … }

# Is a string not empty? → Use »empty() === false«
if (empty('acme') === false) { … }

# → …or use »notEmpty« instead
if (NotEmpty::notEmpty('acme')) { … }

# A strings with whitespaces is blank → use a combination of conditions
if (is_string('   ') && empty(trim('   '))) { … }

# → …or use »blank« instead
if (Blank::blank('   ')) { … }

# → use »present« to check the opposite of »blank«
if (Present::present('acme')) { … }

💡 如果您想将方法作为全局函数使用(使用 blank() 而不是命名空间化的 Pixelbrackets\NotEmpty\Blank::blank()),则请安装 "global" 分支,使用 composer require pixelbrackets/not-empty:dev-global

许可

GNU 通用公共许可证版本 2 或更高版本

GNU 通用公共许可证可在 https://gnu.ac.cn/copyleft/gpl.html 找到。

作者

Dan Untenzu (mail@pixelbrackets.de / @pixelbrackets)

变更日志

请参阅 ./CHANGELOG.md

贡献

此脚本是开源的,因此请使用、修补、扩展或分叉它。

此软件包目前未在积极开发,但仍欢迎贡献。