src/Prettus/Repository/Traits/PresentableTrait.php
<?php
namespace Prettus\Repository\Traits;
use Illuminate\Support\Arr;
use Prettus\Repository\Contracts\PresenterInterface;
/**
* Class PresentableTrait
*
* @package Prettus\Repository\Traits
*/
trait PresentableTrait
{
/**
* @var PresenterInterface
*/
protected $presenter = null;
/**
* @param \Prettus\Repository\Contracts\PresenterInterface $presenter
*
* @return $this
*/
public function setPresenter(PresenterInterface $presenter)
{
$this->presenter = $presenter;
return $this;
}
/**
* @param $key
* @param null $default
*
* @return mixed|null
*/
public function present($key, $default = null)
{
if ($this->hasPresenter()) {
$data = $this->presenter()['data'];
return Arr::get($data, $key, $default);
}
return $default;
}
/**
* @return bool
*/
public function hasPresenter()
{
return isset($this->presenter) && $this->presenter instanceof PresenterInterface;
}
/**
* @return $this|mixed
*/
public function presenter()
{
if ($this->hasPresenter()) {
return $this->presenter->present($this);
}
return $this;
}
}