SplBean

SplBean是一个抽象类,借以实现类似Java Bean中半自动化ORM。 例如在java中常见的:

  1. $db->insert('user_table',$bean->toArray());

当用户需要Bean层时,只需要新建对应的Bean class并继承\Core\Component\Spl\SplBean实现其initialize()。

  1. class UserBean extends \Core\Component\Spl\SplBean {
  2. protected $account;
  3. protected $sex;
  4. protected $addTime;
  5. /**
  6. * @return mixed
  7. */
  8. public function getAccount()
  9. {
  10. return $this->account;
  11. }
  12. /**
  13. * @param mixed $account
  14. */
  15. public function setAccount($account)
  16. {
  17. $this->account = $account;
  18. }
  19. /**
  20. * @return mixed
  21. */
  22. public function getSex()
  23. {
  24. return $this->sex;
  25. }
  26. /**
  27. * @param mixed $sex
  28. */
  29. public function setSex($sex)
  30. {
  31. $this->sex = $sex;
  32. }
  33. /**
  34. * @return mixed
  35. */
  36. public function getAddTime()
  37. {
  38. return $this->addTime;
  39. }
  40. /**
  41. * @param mixed $addTime
  42. */
  43. public function setAddTime($addTime)
  44. {
  45. $this->addTime = $addTime;
  46. }
  47. protected function initialize()
  48. {
  49. // TODO: Implement initialize() method.
  50. $this->addTime = time();
  51. }
  52. }

使用

  1. $bean = new UserBean(
  2. array(
  3. 'account'=>"account",
  4. 'sex'=>0,
  5. 'other'=>'other'
  6. )
  7. );
  8. var_dump($bean->toArray());
  9. array(3) {
  10. ["account"]=>
  11. string(7) "account"
  12. ["sex"]=>
  13. int(0)
  14. ["addTime"]=>
  15. int(1504024995)
  16. }

Bean对象在实例化时,可以选择性的传递一个数组作为Bean对象初始化参数,Bean对象会自动过滤无关的键值对。

注意事项

  • 每个

    Bean

    必须实现initialize方法,该方法在

    Bean

    实例被创建时执行,用于为

    Bean

    某些成员变量做初始化设定。该方法中若对成员属性进行赋值,其优先级是最高的。 因此若需要保留在实例化对象时传入的属性值,请做判断再赋值。例如:

    1. protected function initialize()
    2. {
    3. // TODO: Implement initialize() method.
    4. if(emptye($this->addTime)){
    5. $this->addTime = time();
    6. }
    7. }
  • 成员变量请确保全部为protected,并实现其get/set方法。

  1. $bean->setSex(null);
  2. var_dump($bean->toArray(false,['account','sex']));
  3. array(2) {
  4. ["account"]=>
  5. string(7) "account"
  6. ["sex"]=>
  7. NULL
  8. }
  9. var_dump($bean->toArray(UserBean::FILTER_TYPE_NOT_NULL,['account','sex']));
  10. array(1) {
  11. ["account"]=>
  12. string(7) "account"
  13. }
  14. $bean->setSex('');
  15. var_dump($bean->toArray(UserBean::FILTER_TYPE_NOT_EMPTY));
  16. array(2) {
  17. ["account"]=>
  18. string(7) "account"
  19. ["addTime"]=>
  20. int(1504025589)
  21. }

在将Bean对象转数组的时候,可以选择Bean对象中的指定字段进行导出,以及对字段值未NULL或者是为空的进行过滤。

方法介绍

  1. const FILTER_NOT_NULL = 1;// 过滤不为NULL的属性
  2. const FILTER_NOT_EMPTY = 2;//0 过滤不为空的属性,不算empty

构造方法:

data 为类属性集合,autoCreateProperty 是否自动创建不存在的属性

  1. public function __construct(array $data = null,$autoCreateProperty = false)

获得所有属性集合:

  1. final public function allProperty():array

转换为数组:

columns 键值集合,filter 过滤条件(FILTER_NOT_NULL | FILTER_NOT_EMPTY),

  1. function toArray(array $columns = null,$filter = null):array

将数组转批量添加为bean规范的属性:

data 为类属性集合,autoCreateProperty 是否自动创建不存在的属性

  1. final public function arrayToBean(array $data,$autoCreateProperty = false):SplBean

添加属性:

name 属性名字,value 属性值

  1. final public function addProperty($name,$value = null):void

获得属性:

name 属性名字

  1. final public function getProperty($name)

指定需要被序列化成 JSON 的数据:

由于Bean类继承了JsonSerializable接口,遵循规范需要实现jsonSerialize方法。

  1. final public function jsonSerialize():array

初始化方法:

由于遵循Bean类的规范,构造方法的参数被固定了,如果需要预处理,可以采用该方法进行处理。

  1. protected function initialize():void

将类属性名和值的集合转换成字符串:

返回的是一个规范的json对象。

  1. public function __toString()