fix set date

This commit is contained in:
nasser committed 2022-04-11 11:38:07 +04:30
1 parent 69a397ba33
commit 8ef16c1106
5 files changed
+24 -41

No files matched your search

+1 -1
View File
@@ -220,7 +220,7 @@ $v = $v->timezone('Asia/Baku');
<pre> <pre>
$v = $v->setDateTime(1395, 4, 25, 16, 50, 42); $v = $v->setDateTime(1395, 4, 25, 16, 50, 42);
$v = $v->setDateTime(1395, 4, 25, 16, 50, 42, 1569856); $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'); $v = $v->setTimeString('12:25:48');
</pre> </pre>
<h2 id="format">Format</h2> <h2 id="format">Format</h2>
+1 -1
View File
@@ -225,7 +225,7 @@ $v = $v->timezone('Asia/Baku');
<pre> <pre>
$v = $v->setDateTime(1395, 4, 25, 16, 50, 42); $v = $v->setDateTime(1395, 4, 25, 16, 50, 42);
$v = $v->setDateTime(1395, 4, 25, 16, 50, 42, 1569856); $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'); $v = $v->setTimeString('12:25:48');
</pre> </pre>
<h2 id="format">قالب نمایش</h2> <h2 id="format">قالب نمایش</h2>
+20 -37
View File
@@ -13,12 +13,10 @@ trait Accessor
* Get a part of the Verta object * Get a part of the Verta object
* *
* @param string $name * @param string $name
* * @return string|int
* @throws InvalidArgumentException *@throws InvalidArgumentException
*
* @return string|int|DateTimeZone
*/ */
public function __get($name) public function __get(string $name)
{ {
static $formats = [ static $formats = [
'year' => 'Y', 'year' => 'Y',
@@ -35,29 +33,25 @@ trait Accessor
'timestamp' => 'U', 'timestamp' => 'U',
]; ];
switch (true) { if(array_key_exists($name, $formats)) {
case isset($formats[$name]): return (int) $this->format($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));
} }
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 * Check if an attribute exists on the object
* *
* @param string $name * @param string $name
*
* @return bool * @return bool
*/ */
public function __isset($name) public function __isset(string $name)
{ {
try { try {
$this->__get($name); $this->__get($name);
@@ -71,12 +65,11 @@ trait Accessor
/** /**
* Set a part of the Verta object * Set a part of the Verta object
* *
* @param string $name * @param string $name
* @param string|int|\DateTimeZone $value * @param \DateTimeZone|int|string $value
*
* @throws \InvalidArgumentException * @throws \InvalidArgumentException
*/ */
public function __set($name, $value) public function __set(string $name, DateTimeZone|int|string $value)
{ {
switch ($name) { switch ($name) {
case 'year': case 'year':
@@ -111,7 +104,6 @@ trait Accessor
* Set the instance's year * Set the instance's year
* *
* @param int $value * @param int $value
*
* @return static * @return static
*/ */
public function year($value) public function year($value)
@@ -125,7 +117,6 @@ trait Accessor
* Set the instance's month * Set the instance's month
* *
* @param int $value * @param int $value
*
* @return static * @return static
*/ */
public function month($value) public function month($value)
@@ -139,7 +130,6 @@ trait Accessor
* Set the instance's day * Set the instance's day
* *
* @param int $value * @param int $value
*
* @return static * @return static
*/ */
public function day($value) public function day($value)
@@ -153,7 +143,6 @@ trait Accessor
* Set the instance's hour * Set the instance's hour
* *
* @param int $value * @param int $value
*
* @return static * @return static
*/ */
public function hour($value) public function hour($value)
@@ -167,7 +156,6 @@ trait Accessor
* Set the instance's minute * Set the instance's minute
* *
* @param int $value * @param int $value
*
* @return static * @return static
*/ */
public function minute($value) public function minute($value)
@@ -181,7 +169,6 @@ trait Accessor
* Set the instance's second * Set the instance's second
* *
* @param int $value * @param int $value
*
* @return static * @return static
*/ */
public function second($value) public function second($value)
@@ -195,7 +182,6 @@ trait Accessor
* Set the instance's timestamp * Set the instance's timestamp
* *
* @param int $value * @param int $value
*
* @return static * @return static
*/ */
public function timestamp($value) public function timestamp($value)
@@ -230,7 +216,7 @@ trait Accessor
*/ */
public function setDateTime($year, $month, $day, $hour, $minute, $second = 0, $microseconds = 0) 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 * @return Verta
*/ */
#[ReturnTypeWillChange] public function setDateJalali(int $year, int $month, int $day): Verta
public function setDate($year, $month, $day)
{ {
if (static::isValidDate($year, $month, $day)) { if (static::isValidDate($year, $month, $day)) {
list($year, $month, $day) = self::getGregorian($year, $month, $day); list($year, $month, $day) = self::getGregorian($year, $month, $day);
@@ -258,12 +243,10 @@ trait Accessor
* Set the time by time string * Set the time by time string
* *
* @param string $time * @param string $time
* * @return Verta
* @return static
*
* @throws \InvalidArgumentException * @throws \InvalidArgumentException
*/ */
public function setTimeString($time) public function setTimeString(string $time): Verta
{ {
$time = explode(':', $time); $time = explode(':', $time);
+1 -1
View File
@@ -133,7 +133,7 @@ trait Boundaries
{ {
$month = $this->quarter * static::MONTHS_PER_QUARTER; $month = $this->quarter * static::MONTHS_PER_QUARTER;
return $this->setDate($this->year, $month, 1)->endMonth(); return $this->setDateJalali($this->year, $month, 1)->endMonth();
} }
/** /**
+1 -1
View File
@@ -99,7 +99,7 @@ class AccessTest extends TestCase
public function testSetDate() public function testSetDate()
{ {
$datetime = verta()->setDate(1397, 10, 11); $datetime = verta()->setDateJalali(1397, 10, 11);
$this->assertEquals(1397, $datetime->year); $this->assertEquals(1397, $datetime->year);
$this->assertEquals(10, $datetime->month); $this->assertEquals(10, $datetime->month);