From 1a79ea75847ffd16b9f0b2fea6777d3eec84da48 Mon Sep 17 00:00:00 2001 From: Alexander Nikushkin Date: Tue, 2 Jul 2024 14:14:39 +0500 Subject: [PATCH] docs(resources): modifyRawValue and modifyRawValue methods --- .../views/pages/en/fields/index.blade.php | 51 +++++++++++++++++++ .../views/pages/en/resources/events.blade.php | 15 ++++++ .../en/resources/import_export.blade.php | 30 ++++++++--- .../views/pages/ru/fields/index.blade.php | 51 +++++++++++++++++++ .../views/pages/ru/resources/events.blade.php | 15 ++++++ .../ru/resources/import_export.blade.php | 30 ++++++++--- 6 files changed, 180 insertions(+), 12 deletions(-) diff --git a/resources/views/pages/en/fields/index.blade.php b/resources/views/pages/en/fields/index.blade.php index 4fc2f993..cfa6b699 100644 --- a/resources/views/pages/en/fields/index.blade.php +++ b/resources/views/pages/en/fields/index.blade.php @@ -22,6 +22,7 @@ ['url' => '#wrapper', 'label' => 'Wrapper'], ['url' => '#reactive', 'label' => 'Reactive'], ['url' => '#on-change', 'label' => 'onChange methods'], + ['url' => '#for-value', 'label' => 'Methods for values'], ['url' => '#scheme', 'label' => 'Scheme field\'s work'], ] ]" @@ -1207,6 +1208,56 @@ public function someMethod(MoonShineRequest $request): void Recipes +Methods for values + + + + + The fromRaw() method allows you to add a closure to get the final value from the original.
+ This closure is used when importing data. +
+ + +/** + * @param Closure(mixed $raw, static): mixed $callback + * @return $this + */ +fromRaw(Closure $callback) // [tl! focus] + + + +use App\Enums\StatusEnum; +use MoonShine\Fields\Enum; + +Enum::make('Status') + ->attach(StatusEnum::class) + ->fromRaw(fn(string $raw, Enum $ctx) => StatusEnum::tryFrom($raw)) // [tl! focus] + + + + + + The modifyRawValue() method allows you to add a closure to obtain the raw value.
+ This closure is used when exporting data. +
+ + +/** + * @param Closure(mixed $raw, static): mixed $callback + * @return $this + */ +modifyRawValue(Closure $callback) // [tl! focus] + + + +use App\Enums\StatusEnum; +use MoonShine\Fields\Enum; + +Enum::make('Status') + ->attach(StatusEnum::class) + ->modifyRawValue(fn(StatusEnum $raw, Enum $ctx) => $raw->value)) // [tl! focus] + + Scheme field's work diff --git a/resources/views/pages/en/resources/events.blade.php b/resources/views/pages/en/resources/events.blade.php index ba16f064..5cf431d1 100644 --- a/resources/views/pages/en/resources/events.blade.php +++ b/resources/views/pages/en/resources/events.blade.php @@ -61,6 +61,21 @@ protected function afterMassDeleted(array $ids): void { // Logic here } + +public function beforeImportFilling(array $data): array +{ + return $data; +} + +public function beforeImported(Model $item): Model +{ + return $item; +} + +public function afterImported(Model $item): Model +{ + return $item; +} diff --git a/resources/views/pages/en/resources/import_export.blade.php b/resources/views/pages/en/resources/import_export.blade.php index 20ac0cc5..b0dae198 100644 --- a/resources/views/pages/en/resources/import_export.blade.php +++ b/resources/views/pages/en/resources/import_export.blade.php @@ -69,15 +69,21 @@ public function export(): ?ExportHandler - useOnImport(mixed $condition = null) +useOnImport(mixed $condition = null, ?Closure $fromRaw = null) + +
  • $condition - method execution condition,
  • +
  • $fromRaw - a closure that returns values from the raw.
  • +
    + namespace App\MoonShine\Resources; +use App\Enums\StatusEnum; use App\Models\Post; +use MoonShine\Fields\Enum; use MoonShine\Fields\ID; -use MoonShine\Fields\Text; use MoonShine\Resources\ModelResource; class PostResource extends ModelResource @@ -94,8 +100,9 @@ public function fields(): array ID::make() ->useOnImport(), // [tl! focus] - Text::make('Title', 'title') - ->useOnImport() // [tl! focus] + Enum::make('Status') + ->attach(StatusEnum::class) + ->useOnImport(fromRaw: static fn(string $raw, Enum $ctx) => StatusEnum::tryFrom($raw)), // [tl! focus] ]; } @@ -212,13 +219,20 @@ public function boot(): void - showOnExport(mixed $condition = null) +showOnExport(mixed $condition = null, ?Closure $modifyRawValue = null) + +
  • $condition - method execution condition,
  • +
  • $modifyRawValue - closure to get the raw value.
  • +
    + namespace App\MoonShine\Resources; +use App\Enums\StatusEnum; use App\Models\Post; +use MoonShine\Fields\Enum; use MoonShine\Fields\Text; use MoonShine\Resources\ModelResource; @@ -234,7 +248,11 @@ public function fields(): array { return [ Text::make('Title', 'title') - ->showOnExport() // [tl! focus] + ->showOnExport(), // [tl! focus] + + Enum::make('Status') + ->attach(StatusEnum::class) + ->showOnExport(modifyRawValue: static fn(StatusEnum $raw, Enum $ctx) => $raw->value), // [tl! focus] ]; } diff --git a/resources/views/pages/ru/fields/index.blade.php b/resources/views/pages/ru/fields/index.blade.php index 0e1ede65..e05ff5ab 100644 --- a/resources/views/pages/ru/fields/index.blade.php +++ b/resources/views/pages/ru/fields/index.blade.php @@ -22,6 +22,7 @@ ['url' => '#wrapper', 'label' => 'Wrapper'], ['url' => '#reactive', 'label' => 'Реактивность'], ['url' => '#on-change', 'label' => 'Методы onChange'], + ['url' => '#for-value', 'label' => 'Методы для значений'], ['url' => '#scheme', 'label' => 'Схема работы поля'], ] ]" @@ -1207,6 +1208,56 @@ public function someMethod(MoonShineRequest $request): void Recipes +Методы для значений + + + + + Метод fromRaw() позволяет добавить замыкание для получения итоговое значение из исходного.
    + Данное замыкание используется при импорте данных. +
    + + +/** + * @param Closure(mixed $raw, static): mixed $callback + * @return $this + */ + modifyRawValue(Closure $callback) // [tl! focus] + + + +use App\Enums\StatusEnum; +use MoonShine\Fields\Enum; + +Enum::make('Status') + ->attach(StatusEnum::class) + ->fromRaw(fn(string $raw, Enum $ctx) => StatusEnum::tryFrom($raw)) // [tl! focus] + + + + + + Метод modifyRawValue() позволяет добавить замыкание для получения необработанного значения.
    + Данное замыкание используется при экспорте данных. +
    + + +/** + * @param Closure(mixed $raw, static): mixed $callback + * @return $this + */ +modifyRawValue(Closure $callback) // [tl! focus] + + + +use App\Enums\StatusEnum; +use MoonShine\Fields\Enum; + +Enum::make('Status') + ->attach(StatusEnum::class) + ->modifyRawValue(fn(StatusEnum $raw, Enum $ctx) => $raw->value)) // [tl! focus] + + Схема работы поля diff --git a/resources/views/pages/ru/resources/events.blade.php b/resources/views/pages/ru/resources/events.blade.php index 370a6a60..103ad098 100644 --- a/resources/views/pages/ru/resources/events.blade.php +++ b/resources/views/pages/ru/resources/events.blade.php @@ -61,6 +61,21 @@ protected function afterMassDeleted(array $ids): void { // Logic here } + +public function beforeImportFilling(array $data): array +{ + return $data; +} + +public function beforeImported(Model $item): Model +{ + return $item; +} + +public function afterImported(Model $item): Model +{ + return $item; +}
    diff --git a/resources/views/pages/ru/resources/import_export.blade.php b/resources/views/pages/ru/resources/import_export.blade.php index deacd313..0a388fa4 100644 --- a/resources/views/pages/ru/resources/import_export.blade.php +++ b/resources/views/pages/ru/resources/import_export.blade.php @@ -69,15 +69,21 @@ public function export(): ?ExportHandler - useOnImport(mixed $condition = null) +useOnImport(mixed $condition = null, ?Closure $fromRaw = null) + +
  • $condition - условие выполнения метода,
  • +
  • $fromRaw - замыкание возвращающее значения из исходного.
  • +
    + namespace App\MoonShine\Resources; +use App\Enums\StatusEnum; use App\Models\Post; +use MoonShine\Fields\Enum; use MoonShine\Fields\ID; -use MoonShine\Fields\Text; use MoonShine\Resources\ModelResource; class PostResource extends ModelResource @@ -94,8 +100,9 @@ public function fields(): array ID::make() ->useOnImport(), // [tl! focus] - Text::make('Title', 'title') - ->useOnImport() // [tl! focus] + Enum::make('Status') + ->attach(StatusEnum::class) + ->useOnImport(fromRaw: static fn(string $raw, Enum $ctx) => StatusEnum::tryFrom($raw)), // [tl! focus] ]; } @@ -212,13 +219,20 @@ public function boot(): void - showOnExport(mixed $condition = null) +showOnExport(mixed $condition = null, ?Closure $modifyRawValue = null) + +
  • $condition - условие выполнения метода,
  • +
  • $modifyRawValue - замыкание для получения необработанного значения.
  • +
    + namespace App\MoonShine\Resources; +use App\Enums\StatusEnum; use App\Models\Post; +use MoonShine\Fields\Enum; use MoonShine\Fields\Text; use MoonShine\Resources\ModelResource; @@ -234,7 +248,11 @@ public function fields(): array { return [ Text::make('Title', 'title') - ->showOnExport() // [tl! focus] + ->showOnExport(), // [tl! focus] + + Enum::make('Status') + ->attach(StatusEnum::class) + ->showOnExport(modifyRawValue: static fn(StatusEnum $raw, Enum $ctx) => $raw->value), // [tl! focus] ]; }