How to use a Presenter class
Super simple!
class BookPresenter extends Presenter
{
public function __construct(
public Book $book
) {}
public function price()
{
return number_format($this->book->price, 2);
}
}
You can also add it to your model
class Book
{
public function present()
{
return new BookPresenter($this);
}
}
// usage
$book->present()->price();
You can even use a package to supercharge your presenters : https://github.com/jamierumbelow/presenters