From ca24b918d1ec67d0511e4eb27c040df1fec2fca2 Mon Sep 17 00:00:00 2001 From: Simon Evans Date: Thu, 28 Sep 2023 15:11:17 +0100 Subject: [PATCH 1/4] Switch Rule to ValidationRule. --- src/Rules/CanClaim.php | 22 ++++----- src/Rules/HasBeamFlag.php | 29 ++++++----- src/Rules/IsEndDateValid.php | 44 ++++++----------- src/Rules/IsStartDateValid.php | 37 ++++---------- src/Rules/MaxTokenCount.php | 32 +++++-------- src/Rules/MaxTokenSupply.php | 48 +++++++++++-------- src/Rules/NotPaused.php | 25 +++++----- src/Rules/ScanLimit.php | 34 ++++++------- src/Rules/SingleUseCodeExist.php | 28 ++++------- src/Rules/SingleUseCodesExist.php | 13 +++-- src/Rules/TokenUploadExistInCollection.php | 18 +++---- src/Rules/TokenUploadNotExistInBeam.php | 18 +++---- src/Rules/TokenUploadNotExistInCollection.php | 18 +++---- src/Rules/TokensDoNotExistInBeam.php | 18 +++---- src/Rules/TokensDoNotExistInCollection.php | 18 +++---- src/Rules/TokensExistInBeam.php | 18 +++---- src/Rules/TokensExistInCollection.php | 18 +++---- src/Rules/VerifySignedMessage.php | 38 ++++++--------- tests/Feature/GraphQL/TestCaseGraphQL.php | 9 +++- 19 files changed, 220 insertions(+), 265 deletions(-) diff --git a/src/Rules/CanClaim.php b/src/Rules/CanClaim.php index fdefdd8..b89ae4d 100644 --- a/src/Rules/CanClaim.php +++ b/src/Rules/CanClaim.php @@ -2,14 +2,15 @@ namespace Enjin\Platform\Beam\Rules; +use Closure; use Enjin\Platform\Beam\Rules\Traits\HasDataAwareRule; use Enjin\Platform\Beam\Services\BeamService; use Illuminate\Contracts\Validation\DataAwareRule; -use Illuminate\Contracts\Validation\Rule; +use Illuminate\Contracts\Validation\ValidationRule; use Illuminate\Support\Arr; use Illuminate\Support\Facades\Cache; -class CanClaim implements DataAwareRule, Rule +class CanClaim implements DataAwareRule, ValidationRule { use HasDataAwareRule; @@ -25,25 +26,22 @@ public function __construct(protected bool $singleUse = false) * * @param string $attribute * @param mixed $value + * @param Closure $fail */ - public function passes($attribute, $value): bool + public function validate(string $attribute, mixed $value, Closure $fail): void { if (!Arr::get($this->data, 'account')) { - return true; + return; } if ($this->singleUse) { $value = explode(':', decrypt($value), 3)[1]; } - return ((int) Cache::get(BeamService::key($value), BeamService::claimsCountResolver($value))) > 0; - } + $passes = ((int) Cache::get(BeamService::key($value), BeamService::claimsCountResolver($value))) > 0; - /** - * Get the validation error message. - */ - public function message(): string - { - return __('enjin-platform-beam::validation.can_claim'); + if (!$passes) { + $fail(__('enjin-platform-beam::validation.can_claim')); + } } } diff --git a/src/Rules/HasBeamFlag.php b/src/Rules/HasBeamFlag.php index 9a0ea22..53bbf1b 100644 --- a/src/Rules/HasBeamFlag.php +++ b/src/Rules/HasBeamFlag.php @@ -2,11 +2,12 @@ namespace Enjin\Platform\Beam\Rules; +use Closure; use Enjin\Platform\Beam\Enums\BeamFlag; use Enjin\Platform\Beam\Models\Beam; -use Illuminate\Contracts\Validation\Rule; +use Illuminate\Contracts\Validation\ValidationRule; -class HasBeamFlag implements Rule +class HasBeamFlag implements ValidationRule { /** * Create new rule instance. @@ -17,21 +18,19 @@ public function __construct(protected BeamFlag $flag) /** * Determine if the validation rule passes. + * + * @param string $attribute + * @param mixed $value + * @param Closure $fail + * + * @return void */ - public function passes($attribute, $value) + public function validate(string $attribute, mixed $value, Closure $fail): void { - if (!$beam = Beam::whereCode($value)->first()) { - return false; - } - - return $beam->hasFlag($this->flag); - } + $beam = Beam::whereCode($value)->first(); - /** - * Get the validation error message. - */ - public function message() - { - return __('enjin-platform-beam::validation.has_beam_flag'); + if (!$beam || !$beam->hasFlag($this->flag)) { + $fail(__('enjin-platform-beam::validation.has_beam_flag')); + } } } diff --git a/src/Rules/IsEndDateValid.php b/src/Rules/IsEndDateValid.php index 2dd41e9..ed910c9 100644 --- a/src/Rules/IsEndDateValid.php +++ b/src/Rules/IsEndDateValid.php @@ -3,57 +3,41 @@ namespace Enjin\Platform\Beam\Rules; use Carbon\Carbon; +use Closure; use Enjin\Platform\Beam\Rules\Traits\HasDataAwareRule; use Enjin\Platform\Beam\Services\BeamService; use Illuminate\Contracts\Validation\DataAwareRule; -use Illuminate\Contracts\Validation\Rule; +use Illuminate\Contracts\Validation\ValidationRule; use Illuminate\Support\Arr; -class IsEndDateValid implements DataAwareRule, Rule +class IsEndDateValid implements DataAwareRule, ValidationRule { use HasDataAwareRule; - /** - * The error message. - */ - protected string $message; - /** * Determine if the validation rule passes. * * @param string $attribute * @param mixed $value + * @param Closure $fail * - * @return bool + * @return void */ - public function passes($attribute, $value) + public function validate(string $attribute, mixed $value, Closure $fail): void { $date = Carbon::parse($value); - if ($start = Arr::get($this->data, 'start')) { - if ($date->lte(Carbon::parse($start))) { - $this->message = __('enjin-platform-beam::validation.end_date_after_start'); + $start = Arr::get($this->data, 'start'); + if ($start && $date->lte(Carbon::parse($start))) { + $fail(__('enjin-platform-beam::validation.end_date_after_start')); - return false; - } - } elseif ($beam = resolve(BeamService::class)->findByCode($this->data['code'])) { + return; + } + + if ($beam = resolve(BeamService::class)->findByCode($this->data['code'])) { $startDate = Carbon::parse($beam->start); if ($date->lte($startDate)) { - $this->message = __('enjin-platform-beam::validation.end_date_greater_than', ['value' => $startDate->toDateTimeString()]); - - return false; + $fail(__('enjin-platform-beam::validation.end_date_greater_than', ['value' => $startDate->toDateTimeString()])); } } - - return true; - } - - /** - * Get the validation error message. - * - * @return string - */ - public function message() - { - return $this->message; } } diff --git a/src/Rules/IsStartDateValid.php b/src/Rules/IsStartDateValid.php index 7267c16..8c6c62d 100644 --- a/src/Rules/IsStartDateValid.php +++ b/src/Rules/IsStartDateValid.php @@ -3,63 +3,46 @@ namespace Enjin\Platform\Beam\Rules; use Carbon\Carbon; +use Closure; use Enjin\Platform\Beam\Rules\Traits\HasDataAwareRule; use Enjin\Platform\Beam\Services\BeamService; use Illuminate\Contracts\Validation\DataAwareRule; -use Illuminate\Contracts\Validation\Rule; +use Illuminate\Contracts\Validation\ValidationRule; use Illuminate\Support\Arr; -class IsStartDateValid implements DataAwareRule, Rule +class IsStartDateValid implements DataAwareRule, ValidationRule { use HasDataAwareRule; - /** - * The error message. - */ - protected string $message; - /** * Determine if the validation rule passes. * * @param string $attribute * @param mixed $value + * @param Closure $fail * * @return bool */ - public function passes($attribute, $value) + public function validate(string $attribute, mixed $value, Closure $fail): void { if ($end = Arr::get($this->data, 'end')) { if (Carbon::parse($value)->gte(Carbon::parse($end))) { - $this->message = __('enjin-platform-beam::validation.start_date_after_end'); + $fail(__('enjin-platform-beam::validation.start_date_after_end')); - return false; + return; } } if ($beam = resolve(BeamService::class)->findByCode($this->data['code'])) { if (Carbon::parse($beam->start)->isPast()) { - $this->message = __('enjin-platform-beam::validation.start_date_has_passed'); + $fail(__('enjin-platform-beam::validation.start_date_has_passed')); - return false; + return; } $endDate = Carbon::parse($beam->end); if (Carbon::parse($value)->gte($endDate)) { - $this->message = __('enjin-platform-beam::validation.start_date_less_than', ['value' => $endDate->toDateTimeString()]); - - return false; + $fail(__('enjin-platform-beam::validation.start_date_less_than', ['value' => $endDate->toDateTimeString()])); } } - - return true; - } - - /** - * Get the validation error message. - * - * @return string - */ - public function message() - { - return $this->message; } } diff --git a/src/Rules/MaxTokenCount.php b/src/Rules/MaxTokenCount.php index 2f12158..180b0f6 100644 --- a/src/Rules/MaxTokenCount.php +++ b/src/Rules/MaxTokenCount.php @@ -2,16 +2,17 @@ namespace Enjin\Platform\Beam\Rules; +use Closure; use Enjin\Platform\Beam\Enums\BeamType; use Enjin\Platform\Beam\Models\BeamClaim; use Enjin\Platform\Beam\Rules\Traits\HasDataAwareRule; use Enjin\Platform\Beam\Rules\Traits\IntegerRange; use Enjin\Platform\Models\Collection; use Illuminate\Contracts\Validation\DataAwareRule; -use Illuminate\Contracts\Validation\Rule; +use Illuminate\Contracts\Validation\ValidationRule; use Illuminate\Support\Arr; -class MaxTokenCount implements DataAwareRule, Rule +class MaxTokenCount implements DataAwareRule, ValidationRule { use HasDataAwareRule; use IntegerRange; @@ -32,14 +33,15 @@ public function __construct(protected ?string $collectionId) * * @param string $attribute * @param mixed $value + * @param Closure $fail * - * @return bool + * @return void */ - public function passes($attribute, $value) + public function validate(string $attribute, mixed $value, Closure $fail): void { if ($this->collectionId && ($collection = Collection::withCount('tokens')->firstWhere(['collection_chain_id'=> $this->collectionId]))) { if (!is_null($this->limit = $collection->max_token_count)) { - return $collection->max_token_count >= $collection->tokens_count + $passes = $collection->max_token_count >= $collection->tokens_count + collect($this->data['tokens']) ->filter(fn ($token) => BeamType::getEnumCase($token['type']) == BeamType::MINT_ON_DEMAND) ->reduce(function ($carry, $token) { @@ -48,8 +50,8 @@ public function passes($attribute, $value) return $val + ( $range === false - ? $token['claimQuantity'] - : (($range[1] - $range[0]) + 1) * $token['claimQuantity'] + ? $token['claimQuantity'] + : (($range[1] - $range[0]) + 1) * $token['claimQuantity'] ); }, $carry); }, 0) @@ -58,19 +60,11 @@ public function passes($attribute, $value) fn ($query) => $query->where('collection_chain_id', $this->collectionId)->where('end', '>', now()) )->where('type', BeamType::MINT_ON_DEMAND->name) ->count(); + + if (!$passes) { + $fail(__('enjin-platform-beam::validation.max_token_count', ['limit' => $this->limit])); + } } } - - return true; - } - - /** - * Get the validation error message. - * - * @return string - */ - public function message() - { - return __('enjin-platform-beam::validation.max_token_count', ['limit' => $this->limit]); } } diff --git a/src/Rules/MaxTokenSupply.php b/src/Rules/MaxTokenSupply.php index e1badaf..2c3d395 100644 --- a/src/Rules/MaxTokenSupply.php +++ b/src/Rules/MaxTokenSupply.php @@ -2,6 +2,7 @@ namespace Enjin\Platform\Beam\Rules; +use Closure; use Enjin\Platform\Beam\Enums\BeamType; use Enjin\Platform\Beam\Models\BeamClaim; use Enjin\Platform\Beam\Rules\Traits\HasDataAwareRule; @@ -11,10 +12,10 @@ use Enjin\Platform\Models\Wallet; use Enjin\Platform\Support\Account; use Illuminate\Contracts\Validation\DataAwareRule; -use Illuminate\Contracts\Validation\Rule; +use Illuminate\Contracts\Validation\ValidationRule; use Illuminate\Support\Arr; -class MaxTokenSupply implements DataAwareRule, Rule +class MaxTokenSupply implements DataAwareRule, ValidationRule { use HasDataAwareRule; use IntegerRange; @@ -38,22 +39,37 @@ public function __construct(protected ?string $collectionId) { } + /** + * Get the validation error message. + * + * @return string + */ + public function message() + { + return __($this->error, ['limit' => $this->limit]); + } + /** * Determine if the validation rule passes. * * @param string $attribute * @param mixed $value + * @param Closure $fail * - * @return bool + * @return void */ - public function passes($attribute, $value) + public function validate(string $attribute, mixed $value, Closure $fail): void { if ($this->collectionId && ($collection = Collection::firstWhere(['collection_chain_id' => $this->collectionId])) && !is_null($this->limit = $collection->max_token_supply) ) { if (Arr::get($this->data, str_replace('tokenQuantityPerClaim', 'type', $attribute)) == BeamType::MINT_ON_DEMAND->name) { - return $collection->max_token_supply >= $value; + if (!$collection->max_token_supply >= $value) { + $fail($this->message()); + + return; + } } $tokenIds = Arr::get($this->data, str_replace('tokenQuantityPerClaim', 'tokenIds', $attribute)); @@ -62,7 +78,7 @@ public function passes($attribute, $value) $wallet = Wallet::firstWhere(['public_key' => Account::daemonPublicKey()]); $collection = Collection::firstWhere(['collection_chain_id' => $this->collectionId]); if (!$wallet || !$collection) { - return false; + $fail($this->message()); } $accounts = TokenAccount::join('tokens', 'tokens.id', '=', 'token_accounts.token_id') ->where('token_accounts.wallet_id', $wallet->id) @@ -85,7 +101,9 @@ public function passes($attribute, $value) if ((int) $account->balance < $value + Arr::get($claims, $account->token_chain_id, 0)) { $this->error = 'enjin-platform::validation.max_token_balance'; - return false; + $fail($this->message()); + + return; } } } @@ -95,7 +113,7 @@ public function passes($attribute, $value) $wallet = Wallet::firstWhere(['public_key' => Account::daemonPublicKey()]); $collection = Collection::firstWhere(['collection_chain_id' => $this->collectionId]); if (!$wallet || !$collection) { - return false; + $fail($this->message()); } foreach ($ranges as $range) { [$from, $to] = $this->integerRange($range); @@ -120,23 +138,11 @@ public function passes($attribute, $value) if ((int) $account->balance < $value + Arr::get($claims, $account->token_chain_id, 0)) { $this->error = 'enjin-platform::validation.max_token_balance'; - return false; + $fail($this->message()); } } } } } - - return true; - } - - /** - * Get the validation error message. - * - * @return string - */ - public function message() - { - return __($this->error, ['limit' => $this->limit]); } } diff --git a/src/Rules/NotPaused.php b/src/Rules/NotPaused.php index 022c961..92dc77b 100644 --- a/src/Rules/NotPaused.php +++ b/src/Rules/NotPaused.php @@ -2,11 +2,12 @@ namespace Enjin\Platform\Beam\Rules; +use Closure; use Enjin\Platform\Beam\Enums\BeamFlag; use Enjin\Platform\Beam\Services\BeamService; -use Illuminate\Contracts\Validation\Rule; +use Illuminate\Contracts\Validation\ValidationRule; -class NotPaused implements Rule +class NotPaused implements ValidationRule { public function __construct(protected ?string $code = null) { @@ -14,23 +15,19 @@ public function __construct(protected ?string $code = null) /** * Determine if the validation rule passes. + * + * @param string $attribute + * @param mixed $value + * @param Closure $fail + * + * @return void */ - public function passes(mixed $attribute, mixed $value) + public function validate(string $attribute, mixed $value, Closure $fail): void { if ($beam = resolve(BeamService::class)->findByCode($this->code ?: $value)) { if ($beam->hasFlag(BeamFlag::PAUSED)) { - return false; + $fail(__('enjin-platform-beam::validation.is_paused')); } } - - return true; - } - - /** - * Get the validation error message. - */ - public function message(): array|string - { - return __('enjin-platform-beam::validation.is_paused'); } } diff --git a/src/Rules/ScanLimit.php b/src/Rules/ScanLimit.php index 7cf55bd..2282353 100644 --- a/src/Rules/ScanLimit.php +++ b/src/Rules/ScanLimit.php @@ -2,12 +2,13 @@ namespace Enjin\Platform\Beam\Rules; +use Closure; use Enjin\Platform\Beam\Models\BeamScan; use Enjin\Platform\Beam\Rules\Traits\HasDataAwareRule; use Illuminate\Contracts\Validation\DataAwareRule; -use Illuminate\Contracts\Validation\Rule; +use Illuminate\Contracts\Validation\ValidationRule; -class ScanLimit implements DataAwareRule, Rule +class ScanLimit implements DataAwareRule, ValidationRule { use HasDataAwareRule; @@ -16,27 +17,20 @@ class ScanLimit implements DataAwareRule, Rule * * @param string $attribute * @param mixed $value + * @param Closure $fail * - * @return bool + * @return void */ - public function passes($attribute, $value) + public function validate(string $attribute, mixed $value, Closure $fail): void { - if (!$limit = config('enjin-platform-beam.scan_limit')) { - return true; - } - - return $limit > (int) BeamScan::whereWalletPublicKey($value) - ->hasCode($this->data['code']) - ->first()?->count; - } + $limit = config('enjin-platform-beam.scan_limit'); - /** - * Get the validation error message. - * - * @return string - */ - public function message() - { - return __('enjin-platform-beam::validation.scan_limit'); + if ($limit && + !($limit > (int) BeamScan::whereWalletPublicKey($value) + ->hasCode($this->data['code']) + ->first()?->count) + ) { + $fail(__('enjin-platform-beam::validation.scan_limit')); + } } } diff --git a/src/Rules/SingleUseCodeExist.php b/src/Rules/SingleUseCodeExist.php index 4b6ae44..6c98f5b 100644 --- a/src/Rules/SingleUseCodeExist.php +++ b/src/Rules/SingleUseCodeExist.php @@ -2,38 +2,28 @@ namespace Enjin\Platform\Beam\Rules; +use Closure; use Enjin\Platform\Beam\Models\BeamClaim; use Enjin\Platform\Beam\Services\BeamService; -use Illuminate\Contracts\Validation\Rule; +use Illuminate\Contracts\Validation\ValidationRule; -class SingleUseCodeExist implements Rule +class SingleUseCodeExist implements ValidationRule { /** * Determine if the validation rule passes. * * @param string $attribute * @param mixed $value + * @param Closure $fail * - * @return bool + * @return void */ - public function passes($attribute, $value) + public function validate(string $attribute, mixed $value, Closure $fail): void { - if (BeamService::isSingleUse($value)) { - return BeamClaim::withSingleUseCode($value) - ->claimable() - ->first(); + if (BeamService::isSingleUse($value) && null !== BeamClaim::withSingleUseCode($value)->claimable()->first()) { + return; } - return false; - } - - /** - * Get the validation error message. - * - * @return string - */ - public function message() - { - return __('enjin-platform-beam::validation.verify_signed_message'); + $fail(__('enjin-platform-beam::validation.verify_signed_message')); } } diff --git a/src/Rules/SingleUseCodesExist.php b/src/Rules/SingleUseCodesExist.php index afa4400..20b2adc 100644 --- a/src/Rules/SingleUseCodesExist.php +++ b/src/Rules/SingleUseCodesExist.php @@ -2,6 +2,8 @@ namespace Enjin\Platform\Beam\Rules; +use Closure; + class SingleUseCodesExist extends SingleUseCodeExist { /** @@ -9,17 +11,14 @@ class SingleUseCodesExist extends SingleUseCodeExist * * @param string $attribute * @param mixed $value + * @param Closure $fail * - * @return bool + * @return void */ - public function passes($attribute, $value) + public function validate(string $attribute, mixed $value, Closure $fail): void { foreach ($value as $code) { - if (!parent::passes($attribute, $code)) { - return false; - } + parent::validate($attribute, $code, $fail); } - - return true; } } diff --git a/src/Rules/TokenUploadExistInCollection.php b/src/Rules/TokenUploadExistInCollection.php index 428c92c..f9c2cc9 100644 --- a/src/Rules/TokenUploadExistInCollection.php +++ b/src/Rules/TokenUploadExistInCollection.php @@ -2,12 +2,13 @@ namespace Enjin\Platform\Beam\Rules; +use Closure; use Enjin\Platform\Beam\Rules\Traits\IntegerRange; use Enjin\Platform\Models\Token; -use Illuminate\Contracts\Validation\Rule; +use Illuminate\Contracts\Validation\ValidationRule; use Illuminate\Support\LazyCollection; -class TokenUploadExistInCollection implements Rule +class TokenUploadExistInCollection implements ValidationRule { use IntegerRange; @@ -26,10 +27,11 @@ public function __construct(protected ?string $collectionId) * * @param string $attribute * @param mixed $value + * @param Closure $fail * - * @return bool + * @return void */ - public function passes($attribute, $value) + public function validate(string $attribute, mixed $value, Closure $fail): void { if ($this->collectionId) { $ids = collect(); @@ -51,7 +53,9 @@ public function passes($attribute, $value) ->whereHas('collection', fn ($query) => $query->where('collection_chain_id', $this->collectionId)) ->count(); if ($count !== count($integers)) { - return false; + $fail($this->message()); + + return; } } @@ -62,13 +66,11 @@ public function passes($attribute, $value) ->whereHas('collection', fn ($query) => $query->where('collection_chain_id', $this->collectionId)) ->count(); if ($count !== ($to - $from) + 1) { - return false; + $fail($this->message()); } } } } - - return true; } /** diff --git a/src/Rules/TokenUploadNotExistInBeam.php b/src/Rules/TokenUploadNotExistInBeam.php index bd53c31..37d63a6 100644 --- a/src/Rules/TokenUploadNotExistInBeam.php +++ b/src/Rules/TokenUploadNotExistInBeam.php @@ -2,15 +2,16 @@ namespace Enjin\Platform\Beam\Rules; +use Closure; use Enjin\Platform\Beam\Rules\Traits\HasDataAwareRule; use Enjin\Platform\Beam\Rules\Traits\IntegerRange; use Illuminate\Contracts\Validation\DataAwareRule; -use Illuminate\Contracts\Validation\Rule; +use Illuminate\Contracts\Validation\ValidationRule; use Illuminate\Database\Eloquent\Model; use Illuminate\Support\Arr; use Illuminate\Support\LazyCollection; -class TokenUploadNotExistInBeam implements DataAwareRule, Rule +class TokenUploadNotExistInBeam implements DataAwareRule, ValidationRule { use IntegerRange; use HasDataAwareRule; @@ -24,10 +25,11 @@ public function __construct(protected ?Model $beam = null) * * @param string $attribute * @param mixed $value + * @param Closure $fail * - * @return bool + * @return void */ - public function passes($attribute, $value) + public function validate(string $attribute, mixed $value, Closure $fail): void { $ids = collect(); $tokens = LazyCollection::make(function () use ($value, $ids) { @@ -46,19 +48,19 @@ public function passes($attribute, $value) $integers = collect($tokenIds)->filter(fn ($val) => false === $this->integerRange($val))->all(); if ($integers) { if ($prepare->whereIn('beam_claims.token_chain_id', $integers)->exists()) { - return false; + $fail($this->message()); + + return; } } $ranges = collect($tokenIds)->filter(fn ($val) => false !== $this->integerRange($val))->all(); foreach ($ranges as $range) { [$from, $to] = $this->integerRange($range); if ($prepare->whereBetween('beam_claims.token_chain_id', [(int) $from, (int) $to])->exists()) { - return false; + $fail($this->message()); } } } - - return true; } /** diff --git a/src/Rules/TokenUploadNotExistInCollection.php b/src/Rules/TokenUploadNotExistInCollection.php index 1cfae72..3f94388 100644 --- a/src/Rules/TokenUploadNotExistInCollection.php +++ b/src/Rules/TokenUploadNotExistInCollection.php @@ -2,12 +2,13 @@ namespace Enjin\Platform\Beam\Rules; +use Closure; use Enjin\Platform\Beam\Rules\Traits\IntegerRange; use Enjin\Platform\Models\Token; -use Illuminate\Contracts\Validation\Rule; +use Illuminate\Contracts\Validation\ValidationRule; use Illuminate\Support\LazyCollection; -class TokenUploadNotExistInCollection implements Rule +class TokenUploadNotExistInCollection implements ValidationRule { use IntegerRange; @@ -20,10 +21,11 @@ public function __construct(protected ?string $collectionId) * * @param string $attribute * @param mixed $value + * @param Closure $fail * - * @return bool + * @return void */ - public function passes($attribute, $value) + public function validate(string $attribute, mixed $value, Closure $fail): void { if ($this->collectionId && $value) { $ids = collect(); @@ -45,7 +47,9 @@ public function passes($attribute, $value) ->whereHas('collection', fn ($query) => $query->where('collection_chain_id', $this->collectionId)) ->exists(); if ($exists) { - return false; + $fail($this->message()); + + return; } } @@ -56,13 +60,11 @@ public function passes($attribute, $value) ->whereHas('collection', fn ($query) => $query->where('collection_chain_id', $this->collectionId)) ->exists(); if ($exists) { - return false; + $fail($this->message()); } } } } - - return true; } /** diff --git a/src/Rules/TokensDoNotExistInBeam.php b/src/Rules/TokensDoNotExistInBeam.php index 194e75e..1ad12bd 100644 --- a/src/Rules/TokensDoNotExistInBeam.php +++ b/src/Rules/TokensDoNotExistInBeam.php @@ -2,17 +2,18 @@ namespace Enjin\Platform\Beam\Rules; +use Closure; use Enjin\Platform\Beam\Models\BeamClaim; use Enjin\Platform\Beam\Rules\Traits\HasDataAwareRule; use Enjin\Platform\Beam\Rules\Traits\IntegerRange; use Enjin\Platform\Enums\Substrate\TokenMintCapType; use Illuminate\Contracts\Validation\DataAwareRule; -use Illuminate\Contracts\Validation\Rule; +use Illuminate\Contracts\Validation\ValidationRule; use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Model; use Illuminate\Support\Arr; -class TokensDoNotExistInBeam implements DataAwareRule, Rule +class TokensDoNotExistInBeam implements DataAwareRule, ValidationRule { use IntegerRange; use HasDataAwareRule; @@ -26,27 +27,28 @@ public function __construct(protected ?Model $beam = null) * * @param string $attribute * @param mixed $value + * @param Closure $fail * - * @return bool + * @return void */ - public function passes($attribute, $value) + public function validate(string $attribute, mixed $value, Closure $fail): void { $prepare = static::prepareStatement($this->beam, Arr::get($this->data, 'collectionId')); $integers = collect($value)->filter(fn ($val) => false === $this->integerRange($val))->all(); if ($integers) { if ($prepare->whereIn('beam_claims.token_chain_id', $integers)->exists()) { - return false; + $fail($this->message()); + + return; } } $ranges = collect($value)->filter(fn ($val) => false !== $this->integerRange($val))->all(); foreach ($ranges as $range) { [$from, $to] = $this->integerRange($range); if ($prepare->whereBetween('beam_claims.token_chain_id', [(int) $from, (int) $to])->exists()) { - return false; + $fail($this->message()); } } - - return true; } /** diff --git a/src/Rules/TokensDoNotExistInCollection.php b/src/Rules/TokensDoNotExistInCollection.php index 5c0f733..7dbfd05 100644 --- a/src/Rules/TokensDoNotExistInCollection.php +++ b/src/Rules/TokensDoNotExistInCollection.php @@ -2,11 +2,12 @@ namespace Enjin\Platform\Beam\Rules; +use Closure; use Enjin\Platform\Beam\Rules\Traits\IntegerRange; use Enjin\Platform\Models\Token; -use Illuminate\Contracts\Validation\Rule; +use Illuminate\Contracts\Validation\ValidationRule; -class TokensDoNotExistInCollection implements Rule +class TokensDoNotExistInCollection implements ValidationRule { use IntegerRange; @@ -19,10 +20,11 @@ public function __construct(protected ?string $collectionId) * * @param string $attribute * @param mixed $value + * @param Closure $fail * - * @return bool + * @return void */ - public function passes($attribute, $value) + public function validate(string $attribute, mixed $value, Closure $fail): void { if ($this->collectionId) { $integers = collect($value)->filter(fn ($val) => false === $this->integerRange($val))->all(); @@ -31,7 +33,9 @@ public function passes($attribute, $value) ->whereHas('collection', fn ($query) => $query->where('collection_chain_id', $this->collectionId)) ->exists(); if ($exists) { - return false; + $fail($this->message()); + + return; } } $ranges = collect($value)->filter(fn ($val) => false !== $this->integerRange($val))->all(); @@ -42,12 +46,10 @@ public function passes($attribute, $value) ->exists(); if ($exists) { - return false; + $fail($this->message()); } } } - - return true; } /** diff --git a/src/Rules/TokensExistInBeam.php b/src/Rules/TokensExistInBeam.php index 97f6426..26add13 100644 --- a/src/Rules/TokensExistInBeam.php +++ b/src/Rules/TokensExistInBeam.php @@ -2,13 +2,14 @@ namespace Enjin\Platform\Beam\Rules; +use Closure; use Enjin\Platform\Beam\Models\BeamClaim; use Enjin\Platform\Beam\Rules\Traits\HasDataAwareRule; use Enjin\Platform\Beam\Rules\Traits\IntegerRange; use Illuminate\Contracts\Validation\DataAwareRule; -use Illuminate\Contracts\Validation\Rule; +use Illuminate\Contracts\Validation\ValidationRule; -class TokensExistInBeam implements DataAwareRule, Rule +class TokensExistInBeam implements DataAwareRule, ValidationRule { use IntegerRange; use HasDataAwareRule; @@ -18,10 +19,11 @@ class TokensExistInBeam implements DataAwareRule, Rule * * @param string $attribute * @param mixed $value + * @param Closure $fail * - * @return bool + * @return void */ - public function passes($attribute, $value) + public function validate(string $attribute, mixed $value, Closure $fail): void { if ($code = $this->data['code']) { $integers = collect($value)->filter(fn ($val) => false === $this->integerRange($val))->all(); @@ -32,7 +34,9 @@ public function passes($attribute, $value) ->whereHas('beam', fn ($query) => $query->where('code', $code)) ->count(); if ($count != count($integers)) { - return false; + $fail($this->message()); + + return; } } $ranges = collect($value)->filter(fn ($val) => false !== $this->integerRange($val))->all(); @@ -44,12 +48,10 @@ public function passes($attribute, $value) ->whereHas('beam', fn ($query) => $query->where('code', $code)) ->count(); if ($count !== ($to - $from) + 1) { - return false; + $fail($this->message()); } } } - - return true; } /** diff --git a/src/Rules/TokensExistInCollection.php b/src/Rules/TokensExistInCollection.php index 0bcb53a..4eaf8e2 100644 --- a/src/Rules/TokensExistInCollection.php +++ b/src/Rules/TokensExistInCollection.php @@ -2,11 +2,12 @@ namespace Enjin\Platform\Beam\Rules; +use Closure; use Enjin\Platform\Beam\Rules\Traits\IntegerRange; use Enjin\Platform\Models\Token; -use Illuminate\Contracts\Validation\Rule; +use Illuminate\Contracts\Validation\ValidationRule; -class TokensExistInCollection implements Rule +class TokensExistInCollection implements ValidationRule { use IntegerRange; @@ -19,10 +20,11 @@ public function __construct(protected ?string $collectionId) * * @param string $attribute * @param mixed $value + * @param Closure $fail * - * @return bool + * @return void */ - public function passes($attribute, $value) + public function validate(string $attribute, mixed $value, Closure $fail): void { if ($this->collectionId) { $integers = collect($value)->filter(fn ($val) => false === $this->integerRange($val))->all(); @@ -31,7 +33,9 @@ public function passes($attribute, $value) ->whereHas('collection', fn ($query) => $query->where('collection_chain_id', $this->collectionId)) ->count(); if ($count !== count($integers)) { - return false; + $fail($this->message()); + + return; } } $ranges = collect($value)->filter(fn ($val) => false !== $this->integerRange($val))->all(); @@ -41,12 +45,10 @@ public function passes($attribute, $value) ->whereHas('collection', fn ($query) => $query->where('collection_chain_id', $this->collectionId)) ->count(); if ($count !== ($to - $from) + 1) { - return false; + $fail($this->message()); } } } - - return true; } /** diff --git a/src/Rules/VerifySignedMessage.php b/src/Rules/VerifySignedMessage.php index 8f4d953..1909f12 100644 --- a/src/Rules/VerifySignedMessage.php +++ b/src/Rules/VerifySignedMessage.php @@ -2,6 +2,7 @@ namespace Enjin\Platform\Beam\Rules; +use Closure; use Enjin\BlockchainTools\HexConverter; use Enjin\Platform\Beam\Models\BeamScan; use Enjin\Platform\Beam\Rules\Traits\HasDataAwareRule; @@ -10,31 +11,27 @@ use Enjin\Platform\Services\Blockchain\Interfaces\BlockchainServiceInterface; use Enjin\Platform\Support\SS58Address; use Illuminate\Contracts\Validation\DataAwareRule; -use Illuminate\Contracts\Validation\Rule; +use Illuminate\Contracts\Validation\ValidationRule; -class VerifySignedMessage implements DataAwareRule, Rule +class VerifySignedMessage implements DataAwareRule, ValidationRule { use HasDataAwareRule; - /** - * The error message. - */ - protected string $message; - /** * Determine if the validation rule passes. * * @param string $attribute * @param mixed $value + * @param Closure $fail * - * @return bool + * @return void */ - public function passes($attribute, $value) + public function validate(string $attribute, mixed $value, Closure $fail): void { if (!$publicKey = SS58Address::getPublicKey($this->data['account'])) { - $this->message = __('enjin-platform::validation.valid_substrate_account', ['attribute' => 'account']); + $fail(__('enjin-platform::validation.valid_substrate_account', ['attribute' => 'account'])); - return false; + return; } if (BeamService::isSingleUse($this->data['code'])) { @@ -42,33 +39,26 @@ public function passes($attribute, $value) } if (!$scan = BeamScan::hasCode($this->data['code'])->firstWhere(['wallet_public_key' => $publicKey])) { - $this->message = __('enjin-platform-beam::validation.beam_scan_not_found'); + $fail(__('enjin-platform-beam::validation.beam_scan_not_found')); - return false; + return; } - $this->message = __('enjin-platform-beam::validation.verify_signed_message'); $type = $this->data['cryptoSignatureType'] ?? CryptoSignatureType::SR25519->name; $message = $scan->message; if ($type == CryptoSignatureType::ED25519->name) { $message = HexConverter::stringToHex($message); } - return resolve(BlockchainServiceInterface::class)->verifyMessage( + $passes = resolve(BlockchainServiceInterface::class)->verifyMessage( $message, $value, $scan->wallet_public_key, $type ); - } - /** - * Get the validation error message. - * - * @return string - */ - public function message() - { - return $this->message; + if (!$passes) { + $fail(__('enjin-platform-beam::validation.verify_signed_message')); + } } } diff --git a/tests/Feature/GraphQL/TestCaseGraphQL.php b/tests/Feature/GraphQL/TestCaseGraphQL.php index 4757589..4be6d82 100644 --- a/tests/Feature/GraphQL/TestCaseGraphQL.php +++ b/tests/Feature/GraphQL/TestCaseGraphQL.php @@ -67,9 +67,16 @@ public function graphql(string $query, array $arguments = [], ?bool $expectError $appendErrors = "\n\n" . $this->formatSafeTrace($data['errors'][0]['trace']); } + $validationMessages = collect($result->errors)->map(function ($error) { + if ('validation' == $error->getMessage()) { + return $error->getPrevious()->getValidator()->getMessageBag()->getMessages(); + } + })->all(); + $assertMessage = "Probably unexpected error in GraphQL response:\n" . var_export($data, true) - . $appendErrors; + . $appendErrors . "\n\n" + . "Validation messages:\n" . json_encode($validationMessages); } unset($data['errors'][0]['trace']); From ef2cc582412439b47509d2c796d4201338f56d44 Mon Sep 17 00:00:00 2001 From: Simon Evans Date: Fri, 29 Sep 2023 11:45:43 +0100 Subject: [PATCH 2/4] Feedback. --- src/Rules/CanClaim.php | 6 ++- src/Rules/HasBeamFlag.php | 4 +- src/Rules/IsEndDateValid.php | 7 ++- src/Rules/IsStartDateValid.php | 9 ++-- src/Rules/MaxTokenCount.php | 7 ++- src/Rules/MaxTokenSupply.php | 44 +++++++++---------- src/Rules/NotExpired.php | 12 +++-- src/Rules/NotOwner.php | 10 +++-- src/Rules/NotPaused.php | 4 +- src/Rules/PassesClaimConditions.php | 15 ++++--- src/Rules/ScanLimit.php | 4 +- src/Rules/SingleUseCodeExist.php | 6 +-- src/Rules/SingleUseCodesExist.php | 2 +- src/Rules/TokenUploadExistInCollection.php | 8 ++-- src/Rules/TokenUploadNotExistInBeam.php | 8 ++-- src/Rules/TokenUploadNotExistInCollection.php | 8 ++-- src/Rules/TokensDoNotExistInBeam.php | 8 ++-- src/Rules/TokensDoNotExistInCollection.php | 8 ++-- src/Rules/TokensExistInBeam.php | 8 ++-- src/Rules/TokensExistInCollection.php | 8 ++-- src/Rules/UniqueTokenIds.php | 11 ++++- src/Rules/VerifySignedMessage.php | 11 +++-- 22 files changed, 120 insertions(+), 88 deletions(-) diff --git a/src/Rules/CanClaim.php b/src/Rules/CanClaim.php index b89ae4d..c88e83f 100644 --- a/src/Rules/CanClaim.php +++ b/src/Rules/CanClaim.php @@ -26,7 +26,9 @@ public function __construct(protected bool $singleUse = false) * * @param string $attribute * @param mixed $value - * @param Closure $fail + * @param Closure(string): \Illuminate\Translation\PotentiallyTranslatedString $fail + * + * @return void */ public function validate(string $attribute, mixed $value, Closure $fail): void { @@ -41,7 +43,7 @@ public function validate(string $attribute, mixed $value, Closure $fail): void $passes = ((int) Cache::get(BeamService::key($value), BeamService::claimsCountResolver($value))) > 0; if (!$passes) { - $fail(__('enjin-platform-beam::validation.can_claim')); + $fail('enjin-platform-beam::validation.can_claim')->translate(); } } } diff --git a/src/Rules/HasBeamFlag.php b/src/Rules/HasBeamFlag.php index 53bbf1b..c98d0a6 100644 --- a/src/Rules/HasBeamFlag.php +++ b/src/Rules/HasBeamFlag.php @@ -21,7 +21,7 @@ public function __construct(protected BeamFlag $flag) * * @param string $attribute * @param mixed $value - * @param Closure $fail + * @param Closure(string): \Illuminate\Translation\PotentiallyTranslatedString $fail * * @return void */ @@ -30,7 +30,7 @@ public function validate(string $attribute, mixed $value, Closure $fail): void $beam = Beam::whereCode($value)->first(); if (!$beam || !$beam->hasFlag($this->flag)) { - $fail(__('enjin-platform-beam::validation.has_beam_flag')); + $fail('enjin-platform-beam::validation.has_beam_flag')->translate(); } } } diff --git a/src/Rules/IsEndDateValid.php b/src/Rules/IsEndDateValid.php index ed910c9..5a39d30 100644 --- a/src/Rules/IsEndDateValid.php +++ b/src/Rules/IsEndDateValid.php @@ -19,7 +19,7 @@ class IsEndDateValid implements DataAwareRule, ValidationRule * * @param string $attribute * @param mixed $value - * @param Closure $fail + * @param Closure(string): \Illuminate\Translation\PotentiallyTranslatedString $fail * * @return void */ @@ -36,7 +36,10 @@ public function validate(string $attribute, mixed $value, Closure $fail): void if ($beam = resolve(BeamService::class)->findByCode($this->data['code'])) { $startDate = Carbon::parse($beam->start); if ($date->lte($startDate)) { - $fail(__('enjin-platform-beam::validation.end_date_greater_than', ['value' => $startDate->toDateTimeString()])); + $fail('enjin-platform-beam::validation.end_date_greater_than') + ->translate([ + 'value' => $startDate->toDateTimeString(), + ]); } } } diff --git a/src/Rules/IsStartDateValid.php b/src/Rules/IsStartDateValid.php index 8c6c62d..69b847c 100644 --- a/src/Rules/IsStartDateValid.php +++ b/src/Rules/IsStartDateValid.php @@ -19,9 +19,9 @@ class IsStartDateValid implements DataAwareRule, ValidationRule * * @param string $attribute * @param mixed $value - * @param Closure $fail + * @param Closure(string): \Illuminate\Translation\PotentiallyTranslatedString $fail * - * @return bool + * @return void */ public function validate(string $attribute, mixed $value, Closure $fail): void { @@ -41,7 +41,10 @@ public function validate(string $attribute, mixed $value, Closure $fail): void } $endDate = Carbon::parse($beam->end); if (Carbon::parse($value)->gte($endDate)) { - $fail(__('enjin-platform-beam::validation.start_date_less_than', ['value' => $endDate->toDateTimeString()])); + $fail('enjin-platform-beam::validation.start_date_less_than') + ->translate([ + 'value' => $endDate->toDateTimeString(), + ]); } } } diff --git a/src/Rules/MaxTokenCount.php b/src/Rules/MaxTokenCount.php index 180b0f6..563e9d5 100644 --- a/src/Rules/MaxTokenCount.php +++ b/src/Rules/MaxTokenCount.php @@ -33,7 +33,7 @@ public function __construct(protected ?string $collectionId) * * @param string $attribute * @param mixed $value - * @param Closure $fail + * @param Closure(string): \Illuminate\Translation\PotentiallyTranslatedString $fail * * @return void */ @@ -62,7 +62,10 @@ public function validate(string $attribute, mixed $value, Closure $fail): void ->count(); if (!$passes) { - $fail(__('enjin-platform-beam::validation.max_token_count', ['limit' => $this->limit])); + $fail('enjin-platform-beam::validation.max_token_count') + ->translate([ + 'limit' => $this->limit, + ]); } } } diff --git a/src/Rules/MaxTokenSupply.php b/src/Rules/MaxTokenSupply.php index 2c3d395..3274aa1 100644 --- a/src/Rules/MaxTokenSupply.php +++ b/src/Rules/MaxTokenSupply.php @@ -28,9 +28,10 @@ class MaxTokenSupply implements DataAwareRule, ValidationRule protected $limit; /** - * The error message. + * The error messages. */ - protected string $error = 'enjin-platform-beam::validation.max_token_supply'; + protected string $maxTokenSupplyMessage = 'enjin-platform-beam::validation.max_token_supply'; + protected string $maxTokenBalanceMessage = 'enjin-platform::validation.max_token_balance'; /** * Create instance of rule. @@ -39,22 +40,12 @@ public function __construct(protected ?string $collectionId) { } - /** - * Get the validation error message. - * - * @return string - */ - public function message() - { - return __($this->error, ['limit' => $this->limit]); - } - /** * Determine if the validation rule passes. * * @param string $attribute * @param mixed $value - * @param Closure $fail + * @param Closure(string): \Illuminate\Translation\PotentiallyTranslatedString $fail * * @return void */ @@ -66,7 +57,10 @@ public function validate(string $attribute, mixed $value, Closure $fail): void ) { if (Arr::get($this->data, str_replace('tokenQuantityPerClaim', 'type', $attribute)) == BeamType::MINT_ON_DEMAND->name) { if (!$collection->max_token_supply >= $value) { - $fail($this->message()); + $fail($this->maxTokenSupplyMessage) + ->translate([ + 'limit' => $this->limit, + ]); return; } @@ -78,7 +72,12 @@ public function validate(string $attribute, mixed $value, Closure $fail): void $wallet = Wallet::firstWhere(['public_key' => Account::daemonPublicKey()]); $collection = Collection::firstWhere(['collection_chain_id' => $this->collectionId]); if (!$wallet || !$collection) { - $fail($this->message()); + $fail($this->maxTokenSupplyMessage) + ->translate([ + 'limit' => $this->limit, + ]); + + return; } $accounts = TokenAccount::join('tokens', 'tokens.id', '=', 'token_accounts.token_id') ->where('token_accounts.wallet_id', $wallet->id) @@ -99,9 +98,7 @@ public function validate(string $attribute, mixed $value, Closure $fail): void ->pluck('quantity', 'token_chain_id'); foreach ($accounts as $account) { if ((int) $account->balance < $value + Arr::get($claims, $account->token_chain_id, 0)) { - $this->error = 'enjin-platform::validation.max_token_balance'; - - $fail($this->message()); + $fail($this->maxTokenBalanceMessage)->translate(); return; } @@ -113,7 +110,12 @@ public function validate(string $attribute, mixed $value, Closure $fail): void $wallet = Wallet::firstWhere(['public_key' => Account::daemonPublicKey()]); $collection = Collection::firstWhere(['collection_chain_id' => $this->collectionId]); if (!$wallet || !$collection) { - $fail($this->message()); + $fail($this->maxTokenSupplyMessage) + ->translate([ + 'limit' => $this->limit, + ]); + + return; } foreach ($ranges as $range) { [$from, $to] = $this->integerRange($range); @@ -136,9 +138,7 @@ public function validate(string $attribute, mixed $value, Closure $fail): void ->pluck('quantity', 'token_chain_id'); foreach ($accounts as $account) { if ((int) $account->balance < $value + Arr::get($claims, $account->token_chain_id, 0)) { - $this->error = 'enjin-platform::validation.max_token_balance'; - - $fail($this->message()); + $fail($this->maxTokenBalanceMessage)->translate(); } } } diff --git a/src/Rules/NotExpired.php b/src/Rules/NotExpired.php index 1baa0d2..f7f4537 100644 --- a/src/Rules/NotExpired.php +++ b/src/Rules/NotExpired.php @@ -14,20 +14,24 @@ public function __construct(protected ?string $code = null) } /** - * Run the validation rule. + * Determine if the validation rule passes. * - * @param \Closure(string): \Illuminate\Translation\PotentiallyTranslatedString $fail + * @param string $attribute + * @param mixed $value + * @param Closure(string): \Illuminate\Translation\PotentiallyTranslatedString $fail + * + * @return void */ public function validate(string $attribute, mixed $value, Closure $fail): void { if (!$beam = Beam::where('code', $this->code ?: $value)->first()) { - $fail(__('validation.exists')); + $fail('validation.exists')->translate(); return; } if (Carbon::parse($beam->end)->isPast()) { - $fail(__('enjin-platform-beam::validation.not_expired')); + $fail('enjin-platform-beam::validation.not_expired')->translate(); } } } diff --git a/src/Rules/NotOwner.php b/src/Rules/NotOwner.php index 729c4c4..9dfa6b6 100644 --- a/src/Rules/NotOwner.php +++ b/src/Rules/NotOwner.php @@ -20,9 +20,13 @@ public function __construct(protected bool $isSingleUse = false) } /** - * Run the validation rule. + * Determine if the validation rule passes. * - * @param \Closure(string): \Illuminate\Translation\PotentiallyTranslatedString $fail + * @param string $attribute + * @param mixed $value + * @param Closure(string): \Illuminate\Translation\PotentiallyTranslatedString $fail + * + * @return void */ public function validate(string $attribute, mixed $value, Closure $fail): void { @@ -34,7 +38,7 @@ public function validate(string $attribute, mixed $value, Closure $fail): void ->first() : Beam::with('collection.owner')->where('code', $code)->first(); if ($beam?->collection?->owner && SS58Address::isSameAddress($value, $beam?->collection?->owner?->public_key)) { - $fail(__('enjin-platform-beam::validation.not_owner')); + $fail('enjin-platform-beam::validation.not_owner')->translate(); } } } diff --git a/src/Rules/NotPaused.php b/src/Rules/NotPaused.php index 92dc77b..7570c34 100644 --- a/src/Rules/NotPaused.php +++ b/src/Rules/NotPaused.php @@ -18,7 +18,7 @@ public function __construct(protected ?string $code = null) * * @param string $attribute * @param mixed $value - * @param Closure $fail + * @param Closure(string): \Illuminate\Translation\PotentiallyTranslatedString $fail * * @return void */ @@ -26,7 +26,7 @@ public function validate(string $attribute, mixed $value, Closure $fail): void { if ($beam = resolve(BeamService::class)->findByCode($this->code ?: $value)) { if ($beam->hasFlag(BeamFlag::PAUSED)) { - $fail(__('enjin-platform-beam::validation.is_paused')); + $fail('enjin-platform-beam::validation.is_paused')->translate(); } } } diff --git a/src/Rules/PassesClaimConditions.php b/src/Rules/PassesClaimConditions.php index 47a5f6f..6483a3e 100644 --- a/src/Rules/PassesClaimConditions.php +++ b/src/Rules/PassesClaimConditions.php @@ -68,19 +68,20 @@ public static function clearConditionalFunctions(): void } /** - * Get the validation error message. + * Determine if the validation rule passes. + * + * @param string $attribute + * @param mixed $value + * @param Closure(string): \Illuminate\Translation\PotentiallyTranslatedString $fail + * + * @return void */ - public function message() - { - return __('enjin-platform-beam::validation.passes_conditions'); - } - public function validate(string $attribute, mixed $value, Closure $fail): void { $conditions = collect(static::$functions); if (!$conditions->every(fn ($function) => $function($attribute, $value, $this->singleUse, $this->data))) { - $fail(__('enjin-platform-beam::validation.passes_conditions')); + $fail('enjin-platform-beam::validation.passes_conditions')->translate(); } } diff --git a/src/Rules/ScanLimit.php b/src/Rules/ScanLimit.php index 2282353..ae85b5f 100644 --- a/src/Rules/ScanLimit.php +++ b/src/Rules/ScanLimit.php @@ -17,7 +17,7 @@ class ScanLimit implements DataAwareRule, ValidationRule * * @param string $attribute * @param mixed $value - * @param Closure $fail + * @param Closure(string): \Illuminate\Translation\PotentiallyTranslatedString $fail * * @return void */ @@ -30,7 +30,7 @@ public function validate(string $attribute, mixed $value, Closure $fail): void ->hasCode($this->data['code']) ->first()?->count) ) { - $fail(__('enjin-platform-beam::validation.scan_limit')); + $fail('enjin-platform-beam::validation.scan_limit')->translate(); } } } diff --git a/src/Rules/SingleUseCodeExist.php b/src/Rules/SingleUseCodeExist.php index 6c98f5b..14a6503 100644 --- a/src/Rules/SingleUseCodeExist.php +++ b/src/Rules/SingleUseCodeExist.php @@ -14,16 +14,16 @@ class SingleUseCodeExist implements ValidationRule * * @param string $attribute * @param mixed $value - * @param Closure $fail + * @param Closure(string): \Illuminate\Translation\PotentiallyTranslatedString $fail * * @return void */ public function validate(string $attribute, mixed $value, Closure $fail): void { - if (BeamService::isSingleUse($value) && null !== BeamClaim::withSingleUseCode($value)->claimable()->first()) { + if (BeamService::isSingleUse($value) && BeamClaim::withSingleUseCode($value)->claimable()->exists()) { return; } - $fail(__('enjin-platform-beam::validation.verify_signed_message')); + $fail('enjin-platform-beam::validation.verify_signed_message')->translate(); } } diff --git a/src/Rules/SingleUseCodesExist.php b/src/Rules/SingleUseCodesExist.php index 20b2adc..e93cba6 100644 --- a/src/Rules/SingleUseCodesExist.php +++ b/src/Rules/SingleUseCodesExist.php @@ -11,7 +11,7 @@ class SingleUseCodesExist extends SingleUseCodeExist * * @param string $attribute * @param mixed $value - * @param Closure $fail + * @param Closure(string): \Illuminate\Translation\PotentiallyTranslatedString $fail * * @return void */ diff --git a/src/Rules/TokenUploadExistInCollection.php b/src/Rules/TokenUploadExistInCollection.php index f9c2cc9..79a32eb 100644 --- a/src/Rules/TokenUploadExistInCollection.php +++ b/src/Rules/TokenUploadExistInCollection.php @@ -27,7 +27,7 @@ public function __construct(protected ?string $collectionId) * * @param string $attribute * @param mixed $value - * @param Closure $fail + * @param Closure(string): \Illuminate\Translation\PotentiallyTranslatedString $fail * * @return void */ @@ -53,7 +53,7 @@ public function validate(string $attribute, mixed $value, Closure $fail): void ->whereHas('collection', fn ($query) => $query->where('collection_chain_id', $this->collectionId)) ->count(); if ($count !== count($integers)) { - $fail($this->message()); + $fail($this->message())->translate(); return; } @@ -66,7 +66,7 @@ public function validate(string $attribute, mixed $value, Closure $fail): void ->whereHas('collection', fn ($query) => $query->where('collection_chain_id', $this->collectionId)) ->count(); if ($count !== ($to - $from) + 1) { - $fail($this->message()); + $fail($this->message())->translate(); } } } @@ -80,6 +80,6 @@ public function validate(string $attribute, mixed $value, Closure $fail): void */ public function message() { - return __('enjin-platform-beam::validation.tokens_exist_in_collection'); + return 'enjin-platform-beam::validation.tokens_exist_in_collection'; } } diff --git a/src/Rules/TokenUploadNotExistInBeam.php b/src/Rules/TokenUploadNotExistInBeam.php index 37d63a6..dac14e1 100644 --- a/src/Rules/TokenUploadNotExistInBeam.php +++ b/src/Rules/TokenUploadNotExistInBeam.php @@ -25,7 +25,7 @@ public function __construct(protected ?Model $beam = null) * * @param string $attribute * @param mixed $value - * @param Closure $fail + * @param Closure(string): \Illuminate\Translation\PotentiallyTranslatedString $fail * * @return void */ @@ -48,7 +48,7 @@ public function validate(string $attribute, mixed $value, Closure $fail): void $integers = collect($tokenIds)->filter(fn ($val) => false === $this->integerRange($val))->all(); if ($integers) { if ($prepare->whereIn('beam_claims.token_chain_id', $integers)->exists()) { - $fail($this->message()); + $fail($this->message())->translate(); return; } @@ -57,7 +57,7 @@ public function validate(string $attribute, mixed $value, Closure $fail): void foreach ($ranges as $range) { [$from, $to] = $this->integerRange($range); if ($prepare->whereBetween('beam_claims.token_chain_id', [(int) $from, (int) $to])->exists()) { - $fail($this->message()); + $fail($this->message())->translate(); } } } @@ -70,6 +70,6 @@ public function validate(string $attribute, mixed $value, Closure $fail): void */ public function message() { - return __('enjin-platform-beam::validation.tokens_doesnt_exist_in_beam'); + return 'enjin-platform-beam::validation.tokens_doesnt_exist_in_beam'; } } diff --git a/src/Rules/TokenUploadNotExistInCollection.php b/src/Rules/TokenUploadNotExistInCollection.php index 3f94388..9b3bf51 100644 --- a/src/Rules/TokenUploadNotExistInCollection.php +++ b/src/Rules/TokenUploadNotExistInCollection.php @@ -21,7 +21,7 @@ public function __construct(protected ?string $collectionId) * * @param string $attribute * @param mixed $value - * @param Closure $fail + * @param Closure(string): \Illuminate\Translation\PotentiallyTranslatedString $fail * * @return void */ @@ -47,7 +47,7 @@ public function validate(string $attribute, mixed $value, Closure $fail): void ->whereHas('collection', fn ($query) => $query->where('collection_chain_id', $this->collectionId)) ->exists(); if ($exists) { - $fail($this->message()); + $fail($this->message())->translate(); return; } @@ -60,7 +60,7 @@ public function validate(string $attribute, mixed $value, Closure $fail): void ->whereHas('collection', fn ($query) => $query->where('collection_chain_id', $this->collectionId)) ->exists(); if ($exists) { - $fail($this->message()); + $fail($this->message())->translate(); } } } @@ -74,6 +74,6 @@ public function validate(string $attribute, mixed $value, Closure $fail): void */ public function message() { - return __('enjin-platform-beam::validation.tokens_doesnt_exist_in_collection'); + return 'enjin-platform-beam::validation.tokens_doesnt_exist_in_collection'; } } diff --git a/src/Rules/TokensDoNotExistInBeam.php b/src/Rules/TokensDoNotExistInBeam.php index 1ad12bd..d9d4a44 100644 --- a/src/Rules/TokensDoNotExistInBeam.php +++ b/src/Rules/TokensDoNotExistInBeam.php @@ -27,7 +27,7 @@ public function __construct(protected ?Model $beam = null) * * @param string $attribute * @param mixed $value - * @param Closure $fail + * @param Closure(string): \Illuminate\Translation\PotentiallyTranslatedString $fail * * @return void */ @@ -37,7 +37,7 @@ public function validate(string $attribute, mixed $value, Closure $fail): void $integers = collect($value)->filter(fn ($val) => false === $this->integerRange($val))->all(); if ($integers) { if ($prepare->whereIn('beam_claims.token_chain_id', $integers)->exists()) { - $fail($this->message()); + $fail($this->message())->translate(); return; } @@ -46,7 +46,7 @@ public function validate(string $attribute, mixed $value, Closure $fail): void foreach ($ranges as $range) { [$from, $to] = $this->integerRange($range); if ($prepare->whereBetween('beam_claims.token_chain_id', [(int) $from, (int) $to])->exists()) { - $fail($this->message()); + $fail($this->message())->translate(); } } } @@ -86,6 +86,6 @@ public static function prepareStatement(?Model $beam, ?string $collectionId = nu */ public function message() { - return __('enjin-platform-beam::validation.tokens_doesnt_exist_in_beam'); + return 'enjin-platform-beam::validation.tokens_doesnt_exist_in_beam'; } } diff --git a/src/Rules/TokensDoNotExistInCollection.php b/src/Rules/TokensDoNotExistInCollection.php index 7dbfd05..dd2feaa 100644 --- a/src/Rules/TokensDoNotExistInCollection.php +++ b/src/Rules/TokensDoNotExistInCollection.php @@ -20,7 +20,7 @@ public function __construct(protected ?string $collectionId) * * @param string $attribute * @param mixed $value - * @param Closure $fail + * @param Closure(string): \Illuminate\Translation\PotentiallyTranslatedString $fail * * @return void */ @@ -33,7 +33,7 @@ public function validate(string $attribute, mixed $value, Closure $fail): void ->whereHas('collection', fn ($query) => $query->where('collection_chain_id', $this->collectionId)) ->exists(); if ($exists) { - $fail($this->message()); + $fail($this->message())->translate(); return; } @@ -46,7 +46,7 @@ public function validate(string $attribute, mixed $value, Closure $fail): void ->exists(); if ($exists) { - $fail($this->message()); + $fail($this->message())->translate(); } } } @@ -59,6 +59,6 @@ public function validate(string $attribute, mixed $value, Closure $fail): void */ public function message() { - return __('enjin-platform-beam::validation.tokens_doesnt_exist_in_collection'); + return 'enjin-platform-beam::validation.tokens_doesnt_exist_in_collection'; } } diff --git a/src/Rules/TokensExistInBeam.php b/src/Rules/TokensExistInBeam.php index 26add13..62cdd69 100644 --- a/src/Rules/TokensExistInBeam.php +++ b/src/Rules/TokensExistInBeam.php @@ -19,7 +19,7 @@ class TokensExistInBeam implements DataAwareRule, ValidationRule * * @param string $attribute * @param mixed $value - * @param Closure $fail + * @param Closure(string): \Illuminate\Translation\PotentiallyTranslatedString $fail * * @return void */ @@ -34,7 +34,7 @@ public function validate(string $attribute, mixed $value, Closure $fail): void ->whereHas('beam', fn ($query) => $query->where('code', $code)) ->count(); if ($count != count($integers)) { - $fail($this->message()); + $fail($this->message())->translate(); return; } @@ -48,7 +48,7 @@ public function validate(string $attribute, mixed $value, Closure $fail): void ->whereHas('beam', fn ($query) => $query->where('code', $code)) ->count(); if ($count !== ($to - $from) + 1) { - $fail($this->message()); + $fail($this->message())->translate(); } } } @@ -61,6 +61,6 @@ public function validate(string $attribute, mixed $value, Closure $fail): void */ public function message() { - return __('enjin-platform-beam::validation.tokens_exist_in_beam'); + return 'enjin-platform-beam::validation.tokens_exist_in_beam'; } } diff --git a/src/Rules/TokensExistInCollection.php b/src/Rules/TokensExistInCollection.php index 4eaf8e2..a0b6e03 100644 --- a/src/Rules/TokensExistInCollection.php +++ b/src/Rules/TokensExistInCollection.php @@ -20,7 +20,7 @@ public function __construct(protected ?string $collectionId) * * @param string $attribute * @param mixed $value - * @param Closure $fail + * @param Closure(string): \Illuminate\Translation\PotentiallyTranslatedString $fail * * @return void */ @@ -33,7 +33,7 @@ public function validate(string $attribute, mixed $value, Closure $fail): void ->whereHas('collection', fn ($query) => $query->where('collection_chain_id', $this->collectionId)) ->count(); if ($count !== count($integers)) { - $fail($this->message()); + $fail($this->message())->translate(); return; } @@ -45,7 +45,7 @@ public function validate(string $attribute, mixed $value, Closure $fail): void ->whereHas('collection', fn ($query) => $query->where('collection_chain_id', $this->collectionId)) ->count(); if ($count !== ($to - $from) + 1) { - $fail($this->message()); + $fail($this->message())->translate(); } } } @@ -58,6 +58,6 @@ public function validate(string $attribute, mixed $value, Closure $fail): void */ public function message() { - return __('enjin-platform-beam::validation.tokens_exist_in_collection'); + return 'enjin-platform-beam::validation.tokens_exist_in_collection'; } } diff --git a/src/Rules/UniqueTokenIds.php b/src/Rules/UniqueTokenIds.php index 3df1fde..4c91eaa 100644 --- a/src/Rules/UniqueTokenIds.php +++ b/src/Rules/UniqueTokenIds.php @@ -10,6 +10,15 @@ class UniqueTokenIds implements ValidationRule { use IntegerRange; + /** + * Determine if the validation rule passes. + * + * @param string $attribute + * @param mixed $value + * @param Closure(string): \Illuminate\Translation\PotentiallyTranslatedString $fail + * + * @return void + */ public function validate(string $attribute, mixed $value, Closure $fail): void { $tokenIds = []; @@ -21,7 +30,7 @@ public function validate(string $attribute, mixed $value, Closure $fail): void foreach ($tokens->all() as $tokenId) { if ($this->tokenIdExists($tokenIds, $tokenId)) { - $fail(__('enjin-platform-beam::validation.duplicate_token_ids')); + $fail('enjin-platform-beam::validation.duplicate_token_ids')->translate(); return; } diff --git a/src/Rules/VerifySignedMessage.php b/src/Rules/VerifySignedMessage.php index 1909f12..fbec846 100644 --- a/src/Rules/VerifySignedMessage.php +++ b/src/Rules/VerifySignedMessage.php @@ -22,14 +22,17 @@ class VerifySignedMessage implements DataAwareRule, ValidationRule * * @param string $attribute * @param mixed $value - * @param Closure $fail + * @param Closure(string): \Illuminate\Translation\PotentiallyTranslatedString $fail * * @return void */ public function validate(string $attribute, mixed $value, Closure $fail): void { if (!$publicKey = SS58Address::getPublicKey($this->data['account'])) { - $fail(__('enjin-platform::validation.valid_substrate_account', ['attribute' => 'account'])); + $fail('enjin-platform::validation.valid_substrate_account') + ->translate([ + 'attribute' => 'account', + ]); return; } @@ -39,7 +42,7 @@ public function validate(string $attribute, mixed $value, Closure $fail): void } if (!$scan = BeamScan::hasCode($this->data['code'])->firstWhere(['wallet_public_key' => $publicKey])) { - $fail(__('enjin-platform-beam::validation.beam_scan_not_found')); + $fail('enjin-platform-beam::validation.beam_scan_not_found')->translate(); return; } @@ -58,7 +61,7 @@ public function validate(string $attribute, mixed $value, Closure $fail): void ); if (!$passes) { - $fail(__('enjin-platform-beam::validation.verify_signed_message')); + $fail('enjin-platform-beam::validation.verify_signed_message')->translate(); } } } From d2f72174f1363e5d0619c0dd81e0b8e436244494 Mon Sep 17 00:00:00 2001 From: Simon Evans Date: Mon, 2 Oct 2023 12:03:07 +0100 Subject: [PATCH 3/4] Use the HasDataAwareRule trait from platform-core. --- src/Rules/CanClaim.php | 2 +- src/Rules/IsEndDateValid.php | 2 +- src/Rules/IsStartDateValid.php | 2 +- src/Rules/MaxTokenCount.php | 2 +- src/Rules/MaxTokenSupply.php | 2 +- src/Rules/NotOwner.php | 2 +- src/Rules/PassesClaimConditions.php | 12 +++-------- src/Rules/ScanLimit.php | 2 +- src/Rules/TokenUploadNotExistInBeam.php | 2 +- src/Rules/TokensDoNotExistInBeam.php | 2 +- src/Rules/TokensExistInBeam.php | 2 +- src/Rules/Traits/HasDataAwareRule.php | 27 ------------------------- src/Rules/VerifySignedMessage.php | 2 +- 13 files changed, 14 insertions(+), 47 deletions(-) delete mode 100644 src/Rules/Traits/HasDataAwareRule.php diff --git a/src/Rules/CanClaim.php b/src/Rules/CanClaim.php index c88e83f..4293e0a 100644 --- a/src/Rules/CanClaim.php +++ b/src/Rules/CanClaim.php @@ -3,8 +3,8 @@ namespace Enjin\Platform\Beam\Rules; use Closure; -use Enjin\Platform\Beam\Rules\Traits\HasDataAwareRule; use Enjin\Platform\Beam\Services\BeamService; +use Enjin\Platform\Rules\Traits\HasDataAwareRule; use Illuminate\Contracts\Validation\DataAwareRule; use Illuminate\Contracts\Validation\ValidationRule; use Illuminate\Support\Arr; diff --git a/src/Rules/IsEndDateValid.php b/src/Rules/IsEndDateValid.php index 5a39d30..2ccac4b 100644 --- a/src/Rules/IsEndDateValid.php +++ b/src/Rules/IsEndDateValid.php @@ -4,8 +4,8 @@ use Carbon\Carbon; use Closure; -use Enjin\Platform\Beam\Rules\Traits\HasDataAwareRule; use Enjin\Platform\Beam\Services\BeamService; +use Enjin\Platform\Rules\Traits\HasDataAwareRule; use Illuminate\Contracts\Validation\DataAwareRule; use Illuminate\Contracts\Validation\ValidationRule; use Illuminate\Support\Arr; diff --git a/src/Rules/IsStartDateValid.php b/src/Rules/IsStartDateValid.php index 69b847c..1d12d0e 100644 --- a/src/Rules/IsStartDateValid.php +++ b/src/Rules/IsStartDateValid.php @@ -4,8 +4,8 @@ use Carbon\Carbon; use Closure; -use Enjin\Platform\Beam\Rules\Traits\HasDataAwareRule; use Enjin\Platform\Beam\Services\BeamService; +use Enjin\Platform\Rules\Traits\HasDataAwareRule; use Illuminate\Contracts\Validation\DataAwareRule; use Illuminate\Contracts\Validation\ValidationRule; use Illuminate\Support\Arr; diff --git a/src/Rules/MaxTokenCount.php b/src/Rules/MaxTokenCount.php index 563e9d5..9d826e9 100644 --- a/src/Rules/MaxTokenCount.php +++ b/src/Rules/MaxTokenCount.php @@ -5,9 +5,9 @@ use Closure; use Enjin\Platform\Beam\Enums\BeamType; use Enjin\Platform\Beam\Models\BeamClaim; -use Enjin\Platform\Beam\Rules\Traits\HasDataAwareRule; use Enjin\Platform\Beam\Rules\Traits\IntegerRange; use Enjin\Platform\Models\Collection; +use Enjin\Platform\Rules\Traits\HasDataAwareRule; use Illuminate\Contracts\Validation\DataAwareRule; use Illuminate\Contracts\Validation\ValidationRule; use Illuminate\Support\Arr; diff --git a/src/Rules/MaxTokenSupply.php b/src/Rules/MaxTokenSupply.php index 3274aa1..3b05df2 100644 --- a/src/Rules/MaxTokenSupply.php +++ b/src/Rules/MaxTokenSupply.php @@ -5,11 +5,11 @@ use Closure; use Enjin\Platform\Beam\Enums\BeamType; use Enjin\Platform\Beam\Models\BeamClaim; -use Enjin\Platform\Beam\Rules\Traits\HasDataAwareRule; use Enjin\Platform\Beam\Rules\Traits\IntegerRange; use Enjin\Platform\Models\Collection; use Enjin\Platform\Models\TokenAccount; use Enjin\Platform\Models\Wallet; +use Enjin\Platform\Rules\Traits\HasDataAwareRule; use Enjin\Platform\Support\Account; use Illuminate\Contracts\Validation\DataAwareRule; use Illuminate\Contracts\Validation\ValidationRule; diff --git a/src/Rules/NotOwner.php b/src/Rules/NotOwner.php index 9dfa6b6..974f745 100644 --- a/src/Rules/NotOwner.php +++ b/src/Rules/NotOwner.php @@ -5,7 +5,7 @@ use Closure; use Enjin\Platform\Beam\Models\Beam; use Enjin\Platform\Beam\Models\BeamClaim; -use Enjin\Platform\Beam\Rules\Traits\HasDataAwareRule; +use Enjin\Platform\Rules\Traits\HasDataAwareRule; use Enjin\Platform\Support\SS58Address; use Illuminate\Contracts\Validation\DataAwareRule; use Illuminate\Contracts\Validation\ValidationRule; diff --git a/src/Rules/PassesClaimConditions.php b/src/Rules/PassesClaimConditions.php index 6483a3e..da32415 100644 --- a/src/Rules/PassesClaimConditions.php +++ b/src/Rules/PassesClaimConditions.php @@ -3,6 +3,7 @@ namespace Enjin\Platform\Beam\Rules; use Closure; +use Enjin\Platform\Rules\Traits\HasDataAwareRule; use Illuminate\Contracts\Validation\DataAwareRule; use Illuminate\Contracts\Validation\ValidationRule; use Illuminate\Support\Arr; @@ -11,9 +12,9 @@ class PassesClaimConditions implements DataAwareRule, ValidationRule { - protected static array $functions = []; + use HasDataAwareRule; - protected array $data = []; + protected static array $functions = []; /** * Create new rule instance. @@ -84,11 +85,4 @@ public function validate(string $attribute, mixed $value, Closure $fail): void $fail('enjin-platform-beam::validation.passes_conditions')->translate(); } } - - public function setData(array $data) - { - $this->data = $data; - - return $this; - } } diff --git a/src/Rules/ScanLimit.php b/src/Rules/ScanLimit.php index ae85b5f..eb83b0c 100644 --- a/src/Rules/ScanLimit.php +++ b/src/Rules/ScanLimit.php @@ -4,7 +4,7 @@ use Closure; use Enjin\Platform\Beam\Models\BeamScan; -use Enjin\Platform\Beam\Rules\Traits\HasDataAwareRule; +use Enjin\Platform\Rules\Traits\HasDataAwareRule; use Illuminate\Contracts\Validation\DataAwareRule; use Illuminate\Contracts\Validation\ValidationRule; diff --git a/src/Rules/TokenUploadNotExistInBeam.php b/src/Rules/TokenUploadNotExistInBeam.php index dac14e1..3ff614d 100644 --- a/src/Rules/TokenUploadNotExistInBeam.php +++ b/src/Rules/TokenUploadNotExistInBeam.php @@ -3,8 +3,8 @@ namespace Enjin\Platform\Beam\Rules; use Closure; -use Enjin\Platform\Beam\Rules\Traits\HasDataAwareRule; use Enjin\Platform\Beam\Rules\Traits\IntegerRange; +use Enjin\Platform\Rules\Traits\HasDataAwareRule; use Illuminate\Contracts\Validation\DataAwareRule; use Illuminate\Contracts\Validation\ValidationRule; use Illuminate\Database\Eloquent\Model; diff --git a/src/Rules/TokensDoNotExistInBeam.php b/src/Rules/TokensDoNotExistInBeam.php index d9d4a44..b665b67 100644 --- a/src/Rules/TokensDoNotExistInBeam.php +++ b/src/Rules/TokensDoNotExistInBeam.php @@ -4,9 +4,9 @@ use Closure; use Enjin\Platform\Beam\Models\BeamClaim; -use Enjin\Platform\Beam\Rules\Traits\HasDataAwareRule; use Enjin\Platform\Beam\Rules\Traits\IntegerRange; use Enjin\Platform\Enums\Substrate\TokenMintCapType; +use Enjin\Platform\Rules\Traits\HasDataAwareRule; use Illuminate\Contracts\Validation\DataAwareRule; use Illuminate\Contracts\Validation\ValidationRule; use Illuminate\Database\Eloquent\Builder; diff --git a/src/Rules/TokensExistInBeam.php b/src/Rules/TokensExistInBeam.php index 62cdd69..6e38930 100644 --- a/src/Rules/TokensExistInBeam.php +++ b/src/Rules/TokensExistInBeam.php @@ -4,8 +4,8 @@ use Closure; use Enjin\Platform\Beam\Models\BeamClaim; -use Enjin\Platform\Beam\Rules\Traits\HasDataAwareRule; use Enjin\Platform\Beam\Rules\Traits\IntegerRange; +use Enjin\Platform\Rules\Traits\HasDataAwareRule; use Illuminate\Contracts\Validation\DataAwareRule; use Illuminate\Contracts\Validation\ValidationRule; diff --git a/src/Rules/Traits/HasDataAwareRule.php b/src/Rules/Traits/HasDataAwareRule.php deleted file mode 100644 index 52d1b77..0000000 --- a/src/Rules/Traits/HasDataAwareRule.php +++ /dev/null @@ -1,27 +0,0 @@ -data = $data; - - return $this; - } -} diff --git a/src/Rules/VerifySignedMessage.php b/src/Rules/VerifySignedMessage.php index fbec846..a22b439 100644 --- a/src/Rules/VerifySignedMessage.php +++ b/src/Rules/VerifySignedMessage.php @@ -5,9 +5,9 @@ use Closure; use Enjin\BlockchainTools\HexConverter; use Enjin\Platform\Beam\Models\BeamScan; -use Enjin\Platform\Beam\Rules\Traits\HasDataAwareRule; use Enjin\Platform\Beam\Services\BeamService; use Enjin\Platform\Enums\Substrate\CryptoSignatureType; +use Enjin\Platform\Rules\Traits\HasDataAwareRule; use Enjin\Platform\Services\Blockchain\Interfaces\BlockchainServiceInterface; use Enjin\Platform\Support\SS58Address; use Illuminate\Contracts\Validation\DataAwareRule; From acde00ef8d9c3ee679ccdb2401a6f5f9762571eb Mon Sep 17 00:00:00 2001 From: Simon Evans Date: Mon, 2 Oct 2023 13:14:45 +0100 Subject: [PATCH 4/4] Use translate method. --- src/Rules/IsEndDateValid.php | 2 +- src/Rules/IsStartDateValid.php | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Rules/IsEndDateValid.php b/src/Rules/IsEndDateValid.php index 2ccac4b..804ed49 100644 --- a/src/Rules/IsEndDateValid.php +++ b/src/Rules/IsEndDateValid.php @@ -28,7 +28,7 @@ public function validate(string $attribute, mixed $value, Closure $fail): void $date = Carbon::parse($value); $start = Arr::get($this->data, 'start'); if ($start && $date->lte(Carbon::parse($start))) { - $fail(__('enjin-platform-beam::validation.end_date_after_start')); + $fail('enjin-platform-beam::validation.end_date_after_start')->translate(); return; } diff --git a/src/Rules/IsStartDateValid.php b/src/Rules/IsStartDateValid.php index 1d12d0e..4f7267a 100644 --- a/src/Rules/IsStartDateValid.php +++ b/src/Rules/IsStartDateValid.php @@ -27,7 +27,7 @@ public function validate(string $attribute, mixed $value, Closure $fail): void { if ($end = Arr::get($this->data, 'end')) { if (Carbon::parse($value)->gte(Carbon::parse($end))) { - $fail(__('enjin-platform-beam::validation.start_date_after_end')); + $fail('enjin-platform-beam::validation.start_date_after_end')->translate(); return; } @@ -35,7 +35,7 @@ public function validate(string $attribute, mixed $value, Closure $fail): void if ($beam = resolve(BeamService::class)->findByCode($this->data['code'])) { if (Carbon::parse($beam->start)->isPast()) { - $fail(__('enjin-platform-beam::validation.start_date_has_passed')); + $fail('enjin-platform-beam::validation.start_date_has_passed')->translate(); return; }