/** * Checks if the given date is a real date or datepart. * Returns false if a expected datepart is missing or a datepart exceeds its possible border. * But the check will only be done for the expected dateparts which are given by format. * If no format is given the standard dateformat for the actual locale is used. * f.e. 30.February.2007 will return false if format is 'dd.MMMM.YYYY' * * @param string|array|\Zend\Date\Date $date Date to parse for correctness * @param string $format (Optional) Format for parsing the date string * @param string|\Zend\Locale\Locale $locale (Optional) Locale for parsing date parts * @return boolean True when all date parts are correct */ public static function isDate($date, $format = null, $locale = null) { if (!is_string($date) && !is_numeric($date) && !$date instanceof Date && !is_array($date)) { return false; } if ($format !== null && $format != 'ee' && $format != 'ss' && $format != 'GG' && $format != 'MM' && $format != 'EE' && $format != 'TT' && Locale::isLocale($format)) { $locale = $format; $format = null; } $locale = Locale::findLocale($locale); if ($format === null) { $format = Format::getDateFormat($locale); } else { if (self::$_options['format_type'] == 'php' && !defined($format)) { $format = Format::convertPhpToIsoFormat($format); } } $format = self::_getLocalizedToken($format, $locale); if (!is_array($date)) { try { $parsed = Format::getDate($date, array('locale' => $locale, 'date_format' => $format, 'format_type' => 'iso', 'fix_date' => false)); } catch (\Zend\Locale\Exception $e) { // Date can not be parsed return false; } } else { $parsed = $date; } if ((strpos($format, 'Y') !== false or strpos($format, 'y') !== false) and !isset($parsed['year'])) { // Year expected but not found return false; } if (strpos($format, 'M') !== false and !isset($parsed['month'])) { // Month expected but not found return false; } if (strpos($format, 'd') !== false and !isset($parsed['day'])) { // Day expected but not found return false; } if ((strpos($format, 'H') !== false or strpos($format, 'h') !== false) and !isset($parsed['hour'])) { // Hour expected but not found return false; } if (strpos($format, 'm') !== false and !isset($parsed['minute'])) { // Minute expected but not found return false; } if (strpos($format, 's') !== false and !isset($parsed['second'])) { // Second expected but not found return false; } // Set not given dateparts if (isset($parsed['hour']) === false) { $parsed['hour'] = 12; } if (isset($parsed['minute']) === false) { $parsed['minute'] = 0; } if (isset($parsed['second']) === false) { $parsed['second'] = 0; } if (isset($parsed['month']) === false) { $parsed['month'] = 1; } if (isset($parsed['day']) === false) { $parsed['day'] = 1; } if (isset($parsed['year']) === false) { $parsed['year'] = 1970; } if (self::isYearLeapYear($parsed['year'])) { $parsed['year'] = 1972; } else { $parsed['year'] = 1971; } $date = new self($parsed, null, $locale); $timestamp = $date->mktime($parsed['hour'], $parsed['minute'], $parsed['second'], $parsed['month'], $parsed['day'], $parsed['year']); if ($parsed['year'] != $date->date('Y', $timestamp)) { // Given year differs from parsed year return false; } if ($parsed['month'] != $date->date('n', $timestamp)) { // Given month differs from parsed month return false; } if ($parsed['day'] != $date->date('j', $timestamp)) { // Given day differs from parsed day return false; } if ($parsed['hour'] != $date->date('G', $timestamp)) { // Given hour differs from parsed hour return false; } if ($parsed['minute'] != $date->date('i', $timestamp)) { // Given minute differs from parsed minute return false; } if ($parsed['second'] != $date->date('s', $timestamp)) { // Given second differs from parsed second return false; } return true; }
/** * test setOption * expected boolean */ public function testSetOption() { $this->assertEquals(8, count(Format::setOptions(array('format_type' => 'php')))); $this->assertTrue(is_array(Format::setOptions())); try { $this->assertTrue(Format::setOptions(array('format_type' => 'xxx'))); $this->fail("exception expected"); } catch (InvalidArgumentException $e) { // success } try { $this->assertTrue(Format::setOptions(array('myformat' => 'xxx'))); $this->fail("exception expected"); } catch (InvalidArgumentException $e) { // success } $format = Format::setOptions(array('locale' => 'de', 'number_format' => Format::STANDARD)); $test = Cldr::getContent('de', 'decimalnumber'); $this->assertEquals($test, $format['number_format']); try { $this->assertFalse(Format::setOptions(array('number_format' => array('x' => 'x')))); $this->fail("exception expected"); } catch (InvalidArgumentException $e) { // success } $format = Format::setOptions(array('locale' => 'de', 'date_format' => Format::STANDARD)); $test = Format::getDateFormat('de'); $this->assertEquals($test, $format['date_format']); try { $this->assertFalse(Format::setOptions(array('date_format' => array('x' => 'x')))); $this->fail("exception expected"); } catch (InvalidArgumentException $e) { // success } try { $this->assertFalse(is_array(Format::setOptions(array('fix_date' => 'no')))); $this->fail("exception expected"); } catch (InvalidArgumentException $e) { // success } $format = Format::setOptions(array('locale' => Format::STANDARD)); $locale = new Locale(); $this->assertEquals($locale->toString(), $format['locale']); try { $this->assertFalse(is_array(Format::setOptions(array('locale' => 'nolocale')))); $this->fail("exception expected"); } catch (InvalidArgumentException $e) { // success } try { $this->assertFalse(is_array(Format::setOptions(array('precision' => 50)))); $this->fail("exception expected"); } catch (InvalidArgumentException $e) { // success } // test interaction between class-wide default date format and using locale's default format try { $result = array('date_format' => 'MMM d, y', 'locale' => 'en_US', 'month' => '7', 'day' => '4', 'year' => '2007'); Format::setOptions(array('format_type' => 'iso', 'date_format' => 'MMM d, y', 'locale' => 'en_US')); // test setUp } catch (InvalidArgumentException $e) { $this->fail("exception expected"); } try { // uses global date_format with global locale $this->assertSame($result, Format::getDate('July 4, 2007')); } catch (InvalidArgumentException $e) { $this->fail("exception expected"); } try { // uses global date_format with given locale $this->assertSame($result, Format::getDate('July 4, 2007', array('locale' => 'en_US'))); } catch (InvalidArgumentException $e) { $this->fail("exception expected"); } try { // sets a new global date format Format::setOptions(array('date_format' => 'M-d-y')); } catch (InvalidArgumentException $e) { $this->fail("exception expected"); } try { // uses global date format with given locale // but this date format differs from the original set... (MMMM d, yyyy != M-d-y) $expected = Format::getDate('July 4, 2007', array('locale' => 'en_US')); $this->assertSame($expected['year'], $result['year']); $this->assertSame($expected['month'], $result['month']); $this->assertSame($expected['day'], $result['day']); } catch (InvalidArgumentException $e) { $this->fail("exception expected"); } try { // the following should not be used... instead of null, Locale::ZFDEFAULT should be used // uses given local with standard format from this locale $this->assertSame($result, Format::getDate('July 4, 2007', array('locale' => 'en_US', 'date_format' => null))); } catch (InvalidArgumentException $e) { $this->fail("exception expected"); } try { // uses given locale with standard format from this locale $this->assertSame($result, Format::getDate('July 4, 2007', array('locale' => 'en_US', 'date_format' => Format::STANDARD))); } catch (InvalidArgumentException $e) { $this->fail("exception expected"); } try { // uses standard locale with standard format from this locale $expect = Format::getDate('July 4, 2007', array('locale' => Format::STANDARD)); $testlocale = new Locale(); $this->assertEquals($testlocale->toString(), $expect['locale']); } catch (InvalidArgumentException $e) { $this->fail("exception expected"); } Format::setOptions(array('date_format' => null, 'locale' => null)); // test tearDown }