Building Specifications

Using Specifications in our Services might be the best example to illustrate how to use Factories within our Services.

Consider the following Service example. Given a request from the outside world, we want to build a feed based on the latest Posts added to the system:

namespace Application\Service;use Domain\Model\Post;use Domain\Model\PostRepository;class LatestPostsFeedService {    private $postRepository;    public function __construct(PostRepository $postRepository)     {        $this->postRepository = $postRepository;    }    /**     * @param LatestPostsFeedRequest $request     */    public function execute($request)     {        $posts = $this->postRepository->latestPosts($request->since);        return array_map(function(Post $post) {            return [ 'id' => $post->id()->id(), ...

Get Domain-Driven Design in PHP now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.