Skip to content

Commit

Permalink
[PLA-1304] Switch Rule to ValidationRule. (#67)
Browse files Browse the repository at this point in the history
  • Loading branch information
v16Studios authored Oct 2, 2023
1 parent 45093db commit 4f3c5c1
Show file tree
Hide file tree
Showing 35 changed files with 424 additions and 838 deletions.
50 changes: 16 additions & 34 deletions src/Rules/AccountExistsInCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@

namespace Enjin\Platform\Rules;

use Closure;
use Enjin\Platform\Rules\Traits\HasDataAwareRule;
use Enjin\Platform\Services\Database\CollectionService;
use Illuminate\Contracts\Validation\DataAwareRule;
use Illuminate\Contracts\Validation\Rule;
use Illuminate\Contracts\Validation\ValidationRule;

class AccountExistsInCollection implements DataAwareRule, Rule
class AccountExistsInCollection implements DataAwareRule, ValidationRule
{
/**
* All of the data under validation.
*/
protected array $data = [];
use HasDataAwareRule;

/**
* The collection service.
Expand All @@ -23,43 +22,26 @@ class AccountExistsInCollection implements DataAwareRule, Rule
*/
public function __construct()
{
$this->collectionService = app()->make(CollectionService::class);
$this->collectionService = resolve(CollectionService::class);
}

/**
* Determine if the validation rule passes.
*
* @param string $attribute
* @param mixed $value
* @param Closure(string): \Illuminate\Translation\PotentiallyTranslatedString $fail
*
* @return bool
*/
public function passes($attribute, $value)
{
return $this->collectionService->accountExistsInCollection($this->data['collectionId'], $value);
}

/**
* Get the validation error message.
*
* @return string
* @return void
*/
public function message()
public function validate(string $attribute, mixed $value, Closure $fail): void
{
return __('enjin-platform::validation.account_exists_in_collection', ['account' => $this->data['collectionAccount'], 'collectionId' => $this->data['collectionId']]);
}

/**
* Set the data under validation.
*
* @param array $data
*
* @return $this
*/
public function setData($data)
{
$this->data = $data;

return $this;
if (!$this->collectionService->accountExistsInCollection($this->data['collectionId'], $value)) {
$fail('enjin-platform::validation.account_exists_in_collection')
->translate([
'account' => $this->data['collectionAccount'],
'collectionId' => $this->data['collectionId'],
]);
}
}
}
60 changes: 16 additions & 44 deletions src/Rules/AccountExistsInToken.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,79 +2,51 @@

namespace Enjin\Platform\Rules;

use Closure;
use Enjin\Platform\GraphQL\Schemas\Primary\Substrate\Traits\HasEncodableTokenId;
use Enjin\Platform\Rules\Traits\HasDataAwareRule;
use Enjin\Platform\Services\Database\TokenService;
use Illuminate\Contracts\Validation\DataAwareRule;
use Illuminate\Contracts\Validation\Rule;
use Illuminate\Contracts\Validation\ValidationRule;

class AccountExistsInToken implements DataAwareRule, Rule
class AccountExistsInToken implements DataAwareRule, ValidationRule
{
use HasDataAwareRule;
use HasEncodableTokenId;

/**
* All of the data under validation.
*/
protected array $data = [];

/**
* The token service.
*/
protected TokenService $tokenService;

/**
* The error message.
*/
protected string $message;

/**
* Create instance.
*/
public function __construct()
{
$this->tokenService = app()->make(TokenService::class);
$this->tokenService = resolve(TokenService::class);
}

/**
* Determine if the validation rule passes.
*
* @param string $attribute
* @param mixed $value
* @param Closure(string): \Illuminate\Translation\PotentiallyTranslatedString $fail
*
* @return bool
* @return void
*/
public function passes($attribute, $value)
public function validate(string $attribute, mixed $value, Closure $fail): void
{
$tokenId = $this->encodeTokenId($this->data);
$this->message = __('enjin-platform::validation.account_exists_in_token', ['account' => $this->data['tokenAccount'], 'collectionId' => $this->data['collectionId'], 'tokenId' => $tokenId]);

if (!$tokenId) {
return false;
if (!$tokenId || !$this->tokenService->accountExistsInToken($this->data['collectionId'], $tokenId, $value)) {
$fail('enjin-platform::validation.account_exists_in_token')
->translate([
'account' => $this->data['tokenAccount'],
'collectionId' => $this->data['collectionId'],
'tokenId' => $tokenId,
]);
}

return $this->tokenService->accountExistsInToken($this->data['collectionId'], $tokenId, $value);
}

/**
* Get the validation error message.
*
* @return string
*/
public function message()
{
return $this->message;
}

/**
* Set the data under validation.
*
* @param array $data
*
* @return $this
*/
public function setData($data)
{
$this->data = $data;

return $this;
}
}
26 changes: 10 additions & 16 deletions src/Rules/AccountExistsInWallet.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@

namespace Enjin\Platform\Rules;

use Closure;
use Enjin\Platform\Services\Database\WalletService;
use Illuminate\Contracts\Validation\Rule;
use Illuminate\Contracts\Validation\ValidationRule;

class AccountExistsInWallet implements Rule
class AccountExistsInWallet implements ValidationRule
{
/**
* The wallet service.
Expand All @@ -17,29 +18,22 @@ class AccountExistsInWallet implements Rule
*/
public function __construct()
{
$this->walletService = app()->make(WalletService::class);
$this->walletService = resolve(WalletService::class);
}

/**
* Determine if the validation rule passes.
*
* @param string $attribute
* @param mixed $value
* @param Closure(string): \Illuminate\Translation\PotentiallyTranslatedString $fail
*
* @return bool
* @return void
*/
public function passes($attribute, $value)
public function validate(string $attribute, mixed $value, Closure $fail): void
{
return $this->walletService->accountExistsInWallet($value);
}

/**
* Get the validation error message.
*
* @return string
*/
public function message()
{
return __('enjin-platform::validation.account_exists_in_wallet');
if (!$this->walletService->accountExistsInWallet($value)) {
$fail('enjin-platform::validation.account_exists_in_wallet')->translate();
}
}
}
50 changes: 16 additions & 34 deletions src/Rules/ApprovalExistsInCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@

namespace Enjin\Platform\Rules;

use Closure;
use Enjin\Platform\Rules\Traits\HasDataAwareRule;
use Enjin\Platform\Services\Database\CollectionService;
use Illuminate\Contracts\Validation\DataAwareRule;
use Illuminate\Contracts\Validation\Rule;
use Illuminate\Contracts\Validation\ValidationRule;

class ApprovalExistsInCollection implements DataAwareRule, Rule
class ApprovalExistsInCollection implements DataAwareRule, ValidationRule
{
/**
* All of the data under validation.
*/
protected array $data = [];
use HasDataAwareRule;

/**
* The collection service.
Expand All @@ -23,43 +22,26 @@ class ApprovalExistsInCollection implements DataAwareRule, Rule
*/
public function __construct()
{
$this->collectionService = app()->make(CollectionService::class);
$this->collectionService = resolve(CollectionService::class);
}

/**
* Determine if the validation rule passes.
*
* @param string $attribute
* @param mixed $value
* @param Closure(string): \Illuminate\Translation\PotentiallyTranslatedString $fail
*
* @return bool
*/
public function passes($attribute, $value)
{
return $this->collectionService->approvalExistsInCollection($this->data['collectionId'], $value);
}

/**
* Get the validation error message.
*
* @return string
* @return void
*/
public function message()
public function validate(string $attribute, mixed $value, Closure $fail): void
{
return __('enjin-platform::validation.approval_exists_in_collection', ['operator' => $this->data['operator'], 'collectionId' => $this->data['collectionId']]);
}

/**
* Set the data under validation.
*
* @param array $data
*
* @return $this
*/
public function setData($data)
{
$this->data = $data;

return $this;
if (!$this->collectionService->approvalExistsInCollection($this->data['collectionId'], $value)) {
$fail('enjin-platform::validation.approval_exists_in_collection')
->translate([
'operator' => $this->data['operator'],
'collectionId' => $this->data['collectionId'],
]);
}
}
}
61 changes: 17 additions & 44 deletions src/Rules/ApprovalExistsInToken.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,79 +2,52 @@

namespace Enjin\Platform\Rules;

use Closure;
use Enjin\Platform\GraphQL\Schemas\Primary\Substrate\Traits\HasEncodableTokenId;
use Enjin\Platform\Rules\Traits\HasDataAwareRule;
use Enjin\Platform\Services\Database\TokenService;
use Illuminate\Contracts\Validation\DataAwareRule;
use Illuminate\Contracts\Validation\Rule;
use Illuminate\Contracts\Validation\ValidationRule;

class ApprovalExistsInToken implements DataAwareRule, Rule
class ApprovalExistsInToken implements DataAwareRule, ValidationRule
{
use HasDataAwareRule;
use HasEncodableTokenId;

/**
* All of the data under validation.
*/
protected array $data = [];

/**
* The token service.
*/
protected TokenService $tokenService;

/**
* The error message.
*/
protected string $message;

/**
* Create a new rule instance.
*/
public function __construct()
{
$this->tokenService = app()->make(TokenService::class);
$this->tokenService = resolve(TokenService::class);
}

/**
* Determine if the validation rule passes.
*
* @param string $attribute
* @param mixed $value
* @param Closure(string): \Illuminate\Translation\PotentiallyTranslatedString $fail
*
* @return bool
* @return void
*/
public function passes($attribute, $value)
public function validate(string $attribute, mixed $value, Closure $fail): void
{
$tokenId = $this->encodeTokenId($this->data);
$this->message = __('enjin-platform::validation.approval_exists_in_token', ['operator' => $this->data['operator'], 'collectionId' => $this->data['collectionId'], 'tokenId' => $tokenId]);

if (!$tokenId) {
return false;
if (!$tokenId || !$this->tokenService->approvalExistsInToken($this->data['collectionId'], $tokenId, $value)) {
$fail('enjin-platform::validation.approval_exists_in_token')
->translate(
[
'operator' => $this->data['operator'],
'collectionId' => $this->data['collectionId'],
'tokenId' => $tokenId],
);
}

return $this->tokenService->approvalExistsInToken($this->data['collectionId'], $tokenId, $value);
}

/**
* Get the validation error message.
*
* @return string
*/
public function message()
{
return $this->message;
}

/**
* Set the data under validation.
*
* @param array $data
*
* @return $this
*/
public function setData($data)
{
$this->data = $data;

return $this;
}
}
Loading

0 comments on commit 4f3c5c1

Please sign in to comment.