PropsRule.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. * 组件的样式
  29. * @var string|array
  30. */
  31. protected $style;
  32. /**
  33. * 组件的 DOM 属性
  34. *
  35. * @var array
  36. */
  37. protected $domProps = [];
  38. /**
  39. * 设置组件的 style 属性
  40. * @param string|array $style
  41. * @return $this
  42. */
  43. public function style($style)
  44. {
  45. $this->style = $style;
  46. return $this;
  47. }
  48. /**
  49. * @return array|string
  50. */
  51. public function getStyle()
  52. {
  53. return $this->style;
  54. }
  55. public function prop($name, $value)
  56. {
  57. $this->props[$name] = $value;
  58. return $this;
  59. }
  60. public function props(array $props)
  61. {
  62. $this->props = array_merge($this->props, $props);
  63. return $this;
  64. }
  65. public function attr($name, $value)
  66. {
  67. $this->attrs[$name] = $value;
  68. return $this;
  69. }
  70. public function attrs(array $attrs)
  71. {
  72. $this->attrs = array_merge($this->attrs, $attrs);
  73. return $this;
  74. }
  75. public function domProp($name, $value)
  76. {
  77. $this->domProps[$name] = $value;
  78. return $this;
  79. }
  80. public function domProps(array $domProps)
  81. {
  82. $this->domProps = array_merge($this->domProps, $domProps);
  83. return $this;
  84. }
  85. public function getProps()
  86. {
  87. return $this->props;
  88. }
  89. public function getProp($name)
  90. {
  91. return isset($this->props[$name]) ? $this->props[$name] : null;
  92. }
  93. public function getAttrs()
  94. {
  95. return $this->attrs;
  96. }
  97. public function getDomProps()
  98. {
  99. return $this->domProps;
  100. }
  101. public function parsePropsRule()
  102. {
  103. $rule = ['props' => (object)$this->props];
  104. if (count($this->attrs))
  105. $rule['attrs'] = $this->attrs;
  106. if (count($this->domProps))
  107. $rule['domProps'] = $this->domProps;
  108. if (!is_null($this->style))
  109. $rule['style'] = $this->style;
  110. return $rule;
  111. }
  112. }