wirnex/arguments-check

1.0.0 2015-02-17 15:00 UTC

This package is not auto-updated.

Last update: 2024-10-02 07:28:08 UTC


README

使用方法

假设我们有一个函数,它接受一个数组作为参数,然后通过一些键读取这个数组。在某些情况下,数组可能不包含特定的键。此类用于验证传递给函数的输入数组。

require 'ArgumentsCheck.php';

function feedMeWithAnArray(array $data)
{
	$balance = 0;
	// Using ArgumentsCheck class we describe what array we expect
	ArgumentsCheck::CheckArguments(array('userId', 'sum', 'timestamp'),  $data);
	//
	// here we sure that all arguments were passed to function
	// and we don't need any extra validation code
	$userId  = $data['userId'];
	$balance += $data['sum'];
	$time	 = $data['timestamp'];
}

// calling this function with not exactly what is wants
feedMeWithAnArray(array(
  'userId'=>130,
  'sum'=>1000,
  'timestam'=>'2014-06-29 13:44' // we have mistake here in key "timestam" while the function expects "timestamp"
                                 // <- and here comes an exception
)); 

运行测试

phpunit test.php

安装

composer require wirnex/arguments-check