/** * Checks parameters and returns validation messages * * @param array $data * @param array $config * @return string */ public function validate(array $data, array $config) { $validationMessages = ''; $userAction = null; if (!empty($data)) { $userAction = $data[0]; array_shift($data); } if (isset($userAction) && isset($config[$userAction])) { // parse the expected parameters of the action $matcher = new RouteMatcher($config[$userAction]['options']['route']); $parts = $matcher->getParts(); array_shift($parts); $expectedParams = []; foreach ($parts as $part) { $expectedParams[$part['name']] = $part; } // parse user parameters $userParams = $this->parseUserParams($data); $validationMessages = $this->validateParameters($expectedParams, $userParams); // add usage message $usages = ConsoleController::getCommandUsage(); $validationMessages .= 'Usage:' . PHP_EOL . "{$userAction} "; $validationMessages .= $usages[$userAction] . PHP_EOL . PHP_EOL; } else { if (!is_null($userAction)) { $validationMessages .= PHP_EOL . "Unknown action name '{$userAction}'." . PHP_EOL . PHP_EOL; } else { $validationMessages .= PHP_EOL . "No action is given in the command." . PHP_EOL . PHP_EOL; } $validationMessages .= 'Available options: ' . PHP_EOL; foreach (array_keys($config) as $action) { $validationMessages .= $action . PHP_EOL; } $validationMessages .= PHP_EOL; } return $validationMessages; }
/** * Checks parameters and returns validation messages * * @param array $data * @param array $config * @return string */ public function validate(array $data, array $config) { $validationMessages = ''; $userAction = null; if (!empty($data)) { $userAction = $data[0]; array_shift($data); } if (isset($userAction) && isset($config[$userAction])) { // parse the expected parameters of the action $matcher = new RouteMatcher($config[$userAction]['options']['route']); $parts = $matcher->getParts(); array_shift($parts); $expectedParams = []; foreach ($parts as $part) { $expectedParams[$part['name']] = $part; } // parse user parameters $userParams = $this->parseUserParams($data); $missingParams = $this->checkMissingParameter($expectedParams, $userParams); $extraParams = $this->checkExtraParameter($expectedParams, $userParams); $missingValues = $this->checkMissingValue($expectedParams, $userParams); $extraValues = $this->checkExtraValue($expectedParams, $userParams); $validationMessages = PHP_EOL; if (!empty($missingParams)) { $validationMessages .= 'Missing required parameters:' . PHP_EOL; foreach ($missingParams as $missingParam) { $validationMessages .= $missingParam . PHP_EOL; } $validationMessages .= PHP_EOL; } if (!empty($extraParams)) { $validationMessages .= 'Unidentified parameters:' . PHP_EOL; foreach ($extraParams as $extraParam) { $validationMessages .= $extraParam . PHP_EOL; } $validationMessages .= PHP_EOL; } if (!empty($missingValues)) { $validationMessages .= 'Parameters missing value:' . PHP_EOL; foreach ($missingValues as $missingValue) { $validationMessages .= $missingValue . PHP_EOL; } $validationMessages .= PHP_EOL; } if (!empty($extraValues)) { $validationMessages .= 'Parameters that don\'t need value:' . PHP_EOL; foreach ($extraValues as $extraValue) { $validationMessages .= $extraValue . PHP_EOL; } $validationMessages .= PHP_EOL; } if (empty($missingParams) && empty($extraParams) && empty($missingValues) && empty($extraValue)) { $validationMessages .= 'Please make sure parameters are in correct format and are not repeated.'; $validationMessages .= PHP_EOL . PHP_EOL; } // add usage message $usages = ConsoleController::getCommandUsage(); $validationMessages .= 'Usage:' . PHP_EOL . "{$userAction} "; $validationMessages .= $usages[$userAction] . PHP_EOL . PHP_EOL; } else { if (!is_null($userAction)) { $validationMessages .= PHP_EOL . "Unknown action name '{$userAction}'." . PHP_EOL . PHP_EOL; } else { $validationMessages .= PHP_EOL . "No action is given in the command." . PHP_EOL . PHP_EOL; } $validationMessages .= 'Available options: ' . PHP_EOL; foreach (array_keys($config) as $action) { $validationMessages .= $action . PHP_EOL; } $validationMessages .= PHP_EOL; } return $validationMessages; }