djb/guard

PHP的Swift 2 guard功能

v1.0 2015-11-19 17:09 UTC

This package is not auto-updated.

Last update: 2024-09-28 18:21:25 UTC


README

Swift 2启发的PHP guard,使简单的验证更易于阅读。

需要更多功能或被请求时,将添加更多方法 - 欢迎提交pull请求!

安装

composer require djb/guard

建议用法

常见的验证需要:电子邮件地址、字符串、整数、大于、小于等应由包处理。

之后,建议扩展类以包含您自己的可读性较高的guard方法

class MyFlyingAppGuard extends \DJB\Guard\Guard {
  
  /**
   * This var allows you to describe your exceptions.
   * Should the raises() function be used, the exception raised will automatically contain the message associated with the relevant validation issue.
   */
  protected $exceptionMessages = [
   'can fly' => 'The provided object cannot fly!'
  ];

  private function canFly() {
    if ( ! $this->value->canFly()) $this->setIssue('can fly');
  }
}

使用中

guard($plane)->canFly()->otherwise(function($error) {
  // $error holds the error description from which validation failed.. but as we are only checking one thing we don't need it!
  throw new \Exception('The provided plane cannot fly!');
});

guard($plane)->canFly()->raises();
// Results in Excepton with message: 'The provided object cannot fly!'

请注意,要使用您的新的类中的guard()函数,您需要修改包中的helpers.php文件,以实例化您的新类而不是包的基础类。

语法

语法灵感来自Swift 2的guard功能,要保护一个简单变量并抛出异常,您可以使用otherwise方法

function createBag($studs) {
  guard($studs)->greaterThan(5)->otherwise(function($error) {
  	  // $error will be 'equal or greater than'
	  throw new \Exception('There are not enough studs to make your gothic bag!');
  });
  return new Bag($studs);
}

或者您可以使用passes获取布尔通过或失败

if (! guard($var)->isString()->equal('hello')->passes()) {
  return 'Error!';
}

或者您可以使用raises让guard为您抛出Exception

$min  = '2015-10-25 00:00:00';
$max  = '2015-10-26 00:00:00';
$date = '2015-10-24 00:00:00';

// The below will raise an Exception as $date does not fall between $min and $max
guard($date)->between($min, $max)->raises();
// Results in Excepton with message: 'The variable provided does not fall between the required boundaries.'

由guard处理的所有验证方法都具有内置的验证错误描述,如上所示。

您可以通过扩展\DJB\Guard\Guard类来覆盖此功能,如建议用法中的示例所示,并使用$exceptionMessages属性提供与您想要覆盖的错误代码匹配的键。例如

protected $exceptionMessages = ['is between' => 'Make it between 12 and 24, please'];

将覆盖between($min, $max)验证方法的错误消息。

验证方法

您可以使用以下方法检查变量

  • exists - 存在(如果变量有长度,或是一个boolarray
  • isTrue - 评估为布尔true
  • isFalse - 评估为布尔false
  • isDate - 是有效的日期格式
  • isClass($className) - 是$className类的实例
  • isArray - 是一个array
  • isInteger - 是一个integer
  • isString - 是一个string
  • isAlpha - 只包含字母字符
  • isNumeric - 只包含数字字符
  • isEmailAddress - 是电子邮件地址格式
  • isURL - 是一个URL
  • isActiveURL - 是一个可以访问的URL(即网站是活跃的)
  • isJSON - 是有效的JSON字符串
  • isIP - 是IP地址格式
  • length($length) - 是指定的长度
  • in(Array $acceptable) - 是接受值之一
  • notIn(Array $excluded) - 不是排除值之一
  • after($date) - 在提供的日期之后(可以是日期格式字符串,或DateTime实例)
  • before($date) - 在提供的日期之前(可以是日期格式字符串,或DateTime实例)
  • between($min, $max) - 在$min$max值之间(可以是日期格式字符串,DateTime实例或数字)
  • equal($value) - 等于提供的值(数字,字符串)
  • lessThan($value) - 小于提供的值(数字)
  • greaterThan($value) - 大于提供的值(数字)
  • equalOrLessThan($value) - 是否等于或小于提供值(数字)
  • equalOrGreaterThan($value) - 是否等于或大于提供值(数字)