定制 jdecool/guard-clauses 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

jdecool/guard-clauses

Composer 安装命令:

composer require jdecool/guard-clauses

包简介

Guard clauses pattern implementation library

README 文档

README

Latest Stable Version Build Status Total Downloads License Latest Unstable Version

A PHP library providing a clean and expressive way to implement guard clauses in your code. Built on top of webmozart/assert, this library offers a fluent interface for input validation and defensive programming.

Installation

You can install the package via Composer:

composer require jdecool/guard-clauses

Requirements

  • PHP 8.1 or higher

Usage

The library provides a Guard class with static methods for various assertions:

use JDecool\GuardClauses\Guard;

// Basic type checks
$validatedValue = Guard::string($value);
$validatedValue = Guard::integer($value);
$validatedValue = Guard::float($value);
$validatedValue = Guard::boolean($value);
$validatedValue = Guard::object($value);

// String validations
$validatedValue = Guard::stringNotEmpty($value);
$validatedValue = Guard::contains($string, $substring);
$validatedValue = Guard::startsWith($string, $prefix);
$validatedValue = Guard::endsWith($string, $suffix);

// Numeric comparisons
$validatedValue = Guard::greaterThan($number, $limit);
$validatedValue = Guard::lessThan($number, $limit);
$validatedValue = Guard::range($number, $min, $max);

// Array operations
$validatedValue = Guard::isArray($value);
$validatedValue = Guard::count($array, $expectedCount);
$validatedValue = Guard::keyExists($array, $key);

// And many more...

Each assertion method accepts an optional message parameter that will be used in the exception if the assertion fails:

$validatedValue = Guard::string($value, 'The value must be a string');

Error Handling

When an assertion fails, a GuardClausesException is thrown, which extends from InvalidArgumentException.

Development

To contribute to the development of this library:

  1. Clone the repository
  2. Install dependencies: composer install
  3. Run tests: composer test
  4. Run static analysis: composer lint

License

This package is open-sourced software licensed under the MIT license.

统计信息

  • 总下载量: 1
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-02-04