Created
October 30, 2018 11:31
-
-
Save Itach1Uchixa/55718ae3d4ce75fcd8ef1200778091bf to your computer and use it in GitHub Desktop.
Value objects
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* @author Kakhramonov Javlonbek <kakjavlon@gmail.com> | |
* @copyright 2018 Foreach.Soft Ltd. (http://each.uz) | |
*/ | |
declare(strict_types=1); | |
namespace Acme\Value; | |
use DateTimeImmutable; | |
use DateTimeInterface; | |
use Acme\Exception\InvalidArgumentException; | |
class Date | |
{ | |
public const DEFAULT_DATE_FORMAT = 'Y-m-d'; | |
/** | |
* @var DateTimeImmutable | |
*/ | |
protected $value; | |
/** | |
* Date constructor. | |
* | |
* @param DateTimeImmutable $value | |
*/ | |
protected function __construct(DateTimeImmutable $value) | |
{ | |
$this->value = $value; | |
} | |
/** | |
* Creates date object from DateTimeInterface | |
* note that only date information will be extracted! | |
* note that timezone of given object will be used | |
* | |
* @param DateTimeInterface $date | |
* | |
* @return Date | |
*/ | |
public static function fromDateTime(DateTimeInterface $date): self | |
{ | |
/** @noinspection PhpUnhandledExceptionInspection */ | |
$immutable = new DateTimeImmutable($date->format('c')); | |
return new static($immutable->setTime(0, 0, 0, 0)); | |
} | |
/** | |
* @see DateTimeImmutable::createFromFormat() | |
* @param string $date | |
* @param string $format | |
* | |
* @return Date | |
* | |
* @throws InvalidArgumentException | |
*/ | |
public static function fromFormat(string $date, string $format = self::DEFAULT_DATE_FORMAT): self | |
{ | |
/** @noinspection PhpUnhandledExceptionInspection */ | |
$immutable = DateTimeImmutable::createFromFormat($format, $date); | |
if ($immutable === false) { | |
throw new InvalidArgumentException("Invalid date string or format given."); | |
} | |
return new static($immutable->setTime(0, 0, 0, 0)); | |
} | |
/** | |
* @param Date $to | |
* | |
* @return bool | |
*/ | |
public function equals(Date $to): bool | |
{ | |
return $this->toString() === $to->toString(); | |
} | |
/** | |
* @param string $format | |
* | |
* @return string | |
* | |
* @throws InvalidArgumentException | |
*/ | |
public function format(string $format): string | |
{ | |
$result = $this->toDateTimeImmutable()->format($format); | |
if ($result === false) { | |
throw new InvalidArgumentException("Invalid format string given"); | |
} | |
return $result; | |
} | |
/** | |
* @inheritDoc | |
*/ | |
public function __toString() | |
{ | |
return $this->toString(); | |
} | |
/** | |
* @return string | |
*/ | |
public function toString(): string | |
{ | |
/** @noinspection PhpUnhandledExceptionInspection */ | |
return $this->format(self::DEFAULT_DATE_FORMAT); | |
} | |
/** | |
* @return DateTimeImmutable | |
*/ | |
public function toDateTimeImmutable(): DateTimeImmutable | |
{ | |
return $this->value; | |
} | |
/** | |
* @return int | |
*/ | |
public function getDay(): int | |
{ | |
return (int) $this->toDateTimeImmutable()->format('j'); | |
} | |
/** | |
* @return int | |
*/ | |
public function getMonth(): int | |
{ | |
return (int) $this->toDateTimeImmutable()->format('n'); | |
} | |
/** | |
* @return int | |
*/ | |
public function getYear(): int | |
{ | |
return (int) $this->toDateTimeImmutable()->format('Y'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment