Row.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. <?php
  2. namespace Dcat\Admin\Grid;
  3. use Closure;
  4. use Dcat\Admin\Grid;
  5. use Dcat\Admin\Support\Helper;
  6. use Illuminate\Contracts\Support\Arrayable;
  7. use Illuminate\Contracts\Support\Htmlable;
  8. use Illuminate\Contracts\Support\Jsonable;
  9. use Illuminate\Contracts\Support\Renderable;
  10. use Illuminate\Support\Fluent;
  11. class Row implements Arrayable
  12. {
  13. /**
  14. * @var Grid
  15. */
  16. protected $grid;
  17. /**
  18. * Row data.
  19. *
  20. * @var Fluent
  21. */
  22. protected $data;
  23. /**
  24. * Attributes of row.
  25. *
  26. * @var array
  27. */
  28. protected $attributes = [];
  29. public function __construct(Grid $grid, $data)
  30. {
  31. $this->grid = $grid;
  32. $this->data = new Fluent(Helper::array($data));
  33. }
  34. /**
  35. * Get the value of the model's primary key.
  36. *
  37. * @return mixed
  38. */
  39. public function key()
  40. {
  41. return $this->data->get($this->grid->keyName());
  42. }
  43. /**
  44. * Get attributes in html format.
  45. *
  46. * @return string
  47. */
  48. public function rowAttributes()
  49. {
  50. return $this->formatHtmlAttributes($this->attributes);
  51. }
  52. /**
  53. * Get column attributes.
  54. *
  55. * @param string $column
  56. *
  57. * @return string
  58. */
  59. public function columnAttributes($column)
  60. {
  61. if (
  62. ($column = $this->grid->columns()->get($column))
  63. && ($attributes = $column->getAttributes())
  64. ) {
  65. return $this->formatHtmlAttributes($attributes);
  66. }
  67. return '';
  68. }
  69. /**
  70. * Format attributes to html.
  71. *
  72. * @param array $attributes
  73. *
  74. * @return string
  75. */
  76. private function formatHtmlAttributes($attributes = [])
  77. {
  78. $attrArr = [];
  79. foreach ($attributes as $name => $val) {
  80. $attrArr[] = "$name=\"$val\"";
  81. }
  82. return implode(' ', $attrArr);
  83. }
  84. /**
  85. * Set attributes.
  86. *
  87. * @param array $attributes
  88. */
  89. public function setAttributes(array $attributes)
  90. {
  91. $this->attributes = $attributes;
  92. }
  93. /**
  94. * Set style of the row.
  95. *
  96. * @param array|string $style
  97. */
  98. public function style($style)
  99. {
  100. if (is_array($style)) {
  101. $style = implode('', array_map(function ($key, $val) {
  102. return "$key:$val";
  103. }, array_keys($style), array_values($style)));
  104. }
  105. if (is_string($style)) {
  106. $this->attributes['style'] = $style;
  107. }
  108. }
  109. /**
  110. * Get data of this row.
  111. *
  112. * @return Fluent
  113. */
  114. public function model()
  115. {
  116. return $this->data;
  117. }
  118. /**
  119. * Getter.
  120. *
  121. * @param mixed $attr
  122. *
  123. * @return mixed
  124. */
  125. public function __get($attr)
  126. {
  127. return $this->data->get($attr);
  128. }
  129. /**
  130. * Setter.
  131. *
  132. * @param mixed $attr
  133. * @param mixed $value
  134. *
  135. * @return void
  136. */
  137. public function __set($attr, $value)
  138. {
  139. $this->data[$attr] = $value;
  140. }
  141. /**
  142. * Get or set value of column in this row.
  143. *
  144. * @param string $name
  145. * @param mixed $value
  146. *
  147. * @return $this|mixed
  148. */
  149. public function column($name, $value = null)
  150. {
  151. if (is_null($value)) {
  152. $column = $this->data->get($name);
  153. return $this->output($column);
  154. }
  155. if ($value instanceof Closure) {
  156. $value = $value->call($this, $this->column($name));
  157. }
  158. $this->data[$name] = $value;
  159. return $this;
  160. }
  161. /**
  162. * @return array
  163. */
  164. public function toArray()
  165. {
  166. return $this->data->toArray();
  167. }
  168. /**
  169. * Output column value.
  170. *
  171. * @param mixed $value
  172. *
  173. * @return mixed|string
  174. */
  175. protected function output($value)
  176. {
  177. if ($value instanceof Renderable) {
  178. $value = $value->render();
  179. }
  180. if ($value instanceof Htmlable) {
  181. $value = $value->toHtml();
  182. }
  183. if ($value instanceof Jsonable) {
  184. $value = $value->toJson();
  185. }
  186. if (! is_null($value) && ! is_scalar($value)) {
  187. return sprintf('<pre class="dump">%s</pre>', json_encode($value, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE));
  188. }
  189. return $value;
  190. }
  191. }