CustomComponent.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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\Driver;
  13. use FormBuilder\Contract\CustomComponentInterface;
  14. use FormBuilder\Rule\BaseRule;
  15. use FormBuilder\Rule\CallPropsRule;
  16. use FormBuilder\Rule\ChildrenRule;
  17. use FormBuilder\Rule\ControlRule;
  18. use FormBuilder\Rule\EmitRule;
  19. use FormBuilder\Rule\PropsRule;
  20. use FormBuilder\Rule\ValidateRule;
  21. /**
  22. * 自定义组件
  23. * Class CustomComponent
  24. */
  25. class CustomComponent implements CustomComponentInterface, \JsonSerializable, \ArrayAccess
  26. {
  27. use BaseRule;
  28. use ChildrenRule;
  29. use EmitRule;
  30. use PropsRule;
  31. use ValidateRule;
  32. use CallPropsRule;
  33. use ControlRule;
  34. protected static $propsRule = [];
  35. protected $defaultProps = [];
  36. protected $appendRule = [];
  37. /**
  38. * CustomComponent constructor.
  39. * @param null|string $type
  40. */
  41. public function __construct($type = null)
  42. {
  43. $this->setRuleType(is_null($type) ? $this->getComponentName() : $type)->props($this->defaultProps);
  44. }
  45. public function __toString()
  46. {
  47. return $this->toJson();
  48. }
  49. public function __invoke()
  50. {
  51. return $this->build();
  52. }
  53. public function toJson()
  54. {
  55. return json_encode($this->build());
  56. }
  57. protected function getComponentName()
  58. {
  59. return lcfirst(basename(str_replace('\\', '/', get_class($this))));
  60. }
  61. public function appendRule($name, $value)
  62. {
  63. $this->appendRule[$name] = $name == 'props' ? (object)$value : $value;
  64. return $this;
  65. }
  66. public function getRule()
  67. {
  68. return array_merge(
  69. $this->parseBaseRule(),
  70. $this->parseEmitRule(),
  71. $this->parsePropsRule(),
  72. $this->parseValidateRule(),
  73. $this->parseChildrenRule(),
  74. $this->parseControlRule()
  75. );
  76. }
  77. public function build()
  78. {
  79. return $this->appendRule + $this->getRule();
  80. }
  81. public function jsonSerialize()
  82. {
  83. return $this->build();
  84. }
  85. public function offsetExists($offset)
  86. {
  87. return isset($this->props[$offset]);
  88. }
  89. public function offsetGet($offset)
  90. {
  91. return $this->props[$offset];
  92. }
  93. public function offsetSet($offset, $value)
  94. {
  95. $this->props[$offset] = $value;
  96. }
  97. public function offsetUnset($offset)
  98. {
  99. unset($this->props[$offset]);
  100. }
  101. }