Fields.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. use Qii\Exceptions\MethodNotFound;
  11. final class Fields
  12. {
  13. const VERSION = '1.2';
  14. protected $keys;
  15. protected $fields;
  16. /**
  17. * 初始化数据表结构
  18. *
  19. * @param $fields
  20. *
  21. */
  22. public function __construct($fields)
  23. {
  24. if (!is_array($fields) && count($fields) == 0) throw new \Exception(\Qii::i(1508), __LINE__);
  25. $this->keys = $fields;
  26. $this->fields = new \stdClass();
  27. return $this;
  28. }
  29. /**
  30. * 设置数据表字段值,仅在列表中的才保存到对应的字段中
  31. *
  32. * @param $name
  33. * @param $val
  34. * @return $this
  35. */
  36. public function __set($name, $val)
  37. {
  38. if (in_array($name, $this->keys)) $this->fields->$name = $val;
  39. return $this;
  40. }
  41. /**
  42. * isset
  43. * @param string $name
  44. * @return bool
  45. */
  46. public function __isset($name) {
  47. return isset($this->fields->$name);
  48. }
  49. /**
  50. * __get
  51. * @param string $name 名称
  52. * @return false
  53. */
  54. public function __get($name) {
  55. if (isset($this->fields->$name)) return $this->fields->$name;
  56. return false;
  57. }
  58. /**
  59. * unset 关键字
  60. * @param string $name 需要移除的关键字
  61. * @return void
  62. */
  63. public function __unset($name){
  64. if(isset($this->fields->$name)) unset($this->fields->$name);
  65. }
  66. /**
  67. * 批量设置字段的值
  68. * @param array $data
  69. * @return $this
  70. */
  71. public function setFieldsVal(array $data)
  72. {
  73. foreach ($data AS $name => $val) {
  74. $this->fields->$name = $val;
  75. }
  76. return $this;
  77. }
  78. /**
  79. * 判断是否存在相关键值
  80. *
  81. * @param $field
  82. * @return bool
  83. */
  84. public function hasField($field)
  85. {
  86. return in_array($field, $this->keys, true);
  87. }
  88. /**
  89. * 获取相关的键值
  90. *
  91. * @param $field
  92. * @return null
  93. */
  94. public function getField($field)
  95. {
  96. if (!$field) return null;
  97. if (isset($this->fields->$field)) return $this->fields->$field;
  98. return null;
  99. }
  100. /**
  101. * 获取所有字段
  102. *
  103. * @return mixed
  104. */
  105. public function getFields() {
  106. return $this->keys;
  107. }
  108. /**
  109. * 获取字段及值
  110. *
  111. * @return stdClass
  112. */
  113. public function getValues()
  114. {
  115. return $this->fields;
  116. }
  117. /**
  118. * 清除所有的值
  119. *
  120. * @return void
  121. */
  122. public function cleanAllFieldsVal(){
  123. foreach ($this->fields as $key => $val) {
  124. unset($this->fields->$key);
  125. }
  126. }
  127. /**
  128. * 以array的形式返回字段及值
  129. *
  130. * @return array
  131. */
  132. public function getValueAsArray()
  133. {
  134. return (array)$this->fields;
  135. }
  136. public function __call($method, $argvs)
  137. {
  138. throw new MethodNotFound(\Qii::i(1101, $method . ' Not found'), __LINE__);
  139. }
  140. }