PropsRule.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. /**
  3. * PHP表单生成器
  4. *
  5. * @package FormBuilder
  6. * @author xaboy <xaboy2005@qq.com>
  7. * @version 2.0
  8. * @license MIT
  9. * @link https://github.com/xaboy/form-builder
  10. */
  11. namespace FormBuilder\Rule;
  12. trait PropsRule
  13. {
  14. /**
  15. * 组件的props
  16. *
  17. * @var array
  18. */
  19. protected $props = [];
  20. /**
  21. * 组件普通的 HTML 特性
  22. *
  23. * @var array
  24. */
  25. protected $attrs = [];
  26. /**
  27. * 组件的 DOM 属性
  28. *
  29. * @var array
  30. */
  31. protected $domProps = [];
  32. public function prop($name, $value)
  33. {
  34. $this->props[$name] = $value;
  35. return $this;
  36. }
  37. public function props(array $props)
  38. {
  39. $this->props = array_merge($this->props, $props);
  40. return $this;
  41. }
  42. public function attr($name, $value)
  43. {
  44. $this->attrs[$name] = $value;
  45. return $this;
  46. }
  47. public function attrs(array $attrs)
  48. {
  49. $this->attrs = array_merge($this->attrs, $attrs);
  50. return $this;
  51. }
  52. public function domProp($name, $value)
  53. {
  54. $this->domProps[$name] = $value;
  55. return $this;
  56. }
  57. public function domProps(array $domProps)
  58. {
  59. $this->domProps = array_merge($this->domProps, $domProps);
  60. return $this;
  61. }
  62. public function getProps()
  63. {
  64. return $this->props;
  65. }
  66. public function getProp($name)
  67. {
  68. return isset($this->props[$name]) ? $this->props[$name] : null;
  69. }
  70. public function getAttrs()
  71. {
  72. return $this->attrs;
  73. }
  74. public function getDomProps()
  75. {
  76. return $this->domProps;
  77. }
  78. public function parsePropsRule()
  79. {
  80. $rule = ['props' => (object)$this->props];
  81. if (count($this->attrs))
  82. $rule['attrs'] = $this->attrs;
  83. if (count($this->domProps))
  84. $rule['domProps'] = $this->domProps;
  85. return $rule;
  86. }
  87. }