Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make ScheduledDate a proper value object #7

Closed
matthiasnoback opened this issue May 11, 2017 · 1 comment
Closed

Make ScheduledDate a proper value object #7

matthiasnoback opened this issue May 11, 2017 · 1 comment

Comments

@matthiasnoback
Copy link
Owner

And make a point about construction from \DateTimeImmutable in the infrastructure layer (another assignment)?

@matthiasnoback
Copy link
Owner Author

Something like?

final class ScheduledDate
{
    private const DATE_TIME_FORMAT = \DateTime::ATOM;

    /**
     * @var string
     */
    private $dateTime;

    private function __construct(string $dateTime)
    {
        $this->dateTime = $dateTime;
    }

    public static function fromPhpDateString(string $phpDateString): ScheduledDate
    {
        try {
            $dateTimeImmutable = new \DateTimeImmutable($phpDateString);
        } catch (\Throwable $throwable) {
            throw new \InvalidArgumentException(
                'Invalid PHP date time format',
                null,
                $throwable
            );
        }

        return self::fromDateTime($dateTimeImmutable);
    }

    public static function fromDateTime(\DateTimeImmutable $dateTime): ScheduledDate
    {
        return new self($dateTime->format(self::DATE_TIME_FORMAT));
    }

    public function __toString()
    {
        return $this->dateTime;
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant