Skip to content

Commit

Permalink
Add DayOfWeek::parse() and DayOfWeek::from().
Browse files Browse the repository at this point in the history
  • Loading branch information
gnutix committed Sep 30, 2023
1 parent e95ae99 commit 91cfd90
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 0 deletions.
33 changes: 33 additions & 0 deletions src/DayOfWeek.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

namespace Brick\DateTime;

use Brick\DateTime\Parser\DateTimeParseException;
use Brick\DateTime\Parser\DateTimeParser;
use Brick\DateTime\Parser\DateTimeParseResult;
use Brick\DateTime\Parser\IsoParsers;
use JsonSerializable;

/**
Expand Down Expand Up @@ -52,6 +56,35 @@ public static function of(int $dayOfWeek): DayOfWeek
return DayOfWeek::get($dayOfWeek);
}

/**
* @throws DateTimeException If the day-of-week is not valid.
* @throws DateTimeParseException If required fields are missing from the result.
*/
public static function from(DateTimeParseResult $result): DayOfWeek
{
return DayOfWeek::of(
(int) $result->getField(Field\DayOfWeek::NAME),
);
}

/**
* Obtains an instance of `DayOfWeek` from a text string.
*
* @param string $text The text to parse, such as `2007-W48`.
* @param DateTimeParser|null $parser The parser to use, defaults to the ISO 8601 parser.
*
* @throws DateTimeException If the day-of-week is not valid.
* @throws DateTimeParseException If the text string does not follow the expected format.
*/
public static function parse(string $text, ?DateTimeParser $parser = null): DayOfWeek
{
if (! $parser) {
$parser = IsoParsers::dayOfWeek();
}

return DayOfWeek::from($parser->parse($text));
}

/**
* Returns the current day-of-week in the given time-zone, according to the given clock.
*
Expand Down
5 changes: 5 additions & 0 deletions src/Field/DayOfWeek.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ final class DayOfWeek
*/
public const NAME = 'day-of-week';

/**
* The regular expression pattern of the ISO 8601 representation.
*/
public const PATTERN = '[0-9]{2}';

/**
* @param int $dayOfWeek The day-of-week to check.
*
Expand Down
18 changes: 18 additions & 0 deletions src/Parser/IsoParsers.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Brick\DateTime\Parser;

use Brick\DateTime\Field\DayOfMonth;
use Brick\DateTime\Field\DayOfWeek;
use Brick\DateTime\Field\FractionOfSecond;
use Brick\DateTime\Field\HourOfDay;
use Brick\DateTime\Field\MinuteOfHour;
Expand Down Expand Up @@ -241,6 +242,23 @@ public static function monthDay(): PatternParser
->toParser();
}

/**
* Returns a parser for a day-of-week such as `03`.
*/
public static function dayOfWeek(): PatternParser
{
/** @var PatternParser|null $parser */
static $parser;

if ($parser) {
return $parser;
}

return $parser = (new PatternParserBuilder())
->appendCapturePattern(DayOfWeek::PATTERN, DayOfWeek::NAME)
->toParser();
}

/**
* Returns a parser for a time-zone offset such as `Z` or `+01:00`.
*/
Expand Down
24 changes: 24 additions & 0 deletions tests/DayOfWeekTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -309,4 +309,28 @@ public function providerToString(): array
[DayOfWeek::SUNDAY, 'Sunday'],
];
}

/**
* @dataProvider providerParse
*
* @param string $string The parse string.
* @param int $expectedDayOfWeek The day-of-week value, from 1 to 7.
*/
public function testParse(string $string, int $expectedDayOfWeek): void
{
self::assertDayOfWeekIs($expectedDayOfWeek, DayOfWeek::parse($string));
}

public function providerParse(): array
{
return [
['01', 1],
['02', 2],
['03', 3],
['04', 4],
['05', 5],
['06', 6],
['07', 7],
];
}
}

0 comments on commit 91cfd90

Please sign in to comment.