/** * testing validateDate method. */ public function testValidateDate() { $this->assertTrue(Utils::validateDate(date('2014-01-01'), 'Y-m-d'), 'ValidateDate: invalid date.'); $this->assertTrue(Utils::validateDate(date('2014-01-01 15:34:56')), 'ValidateDate: invalid datetime.'); $this->assertFalse(Utils::validateDate(date('2014-02-30'), 'Y-m-d'), 'ValidateDate: valid date.'); $this->assertFalse(Utils::validateDate(date('2014-02-30 25:02:78')), 'ValidateDate valid datetime.'); $this->assertTrue(Utils::validateDate(date('2014/03/10 12:35:10'), 'Y/m/d H:i:s'), 'ValidateDate: The given datetime and its format are not covenant.'); $this->assertFalse(Utils::validateDate(date('2014/03/10'), 'Y-m-d H:i:s'), 'ValidateDate: The given date and its format are covenant.'); }
/** * 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; }
/** * @internal */ public function setExchangeRates(array $exchangeRates) { $this->logger->info(sprintf('[|%s] Starting set exchange rates by manually.', Utils::getClassBasename($this))); $currencyRates = array(); // Get the existing currency codes. $currencyCodes = $this->em->getRepository('OpitOpitHrmCurrencyRateBundle:Currency')->getAllCurrencyCodes(); foreach ($exchangeRates as $currencyCode => $dates) { // If the currency code exist then go forward else skip out it. if (in_array($currencyCode, $currencyCodes)) { // Iterate the dates foreach ($dates as $date => $value) { // If date is valid then save it with the value else skip it. if (Utils::validateDate($date, 'Y-m-d')) { $currencyRates[strtoupper($currencyCode)][$date] = (double) \str_replace(',', '.', $value); } else { $this->logger->alert(sprintf('This %s is not a valid date format.', $date)); } } } else { $this->logger->alert(sprintf('This %s currency code does not exist.', $currencyCode)); } } $this->currencyRates = $currencyRates; }