SplBean

用于定义表结构,过滤掉无效字段数据。

相关class位置

  • SplBean
    • namespace: EasySwoole\Spl\SplBean

SplBean相关方法

方法列表

方法名称参数说明备注
__constructarray $data = null,$autoCreateProperty = false构造函数,初始化bean数据
allProperty过滤并转换成数组数据
toArrayarray $columns = null,$filter = null转字符
toArrayWithMappingarray $columns = null,$filter = null获取过滤后带有字段别名的数组数据
arrayToBeanarray $data,$autoCreateProperty = false设置类属性
addProperty$name,$value = null设置类成员变量
getProperty$name获取类成员变量值
jsonSerialize获取类成员变量集合
initialize初始化操作
setKeyMapping设置keyMapping关系,也就是字段别名
setClassMapping设置classMapping关系,也就是关联类
restorearray $data = [], $autoCreateProperty = false重新初始化bean数据
classMap绑定关联类

基本使用

  1. /**
  2. *
  3. * User: LuffyQAQ
  4. * Date: 2019/10/16 16:45
  5. * Email: <1769360227@qq.com>
  6. */
  7. include "./vendor/autoload.php";
  8. use EasySwoole\Spl\SplBean;
  9. class TestBean extends SplBean
  10. {
  11. public $a = 2;
  12. protected $b;
  13. private $c;
  14. protected $d_d;
  15. protected function setKeyMapping(): array
  16. {
  17. return [
  18. 'd-d' => "d_d"
  19. ];
  20. }
  21. }
  22. $bean = new TestBean([
  23. 'a'=>'a',
  24. 'b'=>'b',
  25. 'c'=>'c',
  26. 'd_d'=>'d_d'
  27. ]);
  28. var_dump($bean->allProperty());
  29. $data = $bean->toArray(null, function ($a) {
  30. if (in_array($a, ['d_d'])) {
  31. return $a;
  32. }
  33. });
  34. $bean = new TestBean([
  35. 'a'=>1,
  36. 'b'=>2,
  37. 'c'=>3,
  38. 'd_d'=>4
  39. ]);
  40. $data = $bean->toArrayWithMapping(['a', 'b', 'd-d'], function ($val) {
  41. return $val;
  42. });
  43. var_dump($data);
  44. var_dump($bean->toArrayWithMapping(['a','d-d']));
  45. $bean = new TestBean();
  46. $bean->addProperty('a', 'es');
  47. $bean->addProperty('b', 'es');
  48. $bean->addProperty('d_d', 'es');
  49. var_dump($bean->toArray());
  50. var_dump($bean->getProperty('a'));
  51. var_dump( $bean->jsonSerialize());
  52. var_dump($bean->restore()->toArray());