Skip to content

Commit

Permalink
change namespace and class name to upper
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Sep 26, 2017
1 parent cf032cd commit 80f0c24
Show file tree
Hide file tree
Showing 12 changed files with 62 additions and 34 deletions.
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,13 @@ git clone https://github.com/inhere/php-validate.git // github
<a name="how-to-use"></a>
### 方式 1: 创建一个新的class,并继承Validation

创建一个新的class,并继承 `inhere\validate\Validation`。用于一个(或一系列相关)请求的验证, 相当于 laravel 的 表单请求验证
创建一个新的class,并继承 `Inhere\Validate\Validation`。用于一个(或一系列相关)请求的验证, 相当于 laravel 的 表单请求验证

> 此方式是最为完整的使用方式
```php

use inhere\validate\Validation;
use Inhere\Validate\Validation;

class PageRequest extends Validation
{
Expand Down Expand Up @@ -129,11 +129,11 @@ $db->save($safeData);

### 方式 2: 直接使用类 Validation

需要快速简便的使用验证时,可直接使用 `inhere\validate\Validation`
需要快速简便的使用验证时,可直接使用 `Inhere\Validate\Validation`

```php

use inhere\validate\Validation;
use Inhere\Validate\Validation;

class SomeController
{
Expand All @@ -160,14 +160,14 @@ $db->save($safeData);

### 方式 1: 创建一个新的class,使用 ValidationTrait

创建一个新的class,并使用 Trait `inhere\validate\ValidationTrait`。 此方式是高级自定义的使用方式, 可以方便的嵌入到其他类中
创建一个新的class,并使用 Trait `Inhere\Validate\ValidationTrait`。 此方式是高级自定义的使用方式, 可以方便的嵌入到其他类中

如下, 嵌入到一个数据模型类中,添加数据库记录前自动进行验证

```php
class DataModel
{
use \inhere\validate\ValidationTrait;
use \Inhere\Validate\ValidationTrait;

protected $data = [];

Expand Down Expand Up @@ -235,7 +235,7 @@ $db->save($safeData);

## 如何添加自定义验证器

- 在继承了 `inhere\validate\Validation` 的子类添加验证方法. 请看上面的 **使用方式1**
- 在继承了 `Inhere\Validate\Validation` 的子类添加验证方法. 请看上面的 **使用方式1**
- 通过 `Validation::addValidator()` 添加自定义验证器. e.g:

```php
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "inhere/php-validate",
"type": "library",
"description": "a simple validate library of the php",
"keywords": ["library","validate"],
"keywords": ["library", "validate"],
"homepage": "http://github.com/inhere/php-validate",
"license": "MIT",
"authors": [
Expand All @@ -17,7 +17,7 @@
},
"autoload": {
"psr-4": {
"inhere\\validate\\" : "src/"
"Inhere\\Validate\\" : "src/"
}
},
"suggest": {
Expand Down
2 changes: 1 addition & 1 deletion examples/DataModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/
class DataModel
{
use \inhere\validate\ValidationTrait;
use \Inhere\Validate\ValidationTrait;

protected $data = [];

Expand Down
2 changes: 1 addition & 1 deletion examples/PageRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
/**
* Class PageRequest
*/
class PageRequest extends \inhere\validate\Validation
class PageRequest extends \Inhere\Validate\Validation
{
public function rules()
{
Expand Down
6 changes: 3 additions & 3 deletions examples/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

spl_autoload_register(function($class)
{
// e.g. "inhere\validate\ValidationTrait"
// e.g. "Inhere\Validate\ValidationTrait"
if (strpos($class,'\\')) {
$file = dirname(__DIR__) . '/src/' . trim(strrchr($class,'\\'),'\\'). '.php';
} else {
Expand Down Expand Up @@ -81,8 +81,8 @@

echo "\n----------------------------\n use Validation\n----------------------------\n\n";

$valid = \inhere\validate\Validation::make($data, $rules)
->setAttrTrans([
$valid = \Inhere\Validate\Validation::make($data, $rules)
->setTranslates([
'goods.pear' => '梨子'
])
->validate([], false);
Expand Down
4 changes: 2 additions & 2 deletions src/ErrorMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
* Time: 11:26
*/

namespace inhere\validate;
namespace Inhere\Validate;

/**
* Class ErrorMessage
* @package inhere\validate
* @package Inhere\Validate
*
*/
class ErrorMessage
Expand Down
6 changes: 2 additions & 4 deletions src/FilterList.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,14 @@
* @date 2015.08.05
* 过滤器(strainer/filter): 过滤数据,去除不合要求的数据,返回过滤后的数据(始终返回字符串, 全部不符合返回空字符串)
*/
namespace inhere\validate;
namespace Inhere\Validate;

/**
* Class FilterList
* @package inhere\validate
* @package Inhere\Validate
*/
final class FilterList
{
/////////////////////////////// php internal Filter ///////////////////////////////

/**
* simple trim space
* @param $var
Expand Down
4 changes: 2 additions & 2 deletions src/Filtration.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
* Time: 23:19
*/

namespace inhere\validate;
namespace Inhere\Validate;

/**
* Class Filtration
* @package inhere\validate
* @package Inhere\Validate
*
* usage:
*
Expand Down
31 changes: 29 additions & 2 deletions src/Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
/**
*
*/
namespace inhere\validate;
namespace Inhere\Validate;

/**
* Class StrHelper
* @package inhere\validate
* @package Inhere\Validate
*/
class Helper
{
Expand Down Expand Up @@ -193,4 +193,31 @@ public static function getValueOfArray(array $array, $key, $default = null)

return $array;
}

/**
* @param $cb
* @param array $args
* @return mixed
*/
public static function call($cb, array $args = [])
{
$args = array_values($args);

if (
(is_object($cb) && method_exists($cb, '__invoke')) ||
(is_string($cb) && function_exists($cb))
) {
$ret = $cb(...$args);
} elseif (is_array($cb)) {
list($obj, $mhd) = $cb;

$ret = is_object($obj) ? $obj->$mhd(...$args) : $obj::$mhd(...$args);
} elseif (method_exists('Swoole\Coroutine', 'call_user_func_array')) {
$ret = \Swoole\Coroutine::call_user_func_array($cb, $args);
} else {
$ret = call_user_func_array($cb, $args);
}

return $ret;
}
}
6 changes: 3 additions & 3 deletions src/Validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
* Time: 23:19
*/

namespace inhere\validate;
namespace Inhere\Validate;

/**
* Class Validation
* @package inhere\validate
* @package Inhere\Validate
*
* usage:
*
Expand Down Expand Up @@ -52,7 +52,7 @@ public function __construct(array $data = [], array $rules = [], array $translat
$this
->setRules($rules)
->setScene($scene)
->setAttrTrans($translates);
->setTranslates($translates);

if ($startValidate) {
$this->validate();
Expand Down
13 changes: 8 additions & 5 deletions src/ValidationTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
* Used: 主要功能是 hi
*/

namespace inhere\validate;
namespace Inhere\Validate;

/**
* Trait ValidationTrait
* @package inhere\validate
* @package Inhere\Validate
*
* @property array $data To verify the data list. please define it on main class. 待验证的数据列表
*/
Expand Down Expand Up @@ -256,7 +256,7 @@ public function validate(array $onlyChecked = [], $stopOnError = null)
}

// 设定了为空跳过 并且 值为空
if ($skipOnEmpty && call_user_func($isEmpty, $value)) {
if ($skipOnEmpty && Helper::call($isEmpty, $value)) {
continue;
}

Expand Down Expand Up @@ -796,10 +796,13 @@ public function addValidator(string $name, \Closure $callback, string $msg = '')
* @param \Closure $callback
* @param string $msg
*/
public static function setValidator(string $name, \Closure $callback, string $msg = '')
public static function setValidator(string $name, \Closure $callback, string $msg = null)
{
self::$_validators[$name] = $callback;
self::setDefaultMessage($name, $msg);

if ($msg) {
self::setDefaultMessage($name, $msg);
}
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/ValidatorList.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
* 后期通过类似 $_GET['test']='help'; 将不会存在 输入数据常量中(INPUT_GET 没有test项)。
*/

namespace inhere\validate;
namespace Inhere\Validate;

/**
* Class ValidatorList
* @package inhere\validate
* @package Inhere\Validate
*/
final class ValidatorList
{
Expand Down

0 comments on commit 80f0c24

Please sign in to comment.