PropsRule.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. * @document http://php.form-create.com
  11. */
  12. namespace FormBuilder\Rule;
  13. trait PropsRule
  14. {
  15. /**
  16. * 组件的props
  17. *
  18. * @var array
  19. */
  20. protected $props = [];
  21. /**
  22. * 组件普通的 HTML 特性
  23. *
  24. * @var array
  25. */
  26. protected $attrs = [];
  27. /**
  28. * @var string|array
  29. */
  30. protected $style;
  31. /**
  32. * 组件的 DOM 属性
  33. *
  34. * @var array
  35. */
  36. protected $domProps = [];
  37. /**
  38. * 设置组件的 style 属性
  39. * @param string|array $style
  40. * @return $this
  41. */
  42. public function style($style)
  43. {
  44. $this->style = $style;
  45. return $this;
  46. }
  47. /**
  48. * @return array|string
  49. */
  50. public function getStyle()
  51. {
  52. return $this->style;
  53. }
  54. public function prop($name, $value)
  55. {
  56. $this->props[$name] = $value;
  57. return $this;
  58. }
  59. public function props(array $props)
  60. {
  61. $this->props = array_merge($this->props, $props);
  62. return $this;
  63. }
  64. public function attr($name, $value)
  65. {
  66. $this->attrs[$name] = $value;
  67. return $this;
  68. }
  69. public function attrs(array $attrs)
  70. {
  71. $this->attrs = array_merge($this->attrs, $attrs);
  72. return $this;
  73. }
  74. public function domProp($name, $value)
  75. {
  76. $this->domProps[$name] = $value;
  77. return $this;
  78. }
  79. public function domProps(array $domProps)
  80. {
  81. $this->domProps = array_merge($this->domProps, $domProps);
  82. return $this;
  83. }
  84. public function getProps()
  85. {
  86. return $this->props;
  87. }
  88. public function getProp($name)
  89. {
  90. return isset($this->props[$name]) ? $this->props[$name] : null;
  91. }
  92. public function getAttrs()
  93. {
  94. return $this->attrs;
  95. }
  96. public function getDomProps()
  97. {
  98. return $this->domProps;
  99. }
  100. public function parsePropsRule()
  101. {
  102. $rule = ['props' => (object)$this->props];
  103. if (count($this->attrs))
  104. $rule['attrs'] = $this->attrs;
  105. if (count($this->domProps))
  106. $rule['domProps'] = $this->domProps;
  107. return $rule;
  108. }
  109. }