Row.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. namespace Dcat\Admin\Layout;
  3. use Illuminate\Contracts\Support\Renderable;
  4. class Row implements Buildable, Renderable
  5. {
  6. /**
  7. * @var Column[]
  8. */
  9. protected $columns = [];
  10. /**
  11. * Row constructor.
  12. *
  13. * @param string $content
  14. */
  15. public function __construct($content = '')
  16. {
  17. if (!empty($content)) {
  18. $this->column(12, $content);
  19. }
  20. }
  21. /**
  22. * Add a column.
  23. *
  24. * @param int $width
  25. * @param $content
  26. */
  27. public function column($width, $content)
  28. {
  29. $width = $width < 1 ? round(12 * $width) : $width;
  30. $column = new Column($content, $width);
  31. $this->addColumn($column);
  32. }
  33. /**
  34. * @param Column $column
  35. */
  36. protected function addColumn(Column $column)
  37. {
  38. $this->columns[] = $column;
  39. }
  40. /**
  41. * Build row column.
  42. */
  43. public function build()
  44. {
  45. $this->startRow();
  46. foreach ($this->columns as $column) {
  47. $column->build();
  48. }
  49. $this->endRow();
  50. }
  51. /**
  52. * Start row.
  53. */
  54. protected function startRow()
  55. {
  56. echo '<div class="row">';
  57. }
  58. /**
  59. * End column.
  60. */
  61. protected function endRow()
  62. {
  63. echo '</div>';
  64. }
  65. /**
  66. * Render row.
  67. *
  68. * @return string
  69. */
  70. public function render()
  71. {
  72. ob_start();
  73. $this->build();
  74. $contents = ob_get_contents();
  75. ob_end_clean();
  76. return $contents;
  77. }
  78. }