Row.php 4.3 KB

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