Skip to content

Commit

Permalink
Adds mutate before and after validation
Browse files Browse the repository at this point in the history
  • Loading branch information
eighty9nine authored Aug 22, 2024
1 parent 279bfbe commit eebc3ca
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 4 deletions.
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,29 @@ You can validate the data before importing by using the `validateUsing` method.
}
```

### Mutating data before and after validation
In some cases you may want to mutate the data before or after validation, in order to achieve this, you can use `mutateBeforeValidationUsing` and `mutateAfterValidationUsing` functions methods.

```php

\EightyNine\ExcelImport\ExcelImportAction::make()
->mutateBeforeValidationUsing(function(array $data): array{
$data['date'] = Carbon::make((string) str($value)->replace('.', '-'));
return $data;
})
->validateUsing([
'name' => 'required',
'email' => 'required|email',
'phone' => ['required','numeric'],
])
->mutateAfterValidationUsing(function(array $data): array{
$data['date'] = $data['date']->format('Y-m-d');
return $data;
}),

```


## Testing

```bash
Expand Down
25 changes: 24 additions & 1 deletion src/Concerns/HasUploadForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,28 @@ trait HasUploadForm

protected ?Closure $uploadField = null;

protected ?Closure $afterValidationMutator = null;

protected ?Closure $beforeValidationMutator = null;

protected string | Closure $visibility = 'public';

protected array $validationRules = [];

protected bool $validate = false;

public function mutateBeforeValidationUsing(Closure $closure): static
{
$this->beforeValidationMutator = $closure;
return $this;
}

public function mutateAfterValidationUsing(Closure $closure): static
{
$this->afterValidationMutator = $closure;
return $this;
}

public function validateUsing(array $rules): static
{
$this->validationRules = $rules;
Expand Down Expand Up @@ -120,7 +136,14 @@ public function validationRules(): array
$rules = [];
if($this->validate) {
$rules[] = fn (): Closure => function (string $attribute, $value, Closure $fail) {
Excel::import(new ValidationImport($fail, $this->validationRules), $value);
Excel::import(
new ValidationImport(
$fail,
$this->validationRules,
$this->afterValidationMutator
),
$value
);
};
}
return $rules;
Expand Down
19 changes: 18 additions & 1 deletion src/DefaultImport.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,33 @@ public function setCollectionMethod(Closure $closure): void
$this->collectionMethod = $closure;
}

public function setAfterValidationMutator(Closure $closure): void
{
$this->afterValidationMutator = $closure;
}

public function collection(Collection $collection)
{
if(is_callable($this->collectionMethod)) {
$collection = call_user_func($this->collectionMethod, $this->model, $collection);
$collection = call_user_func(
$this->collectionMethod,
$this->model,
$collection,
$this->additionalData,
$this->afterValidationMutator
);
}else{
foreach ($collection as $row) {
$data = $row->toArray();
if(filled($this->additionalData)) {
$data = array_merge($data, $this->additionalData);
}
if($this->afterValidationMutator){
$data = call_user_func(
$this->afterValidationMutator,
$data
);
}
$this->model::create($data);
}
}
Expand Down
6 changes: 5 additions & 1 deletion src/ExcelImportAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,18 @@ private function importData(): Closure
$this->additionalData
);

if(method_exists($importObject, 'setAdditionalData')) {
if(method_exists($importObject, 'setAdditionalData') && isset($this->additionalData)) {
$importObject->setAdditionalData($this->additionalData);
}

if(method_exists($importObject, 'setCollectionMethod') && isset($this->collectionMethod)) {
$importObject->setCollectionMethod($this->collectionMethod);
}

if(method_exists($importObject, 'setAfterValidationMutator' && isset($this->afterValidationMutator))){
$importObject->setAfterValidationMutator($this->afterValidationMutator);
}

Excel::import($importObject, $data['upload']);

if (is_callable($this->afterImportClosure)) {
Expand Down
6 changes: 5 additions & 1 deletion src/ValidationImport.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class ValidationImport implements ToCollection, WithHeadingRow
public function __construct(
public Closure $fail,
public array $rules = [],
public ?Closure $beforeValidationMutator
) {
}

Expand All @@ -21,9 +22,12 @@ public function collection(Collection $collection)
foreach ($collection as $index => $row) {
$index = $index + 2;
$data = $row->toArray();
$data = isset($this->beforeValidationMutator) ?
call_user_func( $this->beforeValidationMutator, $data ) :
$data;

$validator = Validator::make($data, $this->rules);
if ($validator->fails()) {
// dd($validator->errors()->getMessages());
call_user_func($this->fail, __("excel-import::excel-import.validation_failed", [
"row" => $index,
"messages" => $this->transformErrors($validator->errors()->getMessages()),
Expand Down

0 comments on commit eebc3ca

Please sign in to comment.