Layout.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. namespace Dcat\Admin\Form;
  3. use Dcat\Admin\Form;
  4. use Dcat\Admin\Layout\Column;
  5. class Layout
  6. {
  7. /**
  8. * @var Form
  9. */
  10. protected $form;
  11. /**
  12. * @var Column[]
  13. */
  14. protected $columns = [];
  15. public function __construct(Form $form)
  16. {
  17. $this->form = $form;
  18. }
  19. /**
  20. * @param int $width 1~12
  21. * @param mixed $content
  22. */
  23. public function column(int $width, $content)
  24. {
  25. $width = $width < 1 ? round(12 * $width) : $width;
  26. $column = new Column($content, $width);
  27. $this->columns[] = $column;
  28. }
  29. /**
  30. * @param int $width
  31. * @param mixed $content
  32. */
  33. public function prepend(int $width, $content)
  34. {
  35. $width = $width < 1 ? round(12 * $width) : $width;
  36. $column = new Column($content, $width);
  37. array_unshift($this->columns, $column);
  38. }
  39. /**
  40. * @param \Closure|null $callback
  41. *
  42. * @return MultipleForm
  43. */
  44. public function form(\Closure $callback = null)
  45. {
  46. $form = new Form\MultipleForm($this->form);
  47. $this->form->builder()->addForm($form);
  48. if ($callback) {
  49. $callback($form);
  50. }
  51. return $form;
  52. }
  53. /**
  54. * Build html of content.
  55. *
  56. * @return string
  57. */
  58. public function build()
  59. {
  60. $html = '<div class="row">';
  61. foreach ($this->columns as $column) {
  62. $html .= $column->render();
  63. }
  64. return $html.'</div>';
  65. }
  66. }