Skip to Content
Domain-Driven Design in PHP
book

Domain-Driven Design in PHP

by Keyvan Akbary, Carlos Buenosvinos, Christian Soronellas
June 2017
Intermediate to advanced
394 pages
8h 52m
English
Packt Publishing
Content preview from Domain-Driven Design in PHP

DateTimes

Because DateTimes are widely used in Entities, we think it's important to point out specific approaches on unit testing Entities that have fields with date types. Consider that a Post is new if it was created within the last 15 days:

class Post{    const NEW_TIME_INTERVAL_DAYS = 15;    // ...    private $createdAt;    public function __construct($aContent, $title)    {        // ...        $this->createdAt(new DateTimeImmutable());    }    // ...    public function isNew()    {        return            (new DateTimeImmutable())                 ->diff($this->createdAt)                 ->days <= self::NEW_TIME_INTERVAL_DAYS;    }}

The isNew() method needs to compare two DateTimes; it's a comparison between the date when the Post was created and today's date. We compute the difference and check if it's less than the specified ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

PHP Web Services

PHP Web Services

Lorna Jane Mitchell
PHP Microservices

PHP Microservices

Pablo Solar Vilariño, Carlos Pérez Sánchez

Publisher Resources

ISBN: 9781787284944