HasDisplayers.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. <?php
  2. namespace Dcat\Admin\Grid\Column;
  3. use Dcat\Admin\Grid\Column;
  4. use Dcat\Admin\Grid\Displayers\AbstractDisplayer;
  5. use Illuminate\Contracts\Support\Arrayable;
  6. use Illuminate\Support\Arr;
  7. use Illuminate\Support\Collection;
  8. trait HasDisplayers
  9. {
  10. /**
  11. * @var ValueFilter
  12. */
  13. protected $valueFilter;
  14. /**
  15. * Display using display abstract.
  16. *
  17. * @param string $abstract
  18. * @param array $arguments
  19. *
  20. * @return Column
  21. */
  22. public function displayUsing($abstract, $arguments = [])
  23. {
  24. $grid = $this->grid;
  25. $column = $this;
  26. return $this->display(function ($value) use ($grid, $column, $abstract, $arguments) {
  27. /** @var AbstractDisplayer $displayer */
  28. $displayer = new $abstract($value, $grid, $column, $this);
  29. return $displayer->display(...$arguments);
  30. });
  31. }
  32. /**
  33. * Display column using array value map.
  34. *
  35. * @param array $values
  36. * @param null $default
  37. *
  38. * @return $this
  39. */
  40. public function using(array $values, $default = null)
  41. {
  42. return $this->display(function ($value) use ($values, $default) {
  43. if (is_null($value)) {
  44. return $default;
  45. }
  46. return Arr::get($values, $value, $default);
  47. });
  48. }
  49. /**
  50. * @param string $color
  51. *
  52. * @return $this
  53. */
  54. public function bold($color = 'text-80')
  55. {
  56. return $this->display(function ($value) use ($color) {
  57. if (! $value) {
  58. return $value;
  59. }
  60. return "<b class='$color'>$value</b>";
  61. });
  62. }
  63. /**
  64. * Display column with "long2ip".
  65. *
  66. * @param null $default
  67. *
  68. * @return $this
  69. */
  70. public function long2ip($default = null)
  71. {
  72. return $this->display(function ($value) use ($default) {
  73. if (! $value) {
  74. return $default;
  75. }
  76. return long2ip($value);
  77. });
  78. }
  79. /**
  80. * Render this column with the given view.
  81. *
  82. * @param string $view
  83. *
  84. * @return $this
  85. */
  86. public function view($view)
  87. {
  88. $name = $this->name;
  89. return $this->display(function ($value) use ($view, $name) {
  90. $model = $this;
  91. return view($view, compact('model', 'value', 'name'))->render();
  92. });
  93. }
  94. /**
  95. * @param string $val
  96. *
  97. * @return $this
  98. */
  99. public function prepend($val)
  100. {
  101. return $this->display(function ($v) use (&$val) {
  102. if (is_array($v)) {
  103. array_unshift($v, $val);
  104. return $v;
  105. } elseif ($v instanceof Collection) {
  106. return $v->prepend($val);
  107. }
  108. return $val.$v;
  109. });
  110. }
  111. /**
  112. * @param string $val
  113. *
  114. * @return $this
  115. */
  116. public function append($val)
  117. {
  118. return $this->display(function ($v) use (&$val) {
  119. if (is_array($v)) {
  120. array_push($v, $val);
  121. return $v;
  122. } elseif ($v instanceof Collection) {
  123. return $v->push($val);
  124. }
  125. return $v.$val;
  126. });
  127. }
  128. /**
  129. * Split a string by string.
  130. *
  131. * @param string $d
  132. *
  133. * @return $this
  134. */
  135. public function explode(string $d = ',')
  136. {
  137. return $this->display(function ($v) use ($d) {
  138. if (is_array($v) || $v instanceof Arrayable) {
  139. return $v;
  140. }
  141. return $v ? explode($d, $v) : [];
  142. });
  143. }
  144. /**
  145. * Display the fields in the email format as gavatar.
  146. *
  147. * @param int $size
  148. *
  149. * @return $this
  150. */
  151. public function gravatar($size = 30)
  152. {
  153. return $this->display(function ($value) use ($size) {
  154. $src = sprintf(
  155. 'https://www.gravatar.com/avatar/%s?s=%d',
  156. md5(strtolower($value)),
  157. $size
  158. );
  159. return "<img src='$src' class='img img-circle'/>";
  160. });
  161. }
  162. /**
  163. * Limit the number of characters in a string.
  164. *
  165. * @param int $limit
  166. * @param string $end
  167. *
  168. * @return $this
  169. */
  170. public function limit($limit = 100, $end = '...')
  171. {
  172. return $this->display(function ($value) use ($limit, $end) {
  173. if (mb_strlen($value, 'UTF-8') <= $limit) {
  174. return $value;
  175. }
  176. return mb_substr($value, 0, $limit).$end;
  177. });
  178. }
  179. /**
  180. * @return $this
  181. */
  182. public function asEmpty()
  183. {
  184. return $this->display('');
  185. }
  186. /**
  187. * @param string|\Closure $valueKey
  188. * @param string|\Closure $operator
  189. *
  190. * @return $this
  191. */
  192. public function valueAsFilter($valueKey = null, $operator = '=')
  193. {
  194. $valueFilter = $this->getValueFilter();
  195. $valueFilter->setup($valueKey, $operator);
  196. return $this->display(function ($value) use ($valueFilter) {
  197. return $valueFilter->render($value);
  198. });
  199. }
  200. /**
  201. * @return ValueFilter
  202. */
  203. public function getValueFilter()
  204. {
  205. if (! $this->valueFilter) {
  206. $this->valueFilter = new ValueFilter($this);
  207. }
  208. return $this->valueFilter;
  209. }
  210. }