| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116 |
- <?php
- namespace App\Admin\Extensions;
- use App\Admin\Extensions\Form\Copyable as FormCopyable;
- use App\Admin\Extensions\Show\Copyable as ShowCopyable;
- use Dcat\Admin\Form;
- use Dcat\Admin\Show;
- /**
- * 复制功能助手类
- *
- * 提供便捷的方法为Show和Form添加复制功能
- */
- class CopyableHelper
- {
- /**
- * 为Show字段添加复制功能
- *
- * @param Show $show Show实例
- * @param string $field 字段名
- * @param string|null $label 字段标签
- * @return ShowCopyable
- */
- public static function addToShow(Show $show, string $field, ?string $label = null): ShowCopyable
- {
- return $show->field($field, $label)->as(function ($value) {
- return (new ShowCopyable())->setValue($value)->render();
- });
- }
- /**
- * 为Form字段添加复制功能(只读显示)
- *
- * @param Form $form Form实例
- * @param string $field 字段名
- * @param string|null $label 字段标签
- * @return FormCopyable
- */
- public static function addToForm(Form $form, string $field, ?string $label = null): FormCopyable
- {
- $copyable = new FormCopyable($field, [$label]);
- $copyable->setForm($form);
- return $copyable;
- }
- /**
- * 批量为Show添加复制功能
- *
- * @param Show $show Show实例
- * @param array $fields 字段配置数组,格式:['field_name' => 'Field Label']
- * @return void
- */
- public static function addMultipleToShow(Show $show, array $fields): void
- {
- foreach ($fields as $field => $label) {
- self::addToShow($show, $field, $label);
- }
- }
- /**
- * 批量为Form添加复制功能
- *
- * @param Form $form Form实例
- * @param array $fields 字段配置数组,格式:['field_name' => 'Field Label']
- * @return void
- */
- public static function addMultipleToForm(Form $form, array $fields): void
- {
- foreach ($fields as $field => $label) {
- self::addToForm($form, $field, $label);
- }
- }
- /**
- * 为Show字段添加带格式化的复制功能
- *
- * @param Show $show Show实例
- * @param string $field 字段名
- * @param string|null $label 字段标签
- * @param callable|null $formatter 格式化函数
- * @return ShowCopyable
- */
- public static function addFormattedToShow(Show $show, string $field, ?string $label = null, ?callable $formatter = null): ShowCopyable
- {
- return $show->field($field, $label)->as(function ($value) use ($formatter) {
- if ($formatter) {
- $value = $formatter($value);
- }
- return (new ShowCopyable())->setValue($value)->render();
- });
- }
- /**
- * 为Form字段添加带格式化的复制功能
- *
- * @param Form $form Form实例
- * @param string $field 字段名
- * @param string|null $label 字段标签
- * @param callable|null $formatter 格式化函数
- * @return FormCopyable
- */
- public static function addFormattedToForm(Form $form, string $field, ?string $label = null, ?callable $formatter = null): FormCopyable
- {
- $copyable = new FormCopyable($field, [$label]);
- $copyable->setForm($form);
-
- if ($formatter) {
- $copyable->customFormat(function ($value) use ($formatter) {
- return $formatter($value);
- });
- }
-
- return $copyable;
- }
- }
|