Mod Contribtion validation
This commit is contained in:
parent
79d92a6567
commit
aab004f1f1
|
@ -23,7 +23,7 @@ class GenerateAction
|
|||
public function asController(GenerateRequest $request): BaseCompiler|JsonResponse
|
||||
{
|
||||
app(ContributionFactory::class)->validateType($request);
|
||||
app(ContributionFactory::class)->validatePayload($request);
|
||||
$request->validateContribution();
|
||||
|
||||
return $request->input('validate')
|
||||
? response()->json([])
|
||||
|
|
|
@ -26,8 +26,5 @@ interface HasContributionData {
|
|||
|
||||
public function country(): ?Country;
|
||||
|
||||
/**
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function payload(): array;
|
||||
public function validateContribution(): void;
|
||||
}
|
||||
|
|
|
@ -66,7 +66,3 @@ class ContributionFactory
|
|||
Validator::make(['type' => $request->type()], $this->typeRule())->validate();
|
||||
}
|
||||
|
||||
public function validatePayload(HasContributionData $request): void {
|
||||
Validator::make($request->payload(), $this->rules($request->type()))->validate();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,17 +2,10 @@
|
|||
|
||||
namespace App\Contribution\Requests;
|
||||
|
||||
use App\Contribution\Contracts\HasContributionData;
|
||||
use App\Contribution\Data\MemberData;
|
||||
use App\Contribution\Documents\ContributionDocument;
|
||||
use App\Country;
|
||||
use Lorisleiva\Actions\ActionRequest;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Support\Arr;
|
||||
use Illuminate\Support\Collection;
|
||||
use RuntimeException;
|
||||
|
||||
class GenerateApiRequest extends ActionRequest implements HasContributionData {
|
||||
class GenerateApiRequest extends GenerateRequest {
|
||||
/**
|
||||
* @return array<string, string>
|
||||
*/
|
||||
|
@ -21,47 +14,11 @@ class GenerateApiRequest extends ActionRequest implements HasContributionData {
|
|||
return $this->input();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string|array<array-key, mixed>
|
||||
*/
|
||||
public function value(string $key): string|array
|
||||
{
|
||||
if (!Arr::has($this->payload(), $key)) {
|
||||
throw new RuntimeException('Wert für '.$key.' nicht gefunden.');
|
||||
}
|
||||
return data_get($this->payload(), $key);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return class-string<ContributionDocument>
|
||||
*/
|
||||
public function type(): string
|
||||
{
|
||||
return $this->value('type');
|
||||
}
|
||||
|
||||
public function dateFrom(): Carbon {
|
||||
return Carbon::parse($this->value('dateFrom'));
|
||||
}
|
||||
|
||||
public function dateUntil(): Carbon {
|
||||
return Carbon::parse($this->value('dateUntil'));
|
||||
}
|
||||
|
||||
public function zipLocation(): string {
|
||||
return $this->value('zipLocation');
|
||||
}
|
||||
|
||||
public function eventName(): string {
|
||||
return $this->value('eventName');
|
||||
public function validateContribution(): void {
|
||||
}
|
||||
|
||||
public function members(): Collection {
|
||||
return MemberData::fromApi($this->value('member_data'));
|
||||
}
|
||||
|
||||
public function country(): ?Country {
|
||||
return Country::where('id', $this->value('country'))->first();
|
||||
return MemberData::fromApi($this->value('members'));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -3,12 +3,14 @@
|
|||
namespace App\Contribution\Requests;
|
||||
|
||||
use App\Contribution\Contracts\HasContributionData;
|
||||
use App\Contribution\ContributionFactory;
|
||||
use App\Contribution\Data\MemberData;
|
||||
use App\Contribution\Documents\ContributionDocument;
|
||||
use App\Country;
|
||||
use Lorisleiva\Actions\ActionRequest;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Support\Facades\Validator;
|
||||
|
||||
class GenerateRequest extends ActionRequest implements HasContributionData {
|
||||
/**
|
||||
|
@ -19,6 +21,10 @@ class GenerateRequest extends ActionRequest implements HasContributionData {
|
|||
return json_decode(rawurldecode(base64_decode($this->input('payload', ''))), true);
|
||||
}
|
||||
|
||||
public function validateContribution(): void {
|
||||
Validator::make($this->payload(), app(ContributionFactory::class)->rules($this->type()))->validate();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string|array<array-key, mixed>
|
||||
*/
|
||||
|
|
|
@ -89,7 +89,7 @@ it('testItCompilesContributionDocumentsViaApi', function (string $type, array $b
|
|||
'eventName' => 'Super tolles Lager',
|
||||
'type' => $type,
|
||||
'zipLocation' => '42777 SG',
|
||||
'member_data' => [
|
||||
'members' => [
|
||||
ContributionMemberApiRequestFactory::new()->create(['address' => 'Maxstr 44', 'zip' => '42719', 'firstname' => 'Max', 'lastname' => 'Muster']),
|
||||
ContributionMemberApiRequestFactory::new()->create(['address' => 'Maxstr 44', 'zip' => '42719', 'firstname' => 'Jane', 'lastname' => 'Muster']),
|
||||
],
|
||||
|
|
Loading…
Reference in New Issue