/** * testing validateCurrencyCodeString method. */ public function testValidateCurrencyCodesString() { $this->assertTrue(Utils::validateCurrencyCodesString('HUF'), 'ValidateCurrencyCodesString: HUF is invalid currency code.'); $this->assertTrue(Utils::validateCurrencyCodesString('HUF,EUR'), 'ValidateCurrencyCodesString: HUF,EUR are invalid currency codes.'); $this->assertFalse(Utils::validateCurrencyCodesString('HUFMuf'), 'ValidateCurrencyCodesString: HUFMuf is valid currency code.'); $this->assertFalse(Utils::validateCurrencyCodesString('HU2'), 'ValidateCurrencyCodesString: HU2 is valid currency code.'); $this->assertFalse(Utils::validateCurrencyCodesString('HUF|EUR'), 'ValidateCurrencyCodesString: "|" is valid currency code seperator character.'); }
/** * Validate the option fields. * * @param mixed $options the option fields which will be validated. * @throws MissingMandatoryParametersException * * @return mixed|boolean with the $options array or false if there was invalid arguments. */ private function validateOptions($options) { // If the startDate is not setted or it is empty then throw exception if (!isset($options['startDate']) || empty($options['startDate'])) { $this->logger->error(sprintf('[|%s] Start date is missing in the argument list.', Utils::getClassBasename($this))); throw new MissingMandatoryParametersException('The "startDate" parameter is missing in the argument list!'); } elseif ($options['startDate'] > date('Y-m-d')) { // If the start date is future then return with empty array $this->logger->alert(sprintf('[|%s] Start date is a future date in the argument list.', Utils::getClassBasename($this))); return false; } elseif (!Utils::validateDate($options['startDate'], 'Y-m-d')) { $this->logger->alert(sprintf('[|%s] The start option is in invalid date format.', Utils::getClassBasename($this))); return false; } // If the endDate is not setted or it is empty then set today if (!isset($options['endDate']) || empty($options['endDate'])) { $options['endDate'] = date('Y-m-d'); } elseif (!Utils::validateDate($options['endDate'], 'Y-m-d')) { $this->logger->alert(sprintf('[|%s] The end option is in invalid date format.', Utils::getClassBasename($this))); return false; } // If the currencyNames is not set in the options array or it is empty, it is gotten the currencies from the DB if (!isset($options['currencyNames']) || empty($options['currencyNames'])) { $options['currencyNames'] = implode(',', $this->em->getRepository('OpitOpitHrmCurrencyRateBundle:Currency')->getAllCurrencyCodes()); } elseif (!Utils::validateCurrencyCodesString($options['currencyNames'])) { $this->logger->alert(sprintf('[|%s] The currency option is in invalid format.', Utils::getClassBasename($this))); return false; } else { $options['currencyNames'] = strtoupper($options['currencyNames']); } return $options; }
/** * Validate the command input options * Validate the dates and currency codes, if these are not valid then return with false. * * @param mixin $inputOptions * @param Logger $this->logger * @param Symfony\Component\Console\Output\OutputInterface $output * @return boolean|mixin return false if the command options are not valid else return with options */ protected function validateCommandOptions($inputOptions, $output) { $options = array(); $this->logger = $this->getContainer()->get('logger'); if ($inputOptions['start']) { if (!Utils::validateDate($inputOptions['start'], 'Y-m-d')) { $output->writeln('<error>The start date is invalid:</error> ' . $inputOptions['start'] . '. <comment>Correct format:</comment> 2014-01-20'); $this->logger->alert(sprintf('[|%s] The start option is in invalid date format.', Utils::getClassBasename($this))); exit(0); } $options['startDate'] = $inputOptions['start']; } else { // If the start option is missing and the it wasn't marked as a required options. if (!(isset($this->isNotRequiredOptions['start']) && true === $this->isNotRequiredOptions['start'])) { $output->writeln('<error>The start date is misssing</error>.' . 'Please use the <comment>--start</comment> option'); $this->logger->error(sprintf('[|%s] The start option is missing.', Utils::getClassBasename($this))); exit(0); } } if ($inputOptions['end']) { if (!Utils::validateDate($inputOptions['end'], 'Y-m-d')) { $output->writeln('<error>The end date is invalid:</error> ' . $inputOptions['end'] . '. <comment>Correct format:</comment> 2014-01-20'); $this->logger->alert(sprintf('[|%s] The end option is in invalid date format.', Utils::getClassBasename($this))); exit(0); } $options['endDate'] = $inputOptions['end']; } if ($inputOptions['currency']) { if (!Utils::validateCurrencyCodesString($inputOptions['currency'])) { $output->writeln('<error>The currency codes are invalid:</error> ' . $inputOptions['currency'] . '. <comment>Correct format:</comment> EUR,USD'); $this->logger->alert(sprintf('[|%s] The currency option is in invalid format.', Utils::getClassBasename($this))); exit(0); } $options['currencyNames'] = strtoupper($inputOptions['currency']); } return $options; }