ephpicman/boolean-utility
最新稳定版本:v1.0.0
Composer 安装命令:
composer require ephpicman/boolean-utility
包简介
A comprehensive Boolean utility for PHP, supporting logical operations, counting, and majority evaluation.
README 文档
README
A comprehensive PHP utility for working with booleans and boolean-like values.
Designed to simplify boolean checks, logical operations, counting, and majority logic in PHP projects.
Table of Contents
Installation
Use Composer to install:
composer require ephpicman/boolean-utility
Autoloading is PSR-4 compliant:
use EphpicMan\BooleanUtility\BooleanUtility;
Usage
The BooleanUtility class provides tools for:
- Boolean type checks
- Conversion of various values to boolean
- Logical operations (
AND,OR,XOR, etc.) - Counting true/false values
- Determining majority
- Aliases and toggle operations
Class: BooleanUtility
Checking Methods
| Method | Description |
|---|---|
is(mixed $variable): bool | Checks if a variable is strictly boolean. |
isNot(mixed $variable): bool | Checks if a variable is not boolean. |
isTruthy(mixed $variable): bool | Checks if a value evaluates to true. |
isFalsy(mixed $variable): bool | Checks if a value evaluates to false. |
Conversion
| Method | Description | |
|---|---|---|
toBoolean(mixed ...$variables): bool | array | Converts values to boolean. Strings matching true values, numbers, booleans, and arrays are all supported. |
Logical Operations
| Method | Description | |
|---|---|---|
and(mixed ...$variables): bool | Logical AND across multiple values. | |
nand(mixed ...$variables): bool | Logical NAND across multiple values. | |
or(mixed ...$variables): bool | Logical OR across multiple values. | |
nor(mixed ...$variables): bool | Logical NOR across multiple values. | |
xor(mixed ...$variables): bool | Logical XOR across multiple values. | |
xnor(mixed ...$variables): bool | Logical XNOR across multiple values. | |
not(bool ...$variables): bool | array | Logical NOT / toggle boolean values. |
Counting Methods
| Method | Description |
|---|---|
countTrue(mixed ...$variables): int | Counts how many inputs are true. |
countFalse(mixed ...$variables): int | Counts how many inputs are false. |
Majority Methods
| Method | Description | |
|---|---|---|
majorityTrue(mixed ...$variables): bool | Returns true if the majority of inputs are true. | |
majorityFalse(mixed ...$variables): bool | Returns true if the majority of inputs are false. | |
majority(mixed ...$variables): bool | null | Returns the majority value (true or false). Returns null if tie. |
Aliases
| Method | Description |
|---|---|
allTrue(...$vars): bool | Alias for and(). |
allFalse(...$vars): bool | Alias for nor(). |
xand(...$vars): bool | Alias for and(). |
xnand(...$vars): bool | Alias for nand(). |
anyTrue(...$vars): bool | Alias for or(). |
anyFalse(...$vars): bool | Alias for nor(). |
Toggle
| Method | Description | |
|---|---|---|
toggle(bool ...$variables): bool | array | Alias for not(). |
Examples
use EphpicMan\BooleanUtility\BooleanUtility;
// Simple boolean checks
BooleanUtility::is(true); // true
BooleanUtility::isNot('test'); // true
// Convert mixed values
BooleanUtility::toBoolean('yes'); // true
BooleanUtility::toBoolean(0); // false
BooleanUtility::toBoolean([1, 'no']); // [true, false]
// Logical operations
BooleanUtility::and(true, 'yes', 1); // true
BooleanUtility::or(false, 0, 'ok'); // true
BooleanUtility::xor(true, true, false); // false
// Counting
BooleanUtility::countTrue(true, false, 'yes'); // 2
BooleanUtility::countFalse(true, false, 'yes'); // 1
// Majority
BooleanUtility::majorityTrue(true, false, 'yes'); // true
BooleanUtility::majority(false, false, true, true); // null (tie)
// Toggle
BooleanUtility::toggle(true, false); // [false, true]
License
This package is released under the MIT License. Anyone may use, modify, or distribute it personally or commercially.
Git Repository
统计信息
- 总下载量: 2
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-12-04