Fields.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. /**
  3. * 存储表的相关数据
  4. * @author Jinhui Zhu<jinhui.zhu@live.cn>2015-09-21 16:52
  5. *
  6. * 用法
  7. *
  8. */
  9. namespace Qii\Driver;
  10. final class Fields
  11. {
  12. const VERSION = '1.2';
  13. protected $keys;
  14. protected $fields;
  15. /**
  16. * 初始化数据表结构
  17. *
  18. * @param $fields
  19. *
  20. */
  21. public function __construct($fields)
  22. {
  23. if (!is_array($fields) && count($fields) == 0) throw new \Exception(\Qii::i(1508), __LINE__);
  24. $this->keys = new \stdClass();
  25. $this->fields = $fields;
  26. return $this;
  27. }
  28. /**
  29. * 设置数据表字段值,仅在列表中的才保存到对应的字段中
  30. *
  31. * @param $name
  32. * @param $val
  33. * @return $this
  34. */
  35. public function __set($name, $val)
  36. {
  37. if (in_array($name, $this->fields)) $this->keys->$name = $val;
  38. return $this;
  39. }
  40. /**
  41. * 判断是否存在相关键值
  42. *
  43. * @param $field
  44. * @return bool
  45. */
  46. public function isField($field)
  47. {
  48. if (isset($this->keys->$field)) return true;
  49. return false;
  50. }
  51. /**
  52. * 获取相关的键值
  53. *
  54. * @param $field
  55. * @return null
  56. */
  57. public function getField($field)
  58. {
  59. if (isset($this->keys->$field)) return $this->keys->$field;
  60. return null;
  61. }
  62. /**
  63. * 获取字段及值
  64. *
  65. * @return stdClass
  66. */
  67. public function getValues()
  68. {
  69. return $this->keys;
  70. }
  71. /**
  72. * 以array的形式返回字段及值
  73. *
  74. * @return array
  75. */
  76. public function getValueAsArray()
  77. {
  78. return (array)$this->keys;
  79. }
  80. public function __call($method, $argvs)
  81. {
  82. throw new \Qii\Exceptions\MethodNotFound(\Qii::i(1101, $method . ' Not found'), __LINE__);
  83. }
  84. }