Formatter.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * Author: ljt
  5. * DateTime: 2016/10/20 11:49
  6. * Description:
  7. */
  8. namespace backend\components;
  9. use common\models\Circle;
  10. use common\models\CircleCat;
  11. use common\modules\user\models\User;
  12. use yii\helpers\ArrayHelper;
  13. use yii\helpers\Html;
  14. class Formatter extends \yii\i18n\Formatter
  15. {
  16. /**
  17. * 格式化用户名
  18. * @param $value
  19. * @return mixed
  20. * @author nodelog
  21. */
  22. public function asUser($value)
  23. {
  24. return $this->asAdmin($value);
  25. }
  26. public function asAdmin($value)
  27. {
  28. return ArrayHelper::getValue(User::findOne($value), 'nickname', '');
  29. }
  30. public function asColor($value, $color = 'default')
  31. {
  32. if ($color instanceof \Closure) {
  33. $color = call_user_func($color, $value);
  34. }
  35. return Html::tag('span', $value, ['class' => 'text-' . $color]);
  36. }
  37. /**
  38. * 添加后缀
  39. * @param $value
  40. * @param string $suffix
  41. * @return string
  42. * @author nodelog
  43. */
  44. public function asSuffix($value, $suffix = '')
  45. {
  46. return Html::tag('span', $value . $suffix);
  47. }
  48. /**
  49. * 添加前缀
  50. * @param $value
  51. * @param string $prefix
  52. * @return string
  53. * @author nodelog
  54. */
  55. public function asPrefix($value, $prefix = '')
  56. {
  57. return Html::tag('span', $prefix . $value);
  58. }
  59. /**
  60. * 多张图片格式化
  61. * @param $values
  62. * @param array $options
  63. * @return string
  64. * @author nodelog
  65. */
  66. public function asImages($values, $options = [])
  67. {
  68. $html = '';
  69. foreach ($values as $value) {
  70. $html .= parent::asImage($value, $options);
  71. }
  72. return $html;
  73. }
  74. public function asCircleCat($value)
  75. {
  76. return ArrayHelper::getValue(CircleCat::findOne($value), 'title', '');
  77. }
  78. public function asCircleType($value)
  79. {
  80. return Circle::$typeList[$value];
  81. }
  82. }