add getter and setter
This commit is contained in:
1 parent
d39bfc1a03
commit
21c72c2245
2 files changed
+317
-13
No files matched your search
+258
-13
@@ -17,13 +17,6 @@ use DatePeriod;
|
||||
use InvalidArgumentException;
|
||||
use Exception;
|
||||
|
||||
|
||||
/********************************* CONFIG *********************************/
|
||||
/**
|
||||
* set Default timezone
|
||||
*/
|
||||
date_default_timezone_set('Asia/Tehran');
|
||||
|
||||
class Verta extends DateTime {
|
||||
|
||||
/***************************** STATCT VARIABLE ****************************/
|
||||
@@ -235,14 +228,14 @@ class Verta extends DateTime {
|
||||
* @param bool $timezone [optional]
|
||||
*/
|
||||
public function __construct($datetime = null, $timezone = null) {
|
||||
|
||||
date_default_timezone_set('Asia/Tehran');
|
||||
if ($datetime === null) {
|
||||
$instance = time();
|
||||
}
|
||||
elseif (is_string($datetime)){
|
||||
$object = date_create($datetime);
|
||||
if ($object === false) {
|
||||
throw new InvalidArgumentException('Unknown datetime ('.$datetime.')');
|
||||
throw new InvalidArgumentException(sprintf("Unknown datetime '%s'", $datetime));
|
||||
}
|
||||
$instance = $object->getTimestamp();
|
||||
}
|
||||
@@ -253,7 +246,7 @@ class Verta extends DateTime {
|
||||
$instance = $datetime;
|
||||
}
|
||||
else {
|
||||
throw new \InvalidArgumentException('Unknown datetime ('.$datetime.')');
|
||||
throw new \InvalidArgumentException(sprintf("Unknown datetime '%s'", $datetime));
|
||||
}
|
||||
$timezone = static::createTimeZone($timezone);
|
||||
parent::__construct(date('Y-m-d H:i:s.u', $instance), $timezone);
|
||||
@@ -347,7 +340,7 @@ class Verta extends DateTime {
|
||||
return new static($datetime);
|
||||
}
|
||||
else{
|
||||
throw new \InvalidArgumentException('Unknown datetime ('.$datetime.')');
|
||||
throw new InvalidArgumentException(sprintf("Unknown datetime '%s'", $datetime));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -428,7 +421,7 @@ class Verta extends DateTime {
|
||||
}
|
||||
$object = @timezone_open(strval($timezone));
|
||||
if ($object === false) {
|
||||
throw new InvalidArgumentException('Unknown timezone ('.$timezone.')');
|
||||
throw new InvalidArgumentException(sprintf("Unknown timezone '%s'", $timezone));
|
||||
}
|
||||
|
||||
return $object;
|
||||
@@ -566,10 +559,262 @@ class Verta extends DateTime {
|
||||
return static::createJalali(null, null, null, $hour, $minute, $second, $timezone);
|
||||
}
|
||||
|
||||
/***************************** ACCESS ****************************/
|
||||
|
||||
/**
|
||||
* Get a part of the Verta object
|
||||
*
|
||||
* @param string $name
|
||||
*
|
||||
* @throws \InvalidArgumentException
|
||||
*
|
||||
* @return string|int|\DateTimeZone
|
||||
*/
|
||||
public function __get($name)
|
||||
{
|
||||
switch (true) {
|
||||
case array_key_exists($name, $formats = array(
|
||||
'year' => 'Y',
|
||||
'month' => 'n',
|
||||
'day' => 'j',
|
||||
'hour' => 'G',
|
||||
'minute' => 'i',
|
||||
'second' => 's',
|
||||
'micro' => 'u',
|
||||
'dayOfWeek' => 'w',
|
||||
'dayOfYear' => 'z',
|
||||
'weekOfYear' => 'W',
|
||||
'daysInMonth' => 't',
|
||||
'timestamp' => 'U',
|
||||
)):
|
||||
return (int) $this->format($formats[$name]);
|
||||
|
||||
case $name === 'timezone':
|
||||
return $this->getTimezone()->getName();
|
||||
|
||||
default:
|
||||
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)
|
||||
{
|
||||
try {
|
||||
$this->__get($name);
|
||||
} catch (InvalidArgumentException $e) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set a part of the Verta object
|
||||
*
|
||||
* @param string $name
|
||||
* @param string|int|\DateTimeZone $value
|
||||
*
|
||||
* @throws \InvalidArgumentException
|
||||
*/
|
||||
public function __set($name, $value)
|
||||
{
|
||||
switch ($name) {
|
||||
case 'year':
|
||||
case 'month':
|
||||
case 'day':
|
||||
case 'hour':
|
||||
case 'minute':
|
||||
case 'second':
|
||||
list($year, $month, $day, $hour, $minute, $second) = explode('-', $this->format('Y-n-j-G-i-s'));
|
||||
$$name = $value;
|
||||
$this->setDateTime($year, $month, $day, $hour, $minute, $second);
|
||||
break;
|
||||
|
||||
case 'timestamp':
|
||||
$this->setTimestamp($value);
|
||||
break;
|
||||
|
||||
case 'timezone':
|
||||
case 'tz':
|
||||
$this->setTimezone(new DateTimeZone($value));
|
||||
break;
|
||||
|
||||
default:
|
||||
throw new InvalidArgumentException(sprintf("Unknown setter '%s'", $name));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the instance's year
|
||||
*
|
||||
* @param int $value
|
||||
*
|
||||
* @return static
|
||||
*/
|
||||
public function year($value)
|
||||
{
|
||||
$this->year = $value;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the instance's month
|
||||
*
|
||||
* @param int $value
|
||||
*
|
||||
* @return static
|
||||
*/
|
||||
public function month($value)
|
||||
{
|
||||
$this->month = $value;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the instance's day
|
||||
*
|
||||
* @param int $value
|
||||
*
|
||||
* @return static
|
||||
*/
|
||||
public function day($value)
|
||||
{
|
||||
$this->day = $value;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the instance's hour
|
||||
*
|
||||
* @param int $value
|
||||
*
|
||||
* @return static
|
||||
*/
|
||||
public function hour($value)
|
||||
{
|
||||
$this->hour = $value;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the instance's minute
|
||||
*
|
||||
* @param int $value
|
||||
*
|
||||
* @return static
|
||||
*/
|
||||
public function minute($value)
|
||||
{
|
||||
$this->minute = $value;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the instance's second
|
||||
*
|
||||
* @param int $value
|
||||
*
|
||||
* @return static
|
||||
*/
|
||||
public function second($value)
|
||||
{
|
||||
$this->second = $value;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the instance's timestamp
|
||||
*
|
||||
* @param int $value
|
||||
*
|
||||
* @return static
|
||||
*/
|
||||
public function timestamp($value)
|
||||
{
|
||||
return parent::setTimestamp($value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Alias for setTimezone()
|
||||
*
|
||||
* @param \DateTimeZone|string $value
|
||||
*
|
||||
* @return static
|
||||
*/
|
||||
public function timezone($value)
|
||||
{
|
||||
return parent::setTimezone(new DateTimeZone($value));
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the date and time all together
|
||||
*
|
||||
* @param int $year
|
||||
* @param int $month
|
||||
* @param int $day
|
||||
* @param int $hour
|
||||
* @param int $minute
|
||||
* @param int $second
|
||||
*
|
||||
* @return static
|
||||
*/
|
||||
public function setDateTime($year, $month, $day, $hour, $minute, $second = 0, $microseconds = 0)
|
||||
{
|
||||
return $this->setDate($year, $month, $day)->setTime($hour, $minute, $second, $microseconds);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the current date of the DateTime object to a different date.
|
||||
* Calls modify as a workaround for a php bug
|
||||
*
|
||||
* @param int $year
|
||||
* @param int $month
|
||||
* @param int $day
|
||||
*
|
||||
* @return static
|
||||
*/
|
||||
public function setDate($year, $month, $day)
|
||||
{
|
||||
list($year, $month, $day) = self::getGregorian($year, $month, $day);
|
||||
|
||||
return parent::setDate($year, $month, $day);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the time by time string
|
||||
*
|
||||
* @param string $time
|
||||
*
|
||||
* @return static
|
||||
*
|
||||
* @throws \InvalidArgumentException
|
||||
*/
|
||||
public function setTimeString($time)
|
||||
{
|
||||
$time = explode(':', $time);
|
||||
|
||||
$hour = $time[0];
|
||||
$minute = isset($time[1]) ? $time[1] : 0;
|
||||
$second = isset($time[2]) ? $time[2] : 0;
|
||||
|
||||
return $this->setTime($hour, $minute, $second, 0);
|
||||
}
|
||||
|
||||
/***************************** STRING FORMATED ****************************/
|
||||
|
||||
|
||||
/**
|
||||
* Reset the format used to the default when type juggling a Carbon instance to a string
|
||||
*/
|
||||
|
||||
Reference in new issue
Block a user