Example #1
0
 /**
  * Obtains an instance of `YearMonth` from a text string.
  *
  * @param string              $text   The text to parse, such as `2007-12`.
  * @param DateTimeParser|null $parser The parser to use, defaults to the ISO 8601 parser.
  *
  * @return YearMonth
  *
  * @throws DateTimeException      If the date is not valid.
  * @throws DateTimeParseException If the text string does not follow the expected format.
  */
 public static function parse($text, DateTimeParser $parser = null)
 {
     if (!$parser) {
         $parser = IsoParsers::yearMonth();
     }
     return YearMonth::from($parser->parse($text));
 }
Example #2
0
 /**
  * {@inheritdoc}
  */
 public function expand($className, $value, array $options = [])
 {
     try {
         switch ($className) {
             case Duration::class:
                 return Duration::parse($value);
             case LocalDate::class:
                 return LocalDate::parse($value);
             case LocalTime::class:
                 return LocalTime::parse($value);
             case LocalDateTime::class:
                 return LocalDateTime::parse($value);
             case TimeZoneOffset::class:
                 return TimeZoneOffset::parse($value);
             case TimeZoneRegion::class:
                 return TimeZoneRegion::parse($value);
             case YearMonth::class:
                 return YearMonth::parse($value);
             case ZonedDateTime::class:
                 return ZonedDateTime::parse($value);
         }
     } catch (DateTimeParseException $e) {
         throw new ObjectNotConvertibleException($e->getMessage(), $e->getCode(), $e);
     }
     return null;
 }
Example #3
0
 public function testToString()
 {
     $this->assertSame('2013-09', (string) YearMonth::of(2013, 9));
 }
Example #4
0
 /**
  * Resolves the date, resolving days past the end of month.
  *
  * @param integer $year  The year to represent, validated as an integer from MIN_YEAR to MAX_YEAR.
  * @param integer $month The month-of-year to represent, validated as an integer from 1 to 12.
  * @param integer $day   The day-of-month to represent, validated as an integer from 1 to 31.
  *
  * @return LocalDate
  */
 private function resolvePreviousValid($year, $month, $day)
 {
     if ($day > 28) {
         $day = min($day, YearMonth::of($year, $month)->getLengthOfMonth());
     }
     return new LocalDate($year, $month, $day);
 }
Example #5
0
 /**
  * Combines this year with a month to create a YearMonth.
  *
  * @param integer $month The month-of-year to use, from 1 to 12.
  *
  * @return YearMonth
  *
  * @throws DateTimeException If the month is invalid.
  */
 public function atMonth($month)
 {
     return YearMonth::of($this->year, $month);
 }
Example #6
0
 /**
  * @param integer   $year      The expected year.
  * @param integer   $month     The expected month.
  * @param YearMonth $yearMonth The year-month to test.
  */
 protected function assertYearMonthIs($year, $month, YearMonth $yearMonth)
 {
     $this->compare([$year, $month], [$yearMonth->getYear(), $yearMonth->getMonth()]);
 }