Row.php 1.7 KB

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