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>
$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');
</pre>
<h2 id="format">Format</h2>
+1 -1
View File
@@ -225,7 +225,7 @@ $v = $v->timezone('Asia/Baku');
<pre>
$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');
</pre>
<h2 id="format">قالب نمایش</h2>
+20 -37
View File
@@ -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);
+1 -1
View File
@@ -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();
}
/**
+1 -1
View File
@@ -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);