DoneStep.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. namespace Dcat\Admin\Form;
  3. use Dcat\Admin\Form;
  4. use Dcat\Admin\Support\Helper;
  5. use Illuminate\Contracts\Support\Renderable;
  6. use Illuminate\Support\Arr;
  7. use Illuminate\Support\Str;
  8. class DoneStep
  9. {
  10. /**
  11. * @var Form
  12. */
  13. protected $form;
  14. /**
  15. * @var string
  16. */
  17. protected $title;
  18. /**
  19. * @var string
  20. */
  21. protected $contents;
  22. /**
  23. * @var \Closure
  24. */
  25. protected $builder;
  26. /**
  27. * @var string
  28. */
  29. protected $elementId;
  30. public function __construct(Form $form, $title, \Closure $callback)
  31. {
  32. $this->form = $form;
  33. $this->builder = $callback;
  34. $this->elementId = 'done-step-'.Str::random();
  35. $this->title($title);
  36. }
  37. /**
  38. * @return Form
  39. */
  40. public function form()
  41. {
  42. return $this->form;
  43. }
  44. /**
  45. * @param string $title
  46. * @return void|string
  47. */
  48. public function title($title = null)
  49. {
  50. if ($title === null) {
  51. return $this->title;
  52. }
  53. $this->title = value($title);
  54. }
  55. /**
  56. * @param string|\Closure|Renderable $contents
  57. * @return void
  58. */
  59. public function content($contents)
  60. {
  61. $this->contents = $contents;
  62. }
  63. /**
  64. * @return string
  65. */
  66. public function getElementId(): string
  67. {
  68. return $this->elementId;
  69. }
  70. /**
  71. * @return array
  72. */
  73. public function getNewId()
  74. {
  75. return $this->form->getKey();
  76. }
  77. /**
  78. * @param string|null $key
  79. * @param mixed|null $default
  80. * @return array|mixed
  81. */
  82. public function input($key = null, $default = null)
  83. {
  84. $input = $this->form->getUpdates();
  85. if ($key === null) {
  86. return $input;
  87. }
  88. return Arr::get($input, $key, $default);
  89. }
  90. /**
  91. * @return void
  92. */
  93. public function finish()
  94. {
  95. $value = call_user_func($this->builder, $this);
  96. if ($value) {
  97. $this->content($value);
  98. }
  99. }
  100. /**
  101. * @return string
  102. */
  103. public function render()
  104. {
  105. return Helper::render($this->contents);
  106. }
  107. }