Label.php 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. <?php
  2. namespace Dcat\Admin\Grid\Displayers;
  3. use Dcat\Admin\Admin;
  4. use Dcat\Admin\Support\Helper;
  5. class Label extends AbstractDisplayer
  6. {
  7. protected $baseClass = 'label';
  8. public function display($style = 'primary', $max = null)
  9. {
  10. if (! $value = $this->value($max)) {
  11. return;
  12. }
  13. $original = $this->column->getOriginal();
  14. $defaultStyle = is_array($style) ? ($style['default'] ?? 'default') : 'default';
  15. $background = $this->formatStyle(
  16. is_array($style) ?
  17. (is_scalar($original) ? ($style[$original] ?? $defaultStyle) : current($style))
  18. : $style
  19. );
  20. return collect($value)->map(function ($name) use ($background) {
  21. return "<span class='{$this->baseClass}' {$background}>$name</span>";
  22. })->implode('&nbsp;');
  23. }
  24. protected function formatStyle($style)
  25. {
  26. $background = 'style="background:#d2d6de;color: #555"';
  27. if ($style !== 'default') {
  28. $style = Admin::color()->get($style, $style);
  29. $background = "style='background:{$style}'";
  30. }
  31. return $background;
  32. }
  33. protected function value($max)
  34. {
  35. $values = Helper::array($this->value);
  36. if ($max && count($values) > $max) {
  37. $values = array_slice($values, 0, $max);
  38. $values[] = '...';
  39. }
  40. return $values;
  41. }
  42. }