From 8ef16c110606d5af5f7983af5b729392b7e2ffe5 Mon Sep 17 00:00:00 2001 From: nasser Date: Mon, 11 Apr 2022 11:38:07 +0430 Subject: [PATCH] fix set date --- docs/en/index.html | 2 +- docs/index.html | 2 +- src/Traits/Accessor.php | 57 ++++++++++++++------------------------- src/Traits/Boundaries.php | 2 +- tests/AccessTest.php | 2 +- 5 files changed, 24 insertions(+), 41 deletions(-) diff --git a/docs/en/index.html b/docs/en/index.html index d939e17..ddbef7b 100644 --- a/docs/en/index.html +++ b/docs/en/index.html @@ -220,7 +220,7 @@ $v = $v->timezone('Asia/Baku');
 $v = $v->setDateTime(1395, 4, 25, 16, 50, 42);
 $v = $v->setDateTime(1395, 4, 25, 16, 50, 42, 1569856);
-$v = $v->setDate(1395, 4, 25);
+$v = $v->setDateJalali(1395, 4, 25);
 $v = $v->setTimeString('12:25:48');
 

Format

diff --git a/docs/index.html b/docs/index.html index 20c2614..c4429c1 100644 --- a/docs/index.html +++ b/docs/index.html @@ -225,7 +225,7 @@ $v = $v->timezone('Asia/Baku');
 $v = $v->setDateTime(1395, 4, 25, 16, 50, 42);
 $v = $v->setDateTime(1395, 4, 25, 16, 50, 42, 1569856);
-$v = $v->setDate(1395, 4, 25);
+$v = $v->setDateJalali(1395, 4, 25);
 $v = $v->setTimeString('12:25:48');
 

قالب نمایش

diff --git a/src/Traits/Accessor.php b/src/Traits/Accessor.php index 583c38f..4d8db4f 100644 --- a/src/Traits/Accessor.php +++ b/src/Traits/Accessor.php @@ -13,12 +13,10 @@ trait Accessor * Get a part of the Verta object * * @param string $name - * - * @throws InvalidArgumentException - * - * @return string|int|DateTimeZone + * @return string|int + *@throws InvalidArgumentException */ - public function __get($name) + public function __get(string $name) { static $formats = [ 'year' => 'Y', @@ -35,29 +33,25 @@ trait Accessor 'timestamp' => 'U', ]; - switch (true) { - case isset($formats[$name]): - return (int) $this->format($formats[$name]); - - case $name === 'quarter': - return (int) ceil($this->month / static::MONTHS_PER_QUARTER); - - case $name === 'timezone': - return $this->getTimezone()->getName(); - - default: - throw new InvalidArgumentException(sprintf("Unknown getter '%s'", $name)); + if(array_key_exists($name, $formats)) { + return (int) $this->format($formats[$name]); } + elseif ($name === 'quarter') { + return (int) ceil($this->month / static::MONTHS_PER_QUARTER); + } + elseif ($name === 'timezone') { + return $this->getTimezone()->getName(); + } + throw new InvalidArgumentException(sprintf("Unknown getter '%s'", $name)); } /** * Check if an attribute exists on the object * * @param string $name - * * @return bool */ - public function __isset($name) + public function __isset(string $name) { try { $this->__get($name); @@ -71,12 +65,11 @@ trait Accessor /** * Set a part of the Verta object * - * @param string $name - * @param string|int|\DateTimeZone $value - * + * @param string $name + * @param \DateTimeZone|int|string $value * @throws \InvalidArgumentException */ - public function __set($name, $value) + public function __set(string $name, DateTimeZone|int|string $value) { switch ($name) { case 'year': @@ -111,7 +104,6 @@ trait Accessor * Set the instance's year * * @param int $value - * * @return static */ public function year($value) @@ -125,7 +117,6 @@ trait Accessor * Set the instance's month * * @param int $value - * * @return static */ public function month($value) @@ -139,7 +130,6 @@ trait Accessor * Set the instance's day * * @param int $value - * * @return static */ public function day($value) @@ -153,7 +143,6 @@ trait Accessor * Set the instance's hour * * @param int $value - * * @return static */ public function hour($value) @@ -167,7 +156,6 @@ trait Accessor * Set the instance's minute * * @param int $value - * * @return static */ public function minute($value) @@ -181,7 +169,6 @@ trait Accessor * Set the instance's second * * @param int $value - * * @return static */ public function second($value) @@ -195,7 +182,6 @@ trait Accessor * Set the instance's timestamp * * @param int $value - * * @return static */ public function timestamp($value) @@ -230,7 +216,7 @@ trait Accessor */ public function setDateTime($year, $month, $day, $hour, $minute, $second = 0, $microseconds = 0) { - return $this->setDate($year, $month, $day)->setTime($hour, $minute, $second, $microseconds); + return $this->setDateJalali($year, $month, $day)->setTime($hour, $minute, $second, $microseconds); } /** @@ -243,8 +229,7 @@ trait Accessor * * @return Verta */ - #[ReturnTypeWillChange] - public function setDate($year, $month, $day) + public function setDateJalali(int $year, int $month, int $day): Verta { if (static::isValidDate($year, $month, $day)) { list($year, $month, $day) = self::getGregorian($year, $month, $day); @@ -258,12 +243,10 @@ trait Accessor * Set the time by time string * * @param string $time - * - * @return static - * + * @return Verta * @throws \InvalidArgumentException */ - public function setTimeString($time) + public function setTimeString(string $time): Verta { $time = explode(':', $time); diff --git a/src/Traits/Boundaries.php b/src/Traits/Boundaries.php index 7805207..4f3d402 100644 --- a/src/Traits/Boundaries.php +++ b/src/Traits/Boundaries.php @@ -133,7 +133,7 @@ trait Boundaries { $month = $this->quarter * static::MONTHS_PER_QUARTER; - return $this->setDate($this->year, $month, 1)->endMonth(); + return $this->setDateJalali($this->year, $month, 1)->endMonth(); } /** diff --git a/tests/AccessTest.php b/tests/AccessTest.php index 43e4205..ad8369c 100644 --- a/tests/AccessTest.php +++ b/tests/AccessTest.php @@ -99,7 +99,7 @@ class AccessTest extends TestCase public function testSetDate() { - $datetime = verta()->setDate(1397, 10, 11); + $datetime = verta()->setDateJalali(1397, 10, 11); $this->assertEquals(1397, $datetime->year); $this->assertEquals(10, $datetime->month);