Skip to content

Instantly share code, notes, and snippets.

@naosim
Last active September 25, 2017 12:56
Show Gist options
  • Save naosim/b0ef146d683da5b86bbff393444d94be to your computer and use it in GitHub Desktop.
Save naosim/b0ef146d683da5b86bbff393444d94be to your computer and use it in GitHub Desktop.
ValueObject
<?php
declare(strict_types=1);
class StringVO {
protected $value; // string
function __construct(string $value) {
$this->value = $value;
}
public function getValue(): string {
return $this->value;
}
}
class DateTimeVO {
protected $value; // DateTime
function __construct(DateTime $value) {
$value->setTimeZone(new DateTimeZone('Asia/Tokyo'));
$this->value = $value;
}
public function getValue(): DateTime {
return $this->value;
}
public function getISO8601Value(): String {
return $this->value->format(DateTime::ISO8601);
}
public function getUnixTimestampValue(): int {
return $this->value->getTimestamp();
}
public function getApiValue(): array {
return array(
"ISO8601" => $this->getISO8601Value(),
"unix" => $this->getUnixTimestampValue()
);
}
public function getDbValue(): int {
return $this->value->getTimestamp();
}
}
class UnixTimestampVO {
protected $value; // DateTime
function __construct(int $value) {
$this->value = $value;
}
public function getValue(): int {
return $this->value;
}
public function toDateTime(): DateTime {
return new DateTime('@' . $this->getValue());
}
}
class CurrentDateTime extends DateTimeVO {
}
class DateTimeFactory {
private $datetime;
function __construct() {
$this->datetime = new DateTime();
}
function createDateTime(): DateTime {
return $this->datetime;
}
function createCurrentDateTime(): CurrentDateTime {
return new CurrentDateTime($this->datetime);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment