PHPackages                             deefour/presenter - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Templating &amp; Views](/categories/templating)
4. /
5. deefour/presenter

ActiveLibrary[Templating &amp; Views](/categories/templating)

deefour/presenter
=================

Presenters/Decorators for PHP Objects

3.0.1(8y ago)122.5k1MITPHPPHP &gt;=5.5.0

Since Nov 23Pushed 7y ago1 watchersCompare

[ Source](https://github.com/deefour/presenter)[ Packagist](https://packagist.org/packages/deefour/presenter)[ Docs](https://github.com/deefour/presenter)[ RSS](/packages/deefour-presenter/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (5)Versions (27)Used By (0)

Presenter
=========

[](#presenter)

[![Build Status](https://camo.githubusercontent.com/2790f39516e3bb2f6ce91e2307e8e98d39da41b454ce6d8854df6872a89b8d00/68747470733a2f2f7472617669732d63692e6f72672f646565666f75722f70726573656e7465722e737667)](https://travis-ci.org/deefour/presenter)[![Total Downloads](https://camo.githubusercontent.com/82283007408c5b5ad02449e5d921b9dfdea5e7f3bb948abc9756dbae64fd35ff/68747470733a2f2f706f7365722e707567782e6f72672f646565666f75722f70726573656e7465722f642f746f74616c2e737667)](https://packagist.org/packages/deefour/presenter)[![Latest Stable Version](https://camo.githubusercontent.com/04e10ca82274b2daa169e7249b978e29c332350add10d94ac1b5ccab2cf4cc0a/68747470733a2f2f706f7365722e707567782e6f72672f646565666f75722f70726573656e7465722f762f737461626c652e737667)](https://packagist.org/packages/deefour/presenter)[![License](https://camo.githubusercontent.com/8cb25b84699d705d5ddb0fbd5091126a933f7661b2a92eabc7531083a2486d73/68747470733a2f2f706f7365722e707567782e6f72672f646565666f75722f70726573656e7465722f6c6963656e73652e737667)](https://packagist.org/packages/deefour/presenter)

Object-oriented presentation logic.

Getting Started
---------------

[](#getting-started)

Run the following to add Presenter to your project's `composer.json`. See [Packagist](https://packagist.org/packages/deefour/presenter) for specific versions.

```
composer require deefour/presenter
```

**`>=PHP5.5.0` is required.**

### The Resolver

[](#the-resolver)

The `Deefour\Presenter\Resolver` determines the FQN of a presenter class associated with an object. The default behavior of the resolver is to append `'Presenter'` to the `Article` FQN.

```
use Deefour\Presenter\Resolver;

(new Resolver)->presenter(new Article); //=> 'ArticlePresenter'
```

This behavior can be customized by passing a callable to the resolver.

```
use Deefour\Presenter\Resolver;

$resolver = new Resolver;

$resolver->resolveWith(function ($instance) {
  return "App\Presenters\" . get_class($instance) . 'Presenter';
});

$resolver->presenter(new Article); //=> 'App\Presenters\ArticlePresenter'
```

The resolver will look for a `modelClass()` method on an object. If found, the returned FQN will be used instead of the object itself.

```
class BlogPost
{
  static public function modelClass()
  {
      return Article::class;
  }
}

(new Resolver)->presenter(new BlogPost); //=> 'ArticlePresenter'
```

### Instantiation

[](#instantiation)

Instantiating an instance of a presenter is your responsibility.

```
use Deefour\Presenter\Resolver;

$article       = new Article;
$presenterName = (new Resolver)->presenter($article);
$presenter     = new $presenterName($article);
```

```
use Deefour\Presenter\Resolver;

(new Resolver)->presenter(new Article); //=> 'BlogPresenter'
```

If the resulting FQN from the resolver does not match an existing, valid class name, `null` will be returned or a `NotDefinedException` will be thrown.

```
use Deefour\Presenter\Resolver;

(new Resolver)->presenter(new ObjectWithoutPresenter); //=> null
(new Resolver)->presenterOrFail(new ObjectWithoutPresenter); //=> throws NotDefinedException
```

Presenters
----------

[](#presenters)

The presenters themselves extend `Deefour\Presenter\Presenter`.

```
use Deefour\Presenter\Presenter;

class ArticlePresenter extends Presenter
{
    public function isDraft()
    {
        return $this->_model->isDraft() ? 'Yes' : 'No';
    }
}
```

### The API

[](#the-api)

A quick overview of the API available.

```
use Deefour\Producer\Factory;

$presenter = (new Factory)->make(new Article, 'presenter'); //=> ArticlePolicy

$presenter->_model; //=> Article

$presenter->_model->isDraft(); //=> false
$presenter->isDraft(); //=> 'No'
$presenter->is_draft; //=> 'No'

$presenter->_model()->published; //=> true
$presenter->published; //=> true
```

A few things to notice:

- The underlying object decorated by the presenter can be accessed via the `$_model` property or `model()` method.
- Any property or method publicly accessible on the underlying object can also be accessed directly through the presenter.
- Any publicly accessible, camel-cased method on the presenter or underlying model can be accessed via snake-cased property access.

### Automatic Presenter Resolution

[](#automatic-presenter-resolution)

When a property or method is resolved through the `__get()` or `__call()` methods on the presenter, an attempt will be made to resolve and wrap the return value in a presenter too.

```
namespace App;

use Illuminate\Support\Collection;

class Article
{
    public function category()
    {
        return new Category;
    }

    public function tags()
    {
        $collection = new Collection;

        $collection->push(new Tag);
        $collection->push(new Tag);
        $collection->push(new Tag);

        return $collection;
    }
}
```

Given the existence of `ArticlePresenter`, `CategoryPresenter`, and `TagPresenter`, the following will be returned

```
use Deefour\Presenter\Resolver;

$presenter = (new Resolver)->presenter(new Article); //=> ArticlePresenter

(new $presenter)->category;      //=> CategoryPresenter
(new $presenter)->tags->first(); //=> TagPresenter
```

> **Note:** The collection resolution works by looking for an instance of `IteratorAggregate`. The iterator is used to loop through the collection and generate presenters for each item. An attempt is then made to instantiate a new instance of the original object implementing `IteratorAggregate`. **That** is the return value.

If you want access to the raw association, simply request it from the underlying object.

```
$presenter->_model->tags()->first(); //=> Tag
```

Contribute
----------

[](#contribute)

- Issue Tracker:
- Source Code:

Changelog
---------

[](#changelog)

#### 3.0.1 - November 7, 2017

[](#301---november-7-2017)

- Check for the existence of a method on the underlying modely before checking if it's a property. Fixes a conflict with Laravel's `__isset()` implementation on `Illuminate\Database\Eloquent\Model`.

#### 3.0.0 - July 20, 2017

[](#300---july-20-2017)

- The resolver no longer accepts an object during instantiation. Instead, objects are passed directly to the `presenter()` and `presenterOrFail()` methods.
- A new `resolveWith()` method on the resolver accepts a callable to customize resolution.
- Support for the `presenterClass()` has been removed from the resolver in favor of the new `resolveWith()` method *on* the resolver. You can pass a callable with your existing `presenterClass()` logic to the resolver instead.
- Model access should only be done through the new `model()` method. Access to `_model` has been disabled.

#### 2.0.0 - February 12, 2017

[](#200---february-12-2017)

- Replaced `Factory` with new `Resolver` class.
- Removed dependency on [`deefour\producer`](https://github.com/deefour/producer)
- Removed `Presentable` contract
- Simplified `README.md`

#### 1.0.0 - October 7, 2015

[](#100---october-7-2015)

- Release 1.0.0.

#### 0.8.0 - August 8, 2015

[](#080---august-8-2015)

- Compat changes for updates to [`deefour/producer`](https://github.com/deefour/producer).
- New `Factory` class is available to prevent the need to interact directly with the factory in `deefour/producer`.
- Abstracted presenter resolution out to new [`deefour/producer`](https://github.com/deefour/producer).
- Removed the Laravel service provider and facade. The `'producer'` service in `deefour/producer` should be used instead.

#### 0.6.2 - June 5, 2015

[](#062---june-5-2015)

- Now following PSR-2.

#### 0.6.0 - May 24, 2015

[](#060---may-24-2015)

- Removed `model()` method on base presenter.
- Renamed `$model` property to `$_model` to avoid conflicts with an actual model attribute with the name `'model'`.
- Presenters now only provide property access to **public** properties on the presenter.
- Prefixed API methods/properties with `_` on the base presenter to further avoid conflicts with attribute overrides.
- Made `$_model` property public.
- Updates to code formatting.

#### 0.5.0 - April 27, 2015

[](#050---april-27-2015)

- Snake-case to camel-case method conversions are now cached for performance
- Exceptions are no longer thrown for missing properties/methods. See [`6f33dda`](https://github.com/deefour/presenter/commit/6f33dda7f310d95646091e6e5392ffd66d81ba00) for an explanation.

#### 0.4.0 - March 19, 2015

[](#040---march-19-2015)

- Rename `presenter()` helper to `present()`
- Remove `helpers.php` from Composer autoload. Developers should be able to choose whether these functions are included.
- Cleaning docblocks.
- Type-hinting the presenter factory.
- Renaming `Presentable` trait to `ResolvesPresenters` to avoid naming conflict with `\Deefour\Presenter\Contracts\Presentable`.

#### 0.3.0 - March 16, 2015

[](#030---march-16-2015)

- Allow presenters to be explicitly requested, bypassing the model default. For example ```
      $article = new Article;
      echo get_class($article->presenter()); //=> 'ArticlePresenter'
      echo get_class($article->presenter(FeaturedArticlePresenter::class)); //=> 'FeaturedArticlePresenter'
    ```

#### 0.2.3 - February 27, 2015

[](#023---february-27-2015)

- `Illuminate\Support\Collection` instances and native PHP arrays can now be passed directly into the `presenter()` helper.

#### 0.2.2 - February 20, 2015

[](#022---february-20-2015)

- Updated support for Laravel's Eloquent relations. Relations are now fetched and converted to presenter-wrapped objects or collections when requested.

#### 0.2.0 - February 5, 2015

[](#020---february-5-2015)

- Fix service provider.
- Make global `presenter()` work with Laravel IoC container if it's available.
- Move trait.

#### 0.1.0 - November 21, 2014

[](#010---november-21-2014)

- Initial release.

License
-------

[](#license)

Copyright (c) 2014 [Jason Daly](http://www.deefour.me) ([deefour](https://github.com/deefour)). Released under the [MIT License](http://deefour.mit-license.org/).

###  Health Score

34

—

LowBetter than 77% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity24

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity68

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~69 days

Recently: every ~198 days

Total

24

Last Release

2588d ago

Major Versions

0.8.0 → 1.0.02015-10-07

1.0.3 → 2.0.02017-02-13

2.0.0 → 3.0.02017-07-21

### Community

Maintainers

![](https://www.gravatar.com/avatar/0a2bddbe9f87813e53e82c1799b460513ede9f96a1d85ad7c186c0692a6f0762?d=identicon)[deefour](/maintainers/deefour)

---

Top Contributors

[![deefour](https://avatars.githubusercontent.com/u/14762?v=4)](https://github.com/deefour "deefour (86 commits)")

---

Tags

decoratorphppresentationpresenterlaravelmodelviewpresenterdecoratordeefour

###  Code Quality

Code StylePHP CS Fixer

### Embed Badge

![Health badge](/badges/deefour-presenter/health.svg)

```
[![Health](https://phpackages.com/badges/deefour-presenter/health.svg)](https://phpackages.com/packages/deefour-presenter)
```

###  Alternatives

[laracasts/presenter

Simple view presenters

8643.4M46](/packages/laracasts-presenter)[guilhermegonzaga/presenter

Implementation for Laravel of the presenter design pattern.

47242.1k](/packages/guilhermegonzaga-presenter)[lewis/presenter

Simple view presenter library for Laravel.

174.7k](/packages/lewis-presenter)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
