Skip to content

Commit

Permalink
Inserção de atributos para facilitar manutenção.
Browse files Browse the repository at this point in the history
  • Loading branch information
luispaiva committed Jul 9, 2022
1 parent cfd8068 commit c86ab7f
Showing 1 changed file with 25 additions and 4 deletions.
29 changes: 25 additions & 4 deletions src/Helpers/Validate.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,27 @@
*/
final class Validate
{
/**
* Attributes names.
*
* @var array
*/
private static $_attributes = ['amount', 'from', 'to', 'rate'];

/**
* From currency.
*
* @var array
*/
private static $_from = ['EUR', 'USD', 'BRL'];

/**
* To currency.
*
* @var array
*/
private static $_to = ['EUR', 'USD', 'BRL'];

/**
* Validate args.
*
Expand All @@ -34,16 +55,16 @@ final class Validate
*/
public static function args(array $args)
{
$attributes = ['amount', 'from', 'to', 'rate'];
$error = new \stdClass();

if (empty($args)) {
$error->message = 'Missing parameters';
$error->code = 400;

return $error;
}

foreach ($attributes as $attribute) {
foreach (self::$_attributes as $attribute) {
if (! isset($args[$attribute])) {
$error->message = "Missing {$attribute} parameter";
$error->code = 400;
Expand All @@ -66,14 +87,14 @@ public static function args(array $args)
return $error;
}

if (! in_array($args['from'], ['EUR', 'USD', 'BRL'])) {
if (! in_array($args['from'], self::$_from)) {
$error->message = 'Invalid from currency';
$error->code = 400;

return $error;
}

if (! in_array($args['to'], ['EUR', 'USD', 'BRL'])) {
if (! in_array($args['to'], self::$_to)) {
$error->message = 'Invalid to currency';
$error->code = 400;

Expand Down

0 comments on commit c86ab7f

Please sign in to comment.