CopyableHelper.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. namespace App\Admin\Extensions;
  3. use App\Admin\Extensions\Form\Copyable as FormCopyable;
  4. use App\Admin\Extensions\Show\Copyable as ShowCopyable;
  5. use Dcat\Admin\Form;
  6. use Dcat\Admin\Show;
  7. /**
  8. * 复制功能助手类
  9. *
  10. * 提供便捷的方法为Show和Form添加复制功能
  11. */
  12. class CopyableHelper
  13. {
  14. /**
  15. * 为Show字段添加复制功能
  16. *
  17. * @param Show $show Show实例
  18. * @param string $field 字段名
  19. * @param string|null $label 字段标签
  20. * @return ShowCopyable
  21. */
  22. public static function addToShow(Show $show, string $field, ?string $label = null): ShowCopyable
  23. {
  24. return $show->field($field, $label)->as(function ($value) {
  25. return (new ShowCopyable())->setValue($value)->render();
  26. });
  27. }
  28. /**
  29. * 为Form字段添加复制功能(只读显示)
  30. *
  31. * @param Form $form Form实例
  32. * @param string $field 字段名
  33. * @param string|null $label 字段标签
  34. * @return FormCopyable
  35. */
  36. public static function addToForm(Form $form, string $field, ?string $label = null): FormCopyable
  37. {
  38. $copyable = new FormCopyable($field, [$label]);
  39. $copyable->setForm($form);
  40. return $copyable;
  41. }
  42. /**
  43. * 批量为Show添加复制功能
  44. *
  45. * @param Show $show Show实例
  46. * @param array $fields 字段配置数组,格式:['field_name' => 'Field Label']
  47. * @return void
  48. */
  49. public static function addMultipleToShow(Show $show, array $fields): void
  50. {
  51. foreach ($fields as $field => $label) {
  52. self::addToShow($show, $field, $label);
  53. }
  54. }
  55. /**
  56. * 批量为Form添加复制功能
  57. *
  58. * @param Form $form Form实例
  59. * @param array $fields 字段配置数组,格式:['field_name' => 'Field Label']
  60. * @return void
  61. */
  62. public static function addMultipleToForm(Form $form, array $fields): void
  63. {
  64. foreach ($fields as $field => $label) {
  65. self::addToForm($form, $field, $label);
  66. }
  67. }
  68. /**
  69. * 为Show字段添加带格式化的复制功能
  70. *
  71. * @param Show $show Show实例
  72. * @param string $field 字段名
  73. * @param string|null $label 字段标签
  74. * @param callable|null $formatter 格式化函数
  75. * @return ShowCopyable
  76. */
  77. public static function addFormattedToShow(Show $show, string $field, ?string $label = null, ?callable $formatter = null): ShowCopyable
  78. {
  79. return $show->field($field, $label)->as(function ($value) use ($formatter) {
  80. if ($formatter) {
  81. $value = $formatter($value);
  82. }
  83. return (new ShowCopyable())->setValue($value)->render();
  84. });
  85. }
  86. /**
  87. * 为Form字段添加带格式化的复制功能
  88. *
  89. * @param Form $form Form实例
  90. * @param string $field 字段名
  91. * @param string|null $label 字段标签
  92. * @param callable|null $formatter 格式化函数
  93. * @return FormCopyable
  94. */
  95. public static function addFormattedToForm(Form $form, string $field, ?string $label = null, ?callable $formatter = null): FormCopyable
  96. {
  97. $copyable = new FormCopyable($field, [$label]);
  98. $copyable->setForm($form);
  99. if ($formatter) {
  100. $copyable->customFormat(function ($value) use ($formatter) {
  101. return $formatter($value);
  102. });
  103. }
  104. return $copyable;
  105. }
  106. }