diff --git a/README.md b/README.md index 7b85b0c..30bbf46 100644 --- a/README.md +++ b/README.md @@ -644,6 +644,20 @@ public function diff(Jalalian $ref): array $diff = (new Jalalian(1397, 5, 24))->diff(new Jalalian(1398, 6, 30)); // output: [1, 1, 6] +``` +--- +```php +public function next(string $dayName):Jalalian +$next = (new Jalalian(1403, 5, 22))->next('شنبه')->format('Y-m-d'); +// output: 1403-05-27 + +``` +--- +```php +public function previous(string $dayName):Jalalian +$previous = (new Jalalian(1403, 5, 22))->previous('شنبه')->format('Y-m-d'); +// output: 1403-05-20 + ``` ### CalendarUtils --- diff --git a/src/Converter.php b/src/Converter.php index 8208a21..78ca211 100644 --- a/src/Converter.php +++ b/src/Converter.php @@ -2,6 +2,7 @@ namespace Morilog\Jalali; +use Carbon\CarbonInterface; use Carbon\Exceptions\UnitException; use Date; @@ -141,4 +142,29 @@ public function toFormattedMonthYearString() return $this->format('F Y'); } + /** + * Change persian day name to CarbonInterface + * + * @param string $modifier + * + * @return int + */ + public function toCarbonDayName(string $modifier): int + { + $carbonDayModifiers = [ + 'شنبه' => CarbonInterface::SATURDAY, + 'یکشنبه' => CarbonInterface::SUNDAY, + 'دوشنبه' => CarbonInterface::MONDAY, + 'سه‌شنبه' => CarbonInterface::TUESDAY, + 'چهارشنبه' => CarbonInterface::WEDNESDAY, + 'پنج‌شنبه' => CarbonInterface::THURSDAY, + 'جمعه' => CarbonInterface::FRIDAY, + ]; + + if (!isset($carbonDayModifiers[$modifier])) { + throw new \InvalidArgumentException('Modifier expected among: شنبه, یکشنبه, دوشنبه, سه‌شنبه, چهارشنبه, پنج‌شنبه و جمعه.'); + } + + return $carbonDayModifiers[$modifier]; + } } diff --git a/src/Jalalian.php b/src/Jalalian.php index e859a63..75738ab 100644 --- a/src/Jalalian.php +++ b/src/Jalalian.php @@ -125,7 +125,7 @@ public static function fromDateTime($dateTime, \DateTimeZone $timeZone = null): return static::fromCarbon(new Carbon($dateTime, $timeZone)); } - + public function getFirstDayOfWeek(): Jalalian { return (new static( @@ -785,4 +785,22 @@ public function diff(Jalalian $ref): array return [$yearDiff, $monthDiff, $dayDiff]; } + + /** + * @param string $dayName One of: شنبه, یکشنبه, دوشنبه, سه‌شنبه, چهارشنبه, پنج‌شنبه, جمعه + * @return Jalalian + */ + public function next(string $dayName): Jalalian + { + return $this->fromCarbon($this->toCarbon()->next($this->toCarbonDayName($dayName))); + } + + /** + * @param string $dayName One of: شنبه, یکشنبه, دوشنبه, سه‌شنبه, چهارشنبه, پنج‌شنبه, جمعه + * @return Jalalian + */ + public function previous(string $dayName): Jalalian + { + return $this->fromCarbon($this->toCarbon()->previous($this->toCarbonDayName($dayName))); + } } diff --git a/tests/JalalianTest.php b/tests/JalalianTest.php index 3e308ab..3c22047 100644 --- a/tests/JalalianTest.php +++ b/tests/JalalianTest.php @@ -426,4 +426,16 @@ public function testdiff() //greater year, greater month, greater day $this->assertEquals($jDate->diff(new Jalalian(1405, 9, 30)), [4, 3, 4]); } + + public function testNext() + { + $jDate = new Jalalian(1403, 05, 28); + $this->assertEquals('1403-05-30', $jDate->next('سه‌شنبه')->format('Y-m-d')); + } + + public function testPrevious() + { + $jDate = new Jalalian(1403, 05, 28); + $this->assertEquals('1403-05-22', $jDate->previous('دوشنبه')->format('Y-m-d')); + } }