Skip to content

Commit

Permalink
GH-139 Turn validator into parser for reusability
Browse files Browse the repository at this point in the history
  • Loading branch information
mdziekon committed May 25, 2022
1 parent 6f993ca commit 3a4916f
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 13 deletions.
6 changes: 3 additions & 3 deletions fleet1.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,14 @@

// Management of ShipsList
if (!empty($_POST['ship'])) {
$fleetArrayValidationResult = FlightControl\Utils\Validators\validateFleetArray([
$fleetArrayParsingResult = FlightControl\Utils\Validators\parseFleetArray([
'fleet' => $_POST['ship'],
'planet' => &$_Planet,
'isFromDirectUserInput' => true,
]);

if (!$fleetArrayValidationResult['isValid']) {
$firstValidationError = $fleetArrayValidationResult['errors'][0];
if (!$fleetArrayParsingResult['isValid']) {
$firstValidationError = $fleetArrayParsingResult['errors'][0];

$errorMessage = null;
switch ($firstValidationError['errorCode']) {
Expand Down
6 changes: 3 additions & 3 deletions fleet2.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,14 +171,14 @@
message($_Lang['fl1_NoShipsGiven'], $ErrorTitle, 'fleet.php', 3);
}

$fleetArrayValidationResult = FlightControl\Utils\Validators\validateFleetArray([
$fleetArrayParsingResult = FlightControl\Utils\Validators\parseFleetArray([
'fleet' => $Fleet['array'],
'planet' => &$_Planet,
'isFromDirectUserInput' => false,
]);

if (!$fleetArrayValidationResult['isValid']) {
$firstValidationError = $fleetArrayValidationResult['errors'][0];
if (!$fleetArrayParsingResult['isValid']) {
$firstValidationError = $fleetArrayParsingResult['errors'][0];

$errorMessage = null;
switch ($firstValidationError['errorCode']) {
Expand Down
6 changes: 3 additions & 3 deletions fleet3.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,14 +188,14 @@ function messageRed($Text, $Title)
messageRed($_Lang['fl1_NoShipsGiven'], $ErrorTitle);
}

$fleetArrayValidationResult = FlightControl\Utils\Validators\validateFleetArray([
$fleetArrayParsingResult = FlightControl\Utils\Validators\parseFleetArray([
'fleet' => $Fleet['array'],
'planet' => &$_Planet,
'isFromDirectUserInput' => false,
]);

if (!$fleetArrayValidationResult['isValid']) {
$firstValidationError = $fleetArrayValidationResult['errors'][0];
if (!$fleetArrayParsingResult['isValid']) {
$firstValidationError = $fleetArrayParsingResult['errors'][0];
$errorMessage = FlightControl\Utils\Errors\mapFleetArrayValidationErrorToReadableMessage($firstValidationError);

messageRed($errorMessage, $ErrorTitle);
Expand Down
2 changes: 1 addition & 1 deletion modules/flightControl/utils/errors/fleetArray.utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace UniEngine\Engine\Modules\FlightControl\Utils\Errors;

/**
* @param object $error As returned by FlightControl\Utils\Validators\validateFleetArray
* @param object $error As returned by FlightControl\Utils\Validators\parseFleetArray
*/
function mapFleetArrayValidationErrorToReadableMessage($error) {
global $_Lang;
Expand Down
13 changes: 10 additions & 3 deletions modules/flightControl/utils/validators/fleetArray.validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@
* - Allows ships count to be prettified
* - Allows ships count to be 0
*/
function validateFleetArray ($props) {
function parseFleetArray ($props) {
global $_Vars_ElementCategories;

$isValid = function () {
$isValid = function ($payload) {
return [
'isValid' => true,
'payload' => $payload,
];
};
$isInvalid = function ($errors) {
Expand All @@ -33,6 +34,8 @@ function validateFleetArray ($props) {
false
);

$parsedFleet = [];

foreach ($fleet as $inputShipId => $inputShipCount) {
$shipId = intval($inputShipId);

Expand Down Expand Up @@ -75,10 +78,14 @@ function validateFleetArray ($props) {
]);
}

$parsedFleet[$shipId] = $shipCount;

continue;
}

return $isValid();
return $isValid([
'parsedFleet' => $parsedFleet,
]);
}

?>

0 comments on commit 3a4916f

Please sign in to comment.