Created
February 6, 2024 09:04
-
-
Save diloabininyeri/dd7dc9e261e5e0ec8dfde673b407ffce to your computer and use it in GitHub Desktop.
Entity,ValueObject and Aggregate in the php(DDD)
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 | |
class BookEntity | |
{ | |
public function __construct(private int $id, private string $title) | |
{ | |
} | |
public function getId(): int | |
{ | |
return $this->id; | |
} | |
public function getTitle(): string | |
{ | |
return $this->title; | |
} | |
} | |
readonly class BookValueObject | |
{ | |
public function __construct(private string $author) | |
{ | |
} | |
public function getAuthor(): string | |
{ | |
return $this->author; | |
} | |
} | |
readonly class BookAggregator | |
{ | |
public function __construct(private BookEntity $bookEntity, private BookValueObject $bookValueObject) | |
{ | |
} | |
public function getBookDetails(): string | |
{ | |
$bookDetails = "Book Title: {$this->bookEntity->getTitle()}"; | |
$bookDetails .= "\nBook Author: {$this->bookValueObject->getAuthor()}"; | |
return $bookDetails; | |
} | |
} | |
$bookAggregator = new BookAggregator( | |
new BookEntity(1, 'Domain-Driven Design'), | |
new BookValueObject('Eric Evans') | |
); | |
echo $bookAggregator->getBookDetails(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment