Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
Total | |
100.00% |
1 / 1 |
|
100.00% |
9 / 9 |
CRAP | |
100.00% |
27 / 27 |
TimeOffset | |
100.00% |
1 / 1 |
|
100.00% |
9 / 9 |
11 | |
100.00% |
27 / 27 |
__construct | |
100.00% |
1 / 1 |
1 | |
100.00% |
4 / 4 |
|||
buildFromDateTimeInterface | |
100.00% |
1 / 1 |
1 | |
100.00% |
2 / 2 |
|||
getValue | |
100.00% |
1 / 1 |
1 | |
100.00% |
1 / 1 |
|||
isDst | |
100.00% |
1 / 1 |
1 | |
100.00% |
1 / 1 |
|||
getAbbreviation | |
100.00% |
1 / 1 |
1 | |
100.00% |
1 / 1 |
|||
withValue | |
100.00% |
1 / 1 |
1 | |
100.00% |
3 / 3 |
|||
withDst | |
100.00% |
1 / 1 |
1 | |
100.00% |
3 / 3 |
|||
withAbbreviation | |
100.00% |
1 / 1 |
1 | |
100.00% |
3 / 3 |
|||
buildTimeZone | |
100.00% |
1 / 1 |
3 | |
100.00% |
9 / 9 |
<?php | |
namespace Popy\Calendar\ValueObject; | |
use DateTimeZone; | |
use DateTimeInterface; | |
/** | |
* Represents the time offset induced by timezones and day saving time. | |
* It is an immutable object. | |
* | |
* A TimeOffset MAY NOT be aware of its properties (value, dst status, | |
* abbreviation, usually when it's been built by a parser). | |
*/ | |
class TimeOffset | |
{ | |
/** | |
* Offset value. | |
* | |
* @var integer|null | |
*/ | |
protected $value; | |
/** | |
* Day saving time status. | |
* | |
* @var boolean|null | |
*/ | |
protected $dst; | |
/** | |
* Offset abbreviation. | |
* | |
* @var string|null | |
*/ | |
protected $abbreviation; | |
/** | |
* Class constructor. | |
* | |
* @param integer|null $value Offset value. | |
* @param boolean|null $dst Day saving time status. | |
* @param string|null $abbreviation Offset abbreviation. | |
*/ | |
public function __construct($value = null, $dst = null, $abbreviation = null) | |
{ | |
$this->value = $value; | |
$this->dst = $dst; | |
$this->abbreviation = $abbreviation; | |
} | |
/** | |
* Instanciates a TimeOffset from a DateTimeInterface, using the format | |
* method to extract properties. | |
* | |
* @param DateTimeInterface $date | |
* | |
* @return static | |
*/ | |
public static function buildFromDateTimeInterface(DateTimeInterface $date) | |
{ | |
$parts = explode('|', $date->format('Z|I|T')); | |
return new static((int)$parts[0], (bool)$parts[1], $parts[2]); | |
} | |
/** | |
* Gets the offset value. | |
* | |
* @return integer|null | |
*/ | |
public function getValue() | |
{ | |
return $this->value; | |
} | |
/** | |
* Checks day saving time status. | |
* | |
* @return boolean|null | |
*/ | |
public function isDst() | |
{ | |
return $this->dst; | |
} | |
/** | |
* Gets the Offset abbreviation.. | |
* | |
* @return string|null | |
*/ | |
public function getAbbreviation() | |
{ | |
return $this->abbreviation; | |
} | |
/** | |
* Gets a new TimeOffset instance with the input value. | |
* | |
* @param integer|null $value | |
* | |
* @return static | |
*/ | |
public function withValue($value) | |
{ | |
$res = clone $this; | |
$res->value = $value; | |
return $res; | |
} | |
/** | |
* Gets a new TimeOffset instance with the input dst. | |
* | |
* @param boolean|null $dst | |
* | |
* @return static | |
*/ | |
public function withDst($dst) | |
{ | |
$res = clone $this; | |
$res->dst = $dst; | |
return $res; | |
} | |
/** | |
* Gets a new TimeOffset instance with the input abbreviation. | |
* | |
* @param string|null $abbreviation | |
* | |
* @return static | |
*/ | |
public function withAbbreviation($abbreviation) | |
{ | |
$res = clone $this; | |
$res->abbreviation = $abbreviation; | |
return $res; | |
} | |
/** | |
* Build a DateTimeZone object based on TimeOffset properties, if possible. | |
* | |
* @return DateTimeZone|null | |
*/ | |
public function buildTimeZone() | |
{ | |
if (null !== $this->value) { | |
$sign = $this->value < 0 ? '-' : '+'; | |
$value = intval(abs($this->value) / 60); | |
return new DateTimeZone(sprintf( | |
'%s%02d:%02d', | |
$sign, | |
intval($value / 60), | |
$value % 60 | |
)); | |
} | |
} | |
} |