add getter and setter

This commit is contained in:
Nasser Hekmati committed 2017-06-04 19:10:37 +04:30
1 parent d39bfc1a03
commit 21c72c2245
2 files changed
+317 -13

No files matched your search

+59
View File
@@ -81,7 +81,66 @@ return Verta::createJalali(1394,12,29,15,51,5); // 1394-12-29 15:51:05
return Verta::createJalaliDate(1394,12,29); // 1394-12-29 11:18:29 set time now return Verta::createJalaliDate(1394,12,29); // 1394-12-29 11:18:29 set time now
return Verta::createJalaliTime(15,51,5); // 1395-12-14 15:51:05 set date now return Verta::createJalaliTime(15,51,5); // 1395-12-14 15:51:05 set date now
``` ```
---
#### Geter
```php
//get each part of the time
$v = verta();
return $v->year; // 1396
return $v->month; // 3
return $v->day; // 14
return $v->hour; // 14
return $v->minute; // 18
return $v->second; // 23
return $v->timestamp; // 1496557661
return $v->timezone; // Asia/Tehran
```
---
#### Setter
```php
//set each part of the time
$v = verta();
$v->year = 1395;
$v->month = 4; // set 13 for next year first month
$v->day = 25;
$v->hour = 16;
$v->minute = 50;
$v->second = 42;
$v->timestamp = 1496557661;
$v->timezone = 'Asia/Baku';
//set each part of the time with method
$v = $v->year(1395)
$v = $v->month(4) // set 13 for next year first month
$v = $v->day(25)
$v = $v->hour(16)
$v = $v->minute(50)
$v = $v->second(42)
$v = $v->timestamp(1496557661)
$v = $v->timezone('Asia/Baku');
//set date and time
$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->setTimeString('12:25:48');
```
---
#### Isset
```php
$v = verta();
echo isset($v->year); // true
echo isset($v->month); // true
echo empty($v->day); // false
echo empty($v->hour); // false
echo empty($v->minute); // false
echo empty($v->second); // false
echo isset($v->timestamp); // true
echo isset($v->timezone); // true
```
--- ---
#### Formating #### Formating
```php ```php
+258 -13
View File
@@ -17,13 +17,6 @@ use DatePeriod;
use InvalidArgumentException; use InvalidArgumentException;
use Exception; use Exception;
/********************************* CONFIG *********************************/
/**
* set Default timezone
*/
date_default_timezone_set('Asia/Tehran');
class Verta extends DateTime { class Verta extends DateTime {
/***************************** STATCT VARIABLE ****************************/ /***************************** STATCT VARIABLE ****************************/
@@ -235,14 +228,14 @@ class Verta extends DateTime {
* @param bool $timezone [optional] * @param bool $timezone [optional]
*/ */
public function __construct($datetime = null, $timezone = null) { public function __construct($datetime = null, $timezone = null) {
date_default_timezone_set('Asia/Tehran');
if ($datetime === null) { if ($datetime === null) {
$instance = time(); $instance = time();
} }
elseif (is_string($datetime)){ elseif (is_string($datetime)){
$object = date_create($datetime); $object = date_create($datetime);
if ($object === false) { if ($object === false) {
throw new InvalidArgumentException('Unknown datetime ('.$datetime.')'); throw new InvalidArgumentException(sprintf("Unknown datetime '%s'", $datetime));
} }
$instance = $object->getTimestamp(); $instance = $object->getTimestamp();
} }
@@ -253,7 +246,7 @@ class Verta extends DateTime {
$instance = $datetime; $instance = $datetime;
} }
else { else {
throw new \InvalidArgumentException('Unknown datetime ('.$datetime.')'); throw new \InvalidArgumentException(sprintf("Unknown datetime '%s'", $datetime));
} }
$timezone = static::createTimeZone($timezone); $timezone = static::createTimeZone($timezone);
parent::__construct(date('Y-m-d H:i:s.u', $instance), $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); return new static($datetime);
} }
else{ 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)); $object = @timezone_open(strval($timezone));
if ($object === false) { if ($object === false) {
throw new InvalidArgumentException('Unknown timezone ('.$timezone.')'); throw new InvalidArgumentException(sprintf("Unknown timezone '%s'", $timezone));
} }
return $object; return $object;
@@ -566,10 +559,262 @@ class Verta extends DateTime {
return static::createJalali(null, null, null, $hour, $minute, $second, $timezone); 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 ****************************/ /***************************** STRING FORMATED ****************************/
/** /**
* Reset the format used to the default when type juggling a Carbon instance to a string * Reset the format used to the default when type juggling a Carbon instance to a string
*/ */