项目作者: chaz6chez

项目描述 :
A simple validator
高级语言: PHP
项目地址: git://github.com/chaz6chez/structure.git
创建时间: 2018-09-06T08:12:43Z
项目社区:https://github.com/chaz6chez/structure

开源协议:MIT License

下载


Structure - 2.0.0

A useless validator


If you have any good suggestions and
comments, please contact chaz6chez1993@outlook.com


应用场景

  • 出入参的判断及过滤
  • 数据转化及映射

示例

验证场景

  • 创建一个Structure
  1. namespace YoursNamespace;
  2. use Structure\Struct;
  3. class User extends Struct {
  4. // 当id为非null值时,必须满足int 10 < id < 20
  5. /**
  6. * @rule int,min:10,max:20|id format error:1001
  7. */
  8. public $id;
  9. // name为必填,必须满足string 1 < name长度 < 20
  10. /**
  11. * @rule string,min:1,max:20|name format error:2001
  12. * @required true|name cannot be empty:2002
  13. */
  14. public $name;
  15. // 当sex为null时默认 string female
  16. // 满足string 0 < sex长度 < 10
  17. /**
  18. * @rule string,min:0,max:10|sex format error:1001
  19. * @default string:female
  20. */
  21. public $sex;
  22. }
  • 使用
  1. $struct = \YoursNamespace\User::factory();
  2. $struct->create([
  3. 'id' => 12,
  4. 'name' => 'John Smith'
  5. ]);
  6. // or
  7. $struct = \YoursNamespace\User::factory([
  8. 'id' => 12,
  9. 'name' => 'John Smith'
  10. ]);
  11. // or
  12. $struct = new \YoursNamespace\User();
  13. $struct = \YoursNamespace\User::factory();
  14. $struct->id = 12;
  15. $struct->name = 'John Smith';
  16. // or
  17. $struct = new \YoursNamespace\User([
  18. 'id' => 12,
  19. 'name' => 'John Smith'
  20. ]);
  21. if($struct->hasError()) {
  22. throw new \RuntimeException(
  23. $struct->getError()->getMessage() . '->' . $struct->getError()->getPosition(),
  24. $struct->getError()->getCode()
  25. );
  26. }
  27. return $struct->output(); // array

使用说明

  • 继承 Structure\Struct 及实现结构体
  • public属性接参
  1. namespace Example;
  2. use Structure\Struct;
  3. class User extends Struct{
  4. public $id;
  5. public $name;
  6. public $sex;
  7. }
  • 对要操作和转化的public属性进行注释
  1. /**
  2. * @rule string,min:10,max:20|name format error:1001
  3. */
  4. public $name;
  • 标签分为四个区域
    • a区:标签区
    • b区:场景区
    • c区:验证区
    • d区:内容信息
      1. /**
      2. * a区 b区 c区 d区
      3. * @标签 [场景] 验证方式 | 错误信息 : 错误码
      4. * ↓ ↓ ↓ ↓
      5. * @rule[check] string,min:1|error message:error code
      6. */
      7. `

标签区:" class="reference-link">标签区

  • 转换类的标签配合 filter()在output() 方法内生效,
    会对包含该标签的属性执行转换或者过滤操作
  • 验证类的标签在 validate() 中生效返回布尔值,
    通过getError() 可以获得错误信息
标签名 方式 类型 说明
@default">@default Structure\Handler、func、method 转换 func与method是将返回值默认赋予该标签
@required">@required true 验证 判断是否为必要值
@rule">@rule Structure\Handler、func、method 验证 以func与method的bool返回类型判断验证
@skip">@skip 验证 跳过验证
@ghost">@ghost 转换 跳过输出
@key">@key 转换 标记钥匙属性
@mapping">@mapping 映射键名 转换 映射键转换
@operator">@operator true、func、method 转换 键值特殊转换

@default" class="reference-link">@default">@default

  • 将该属性标记默认模式
  • 当该属性值为null且具备@default标签时生效
  1. /**
  2. * @default string:abc
  3. * @default int:123
  4. * @default float:1.1
  5. * @default object:Handler\Help
  6. * @default map:{"a":"1"}
  7. * @default array:["1"]
  8. * @default bool:true
  9. */
  10. public $name;
  • 验证区可使用func、method进行方法赋值
    • method:className,methodName 必须是静态方法
    • 方法执行过程中抛出的任何异常都会被忽略,并以默认Null赋值
  1. /**
  2. * @default func:is_array 会找到is_array函数
  3. * @default method:_set 会定位当前类的_set方法
  4. * @default method:Handler\Help,get 会定位Handler\Help类的get方法
  5. */
  6. public $name;
  7. public static function _set(){
  8. return 'abc';
  9. }
  • @default仅在output()输出时生效,若要直接使用类属性获取@default赋值,请使用以下方法:
  • 但不建议频繁使用,会多执行一次object clone操作
  1. // 以name的@default标签为string:John举例
  2. /**
  3. * @default string:John
  4. */
  5. public $name;
  1. $struct = new Struct();
  2. // @default无法生效,值为null
  3. $struct->name;
  4. // @default可以生效,值为字符串John
  5. $struct()->name;

@required" class="reference-link">@required">@required

  1. /**
  2. * @required true|name cannot empty
  3. */
  4. public $name;

@rule" class="reference-link">@rule">@rule

  • 通过预置Handler进行验证
  1. /**
  2. * @rule string,min:10,max:20|name format error
  3. * @rule int,min:10,max:20|name format error
  4. * @rule float,min:1.0,max:2.1,scale:3|name format error
  5. * @rule bool,true|name format error
  6. * @rule object,class:|name format error
  7. * @rule array,min:10,max:20,values:string|name format error
  8. * @rule map,min:1,max:5,keys:string,values:int|name format error
  9. * @rule url,path:true,query:true|name format error
  10. * @rule ip,ipv4:false,ipv6:false,private:false,reserved:false|name format error
  11. * @rule regex,min:10,max:20,regex:/.?/|name format error
  12. */
  13. public $name;
  • 验证区可使用func、method进行方法判断
    • method:className,methodName 必须是静态方法
    • 方法执行过程中任何异常会转化成StructureException抛出
  1. /**
  2. * @rule func:_set 会找到_set函数
  3. * @rule method:_set 会定位当前类的_set方法
  4. * @rule method:Handler\Help,get 会定位Handler\Help类的get方法
  5. */
  6. public $name;
  7. public static function _set($value) : bool
  8. {
  9. return $value === '_method';
  10. }
  11. }
  12. function _set($value) : bool
  13. {
  14. return $value === '_func';
  15. }

@ghost" class="reference-link">@ghost">@ghost

  1. // @ghost true
  2. $user->id = 'id';
  3. $user->name = 'name';
  4. $user->output();
  5. // 以上会输出
  6. [
  7. 'name' => 'name'
  8. ];
  9. $user->output(true);
  10. // 以上会输出
  11. [
  12. 'id' => 'id',
  13. 'name' => 'name'
  14. ];

@key" class="reference-link">@key">@key

  • 将该属性标记钥匙字段
  • 通过 filter()->output() 可以做到仅输出钥匙字段
  1. // @key true
  2. $user->id = 'id';
  3. $user->name = 'name';
  4. $user->filter(STRUCT_FILTER_KEY)->output();
  5. // 以上会输出
  6. [
  7. 'name' => 'name'
  8. ];
  9. $user->transfer(STRUCT_FILTER_KEY_REVERSE)->output();
  10. // 以上会输出
  11. [
  12. 'id' => 'id',
  13. ];

@skip" class="reference-link">@skip">@skip

  • 跳过验证,但不影响输出

@operator" class="reference-link">@operator">@operator

1. 识别medoo语法-where 并转换

  1. /**
  2. * @operator true
  3. */
  4. public $name;

通过 transfer()->output() 可以做到转换输出

  1. // @operator true
  2. $user->id = 'abc[>]';
  3. $user->transfer(STRUCT_TRANSFER_OPERATOR)->output();
  4. // 以上会输出
  5. [
  6. 'id[>]' => 'abc'
  7. ];
  8. $user->id = '123,456[<>]';
  9. $user->transfer(STRUCT_TRANSFER_OPERATOR)->output();
  10. // 以上会输出
  11. [
  12. 'id[<>]' => ['123','456'],
  13. ];
  1. 2.2以上版本完善了该标签下的类型转换
  2. 2.2以下版本中不会对类型转换
2.2以下版本:
  1. // @operator true
  2. $user->id = '123[>]';
  3. $user->transfer(STRUCT_TRANSFER_OPERATOR)->output();
  4. // 以上会输出
  5. [
  6. 'id[>]' => '123'
  7. ];
  8. // 并非期待的
  9. [
  10. 'id[>]' => 123
  11. ];
  12. // 此种状况会影响数据库查询的索引
2.2以上版本:
  • 不仅可以配合medoo语法做处理转换,也可以直接做类型转换
  • 字符串类型的数字默认会根据值类型转换
    • 整型内容字符串转换成整型
    • 小数字符串转换成浮点型
  1. // @operator true
  2. $user->id = '123[>]';
  3. $user->transfer(STRUCT_TRANSFER_OPERATOR)->output();
  4. // 以上会输出 int
  5. [
  6. 'id[>]' => 123
  7. ];
  8. // @operator true
  9. $user->id = '123';
  10. $user->transfer(STRUCT_TRANSFER_OPERATOR)->output();
  11. // 以上会输出 int
  12. [
  13. 'id' => 123
  14. ];
  15. $user->id = '1.23[>]';
  16. $user->transfer(STRUCT_TRANSFER_OPERATOR)->output();
  17. // 以上会输出 float
  18. [
  19. 'id[>]' => 1.23
  20. ];
  21. $user->id = '1.23';
  22. $user->transfer(STRUCT_TRANSFER_OPERATOR)->output();
  23. // 以上会输出
  24. [
  25. 'id' => 1.23
  26. ];
  • 可以使用强制转换标签
  1. # String :
  2. // @operator true
  3. $user->id = '123[String][>]';
  4. $user->transfer(STRUCT_TRANSFER_OPERATOR)->output();
  5. // 以上会输出 int
  6. [
  7. 'id[>]' => '123'
  8. ];
  9. // @operator true
  10. $user->id = '123[String]';
  11. $user->transfer(STRUCT_TRANSFER_OPERATOR)->output();
  12. // 以上会输出 int
  13. [
  14. 'id' => '123'
  15. ];
  16. # Int :
  17. // @operator true
  18. $user->id = '1[Int][>]';
  19. $user->transfer(STRUCT_TRANSFER_OPERATOR)->output();
  20. // 以上会输出 int
  21. [
  22. 'id[>]' => 1
  23. ];
  24. // @operator true
  25. $user->id = '1[Int]';
  26. $user->transfer(STRUCT_TRANSFER_OPERATOR)->output();
  27. // 以上会输出 int
  28. [
  29. 'id' => 1
  30. ];
  31. # Float :
  32. // @operator true
  33. $user->id = '123[Float][>]';
  34. $user->transfer(STRUCT_TRANSFER_OPERATOR)->output();
  35. // 以上会输出 int
  36. [
  37. 'id[>]' => 123.0
  38. ];
  39. // @operator true
  40. $user->id = '123[Float]';
  41. $user->transfer(STRUCT_TRANSFER_OPERATOR)->output();
  42. // 以上会输出 int
  43. [
  44. 'id' => 123.0
  45. ];
  46. # Bool :
  47. // @operator true
  48. $user->id = '1[Bool][>]'; // 0 false
  49. $user->transfer(STRUCT_TRANSFER_OPERATOR)->output();
  50. // 以上会输出 int
  51. [
  52. 'id[>]' => true
  53. ];
  54. // @operator true
  55. $user->id = '1[Bool]'; // 0 false
  56. $user->transfer(STRUCT_TRANSFER_OPERATOR)->output();
  57. // 以上会输出 int
  58. [
  59. 'id' => true
  60. ];

2.使用func、method进行转换

  • method:className,methodName 必须是静态方法
  • 方法执行过程中任何异常会转化成StructureException抛出
  1. /**
  2. * @operator func:_add 相当于_add($name)
  3. * @operator method:_add 相当于$this->_add($name)
  4. * @operator method:Handler\Help,_add 相当于Handler\Help::_add($name)
  5. */
  6. public $name;

@mapping" class="reference-link">@mapping">@mapping

  1. // @mapping key
  2. $user->id = 123;
  3. $user->name = 'john';
  4. $user->output();
  5. // 输出
  6. [
  7. 'id' => 123,
  8. 'name' => 'john'
  9. ];
  10. $user->transfer(STRUCT_TRANSFER_MAPPING)->output();
  11. // 输出
  12. [
  13. 'key' => 123,
  14. 'name' => 'john'
  15. ];

方法

  • 实例化
  1. $user = User::factory([
  2. 'id' => 1,
  3. 'name' => 'john'
  4. ],'check');
  • 输入

    • 使用create方法输入数据
    • 使用属性赋值输入数据
    • 使用create可以保存原始数据,使用属性赋值则不会保留原始数据
  1. // 1.使用create输入数据
  2. $user->create([
  3. 'id' => 1,
  4. 'name' => 'john'
  5. ]); // return $this
  1. // 2.使用属性赋值输入数据
  2. $user->id = 1;
  3. $user->name = 'john';
  4. // 使用create可以保存原始数据,建议使用create输入数据
  1. $user->getRaw(); // return array
  • 设置场景
  1. $user->scene('check'); // return $this
  • 转换

    • STRUCT_TRANSFER_MAPPING
    • STRUCT_TRANSFER_OPERATOR
  1. $user->transfer(STRUCT_TRANSFER_MAPPING); // return $this
  2. // STRUCT_TRANSFER_MAPPING
  3. // STRUCT_TRANSFER_OPERATOR
  4. // 接受可变长参数
  5. $user->transfer(
  6. STRUCT_TRANSFER_MAPPING,
  7. STRUCT_TRANSFER_OPERATOR
  8. ); // return $this
  • 过滤
    • STRUCT_FILTER_NULL
    • STRUCT_FILTER_EMPTY
    • STRUCT_FILTER_ZERO
    • STRUCT_FILTER_KEY
    • STRUCT_FILTER_KEY_REVERSE
    • STRUCT_FILTER_OPERATOR
    • STRUCT_FILTER_OPERATOR_REVERSE
  1. $user->filter(STRUCT_FILTER_NULL); // return $this
  2. // STRUCT_FILTER_NULL
  3. // STRUCT_FILTER_EMPTY
  4. // STRUCT_FILTER_ZERO
  5. // STRUCT_FILTER_KEY
  6. // STRUCT_FILTER_KEY_REVERSE
  7. // STRUCT_FILTER_OPERATOR
  8. // STRUCT_FILTER_OPERATOR_REVERSE
  9. // 接受可变长参数
  10. $user->filter(
  11. STRUCT_FILTER_NULL,
  12. STRUCT_FILTER_EMPTY
  13. ); // return $this
  1. $user->validate(); // return bool
  2. $user->hasError(); // return bool
  3. // true 有错误,验证未通过
  4. // false 无错误,验证通过
  • 获取错误

    • 需要在验证执行后才能获取错误信息
  1. $user->getError(); // return Structure\Error
  2. $user->getError()->getMessage(); // 错误信息 string
  3. $user->getError()->getCode(); // 错误码 string
  4. $user->getError()->getField(); // 字段名 string
  5. $user->getError()->getPosition(); // 错误定位 对应Handler对应的options字段
  6. $user->getErrors(); // return Structure\Error[]
  1. $user->output(); // return array
  2. $user->output(true); // 全量输出
  • 清洗
  1. $user->clean(); // 默认不装载raw数据
  2. $user->clean(true); // 装载raw数据

补充

  • Handler 接受自定义注册
  1. \Structure\Handler::register();
  • StructureException
  1. try {
  2. // ...
  3. }catch (\Structure\Exceptions\StructureException $exception){
  4. $exception->getPosition(); // 错误定位信息
  5. $exception->getMessage(); // Structure Exception [{position info}]
  6. $exception->getCode(); // 始终以-666返回
  7. }