Created
August 14, 2017 08:54
-
-
Save Piterden/3e23a74ddee8eac1c44ceecae42bf7fc to your computer and use it in GitHub Desktop.
Presenter Through
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 namespace Anomaly\Streams\Platform\Model; | |
use Anomaly\Streams\Platform\Support\Presenter; | |
use Illuminate\Contracts\Support\Arrayable; | |
/** | |
* Class EloquentPresenter | |
* The base presenter for all our models. | |
* | |
* @link http://pyrocms.com/ | |
* @author PyroCMS, Inc. <support@pyrocms.com> | |
* @author Ryan Thompson <ryan@pyrocms.com> | |
*/ | |
class EloquentPresenter extends Presenter implements Arrayable | |
{ | |
/** | |
* Create a new EloquentPresenter instance. | |
* | |
* @param $object | |
*/ | |
public function __construct($object) | |
{ | |
if ($object instanceof EloquentModel) { | |
$this->object = $object; | |
} | |
} | |
/** | |
* Return the ID. | |
* | |
* @return mixed | |
*/ | |
public function id() | |
{ | |
return $this->object->getKey(); | |
} | |
/** | |
* Return the object as an array. | |
* | |
* @return array | |
*/ | |
public function toArray() | |
{ | |
return $this->object->toArray(); | |
} | |
} |
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 namespace Anomaly\Streams\Platform\Entry; | |
use Anomaly\Streams\Platform\Entry\Contract\EntryInterface; | |
use Anomaly\Streams\Platform\Model\EloquentModel; | |
use Anomaly\Streams\Platform\Model\EloquentPresenter; | |
use Anomaly\Streams\Platform\Support\Value; | |
/** | |
* Class EntryPresenter | |
* | |
* @link http://pyrocms.com/ | |
* @author PyroCMS, Inc. <support@pyrocms.com> | |
* @author Ryan Thompson <ryan@pyrocms.com> | |
*/ | |
class EntryPresenter extends EloquentPresenter | |
{ | |
/** | |
* The resource object. | |
* This is for IDE hinting. | |
* | |
* @var EntryInterface|EloquentModel | |
*/ | |
protected $object; | |
/** | |
* Return the date string for created at. | |
* | |
* @return string | |
*/ | |
public function createdAtDate() | |
{ | |
return $this->object->created_at | |
->setTimezone(config('app.timezone')) | |
->format(config('streams::datetime.date_format')); | |
} | |
/** | |
* Return the datetime string for created at. | |
* | |
* @return string | |
*/ | |
public function createdAtDatetime() | |
{ | |
return $this->object->created_at | |
->setTimezone(config('app.timezone')) | |
->format(config('streams::datetime.date_format') . ' ' . config('streams::datetime.time_format')); | |
} | |
/** | |
* Return the date string for updated at. | |
* | |
* @return string | |
*/ | |
public function updatedAtDate() | |
{ | |
return $this->object->updated_at | |
->setTimezone(config('app.timezone')) | |
->format(config('streams::datetime.date_format')); | |
} | |
/** | |
* Return the datetime string for updated at. | |
* | |
* @return string | |
*/ | |
public function updatedAtDatetime() | |
{ | |
return $this->object->updated_at | |
->setTimezone(config('app.timezone')) | |
->format(config('streams:datetime.date_format') . ' ' . config('streams:datetime.time_format')); | |
} | |
/** | |
* Return a label. | |
* | |
* @param $text | |
* @param string $context | |
* @param string $size | |
* @return string | |
*/ | |
public function label($text = null, $context = null, $size = null) | |
{ | |
if (!$text) { | |
$text = $this->object->getTitleName(); | |
} | |
if (!$context) { | |
$context = 'default'; | |
} | |
if (!$size) { | |
$size = 'sm'; | |
} | |
/* @var Value $value */ | |
$value = app(Value::class); | |
$text = $value->make($text, $this->object); | |
if (trans()->has($text) && is_string(trans($text))) { | |
$text = trans($text); | |
} | |
return '<span class="tag tag-' . $context . ' tag-' . $size . '">' . $text . '</span>'; | |
} | |
/** | |
* Return the edit URL. | |
* | |
* @return string | |
*/ | |
public function editUrl() | |
{ | |
return url( | |
implode( | |
'/', | |
array_unique( | |
array_filter( | |
[ | |
'admin', | |
$this->object->getStreamNamespace(), | |
$this->object->getStreamSlug(), | |
'edit', | |
$this->object->getId(), | |
] | |
) | |
) | |
) | |
); | |
} | |
/** | |
* Return the edit link. | |
* | |
* @return string | |
*/ | |
public function editLink() | |
{ | |
return app('html')->link($this->editUrl(), $this->object->{$this->object->getTitleName()}); | |
} | |
/** | |
* Return the view URL. | |
* | |
* @return string | |
*/ | |
public function viewUrl() | |
{ | |
return url( | |
implode( | |
'/', | |
array_unique( | |
array_filter( | |
[ | |
'admin', | |
$this->object->getStreamNamespace(), | |
$this->object->getStreamSlug(), | |
'view', | |
$this->object->getId(), | |
] | |
) | |
) | |
) | |
); | |
} | |
/** | |
* Return the view link. | |
* | |
* @return string | |
*/ | |
public function viewLink() | |
{ | |
return app('html')->link($this->viewUrl(), $this->object->{$this->object->getTitleName()}); | |
} | |
/** | |
* When accessing a property of a decorated entry | |
* object first check to see if the key represents | |
* a streams field. If it does then return the field | |
* type's presenter object. Otherwise handle normally. | |
* | |
* @param $key | |
* @return mixed | |
*/ | |
public function __get($key) | |
{ | |
if ($assignment = $this->object->getAssignment($key)) { | |
$type = $assignment->getFieldType(); | |
if ($assignment->isTranslatable() && $locale = config('app.locale')) { | |
$entry = $this->object->translateOrDefault($locale); | |
$type->setLocale($locale); | |
} else { | |
$entry = $this->object; | |
} | |
$type->setEntry($entry); | |
if (method_exists($type, 'getRelation')) { | |
return $type->decorate($entry->getRelationValue(camel_case($key))); | |
} | |
$type->setValue($entry->getFieldValue($key)); | |
return $type->getPresenter(); | |
} | |
return $this->__getDecorator()->decorate(parent::__get($key)); | |
} | |
} |
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 namespace Anomaly\Streams\Platform\Support; | |
use Illuminate\Foundation\Bus\DispatchesJobs; | |
/** | |
* Class Presenter | |
* | |
* @link http://pyrocms.com/ | |
* @author PyroCMS, Inc. <support@pyrocms.com> | |
* @author Ryan Thompson <ryan@pyrocms.com> | |
*/ | |
class Presenter extends \Robbo\Presenter\Presenter | |
{ | |
use DispatchesJobs; | |
/** | |
* Protected names. | |
* | |
* @var array | |
*/ | |
protected $protected = [ | |
'delete', | |
'save', | |
'update', | |
]; | |
/** | |
* Get the object. | |
* | |
* @return mixed | |
*/ | |
public function getObject() | |
{ | |
return $this->object; | |
} | |
/** | |
* Pass any unknown variable calls to present{$variable} or fall through to the injected object. | |
* | |
* @param string $var | |
* @return mixed | |
*/ | |
public function __get($var) | |
{ | |
if (in_array($var, $this->protected)) { | |
return null; | |
} | |
if ($method = $this->getPresenterMethodFromVariable($var)) { | |
return $this->$method(); | |
} | |
// Check the presenter for a getter. | |
if (method_exists($this, camel_case('get_' . $var))) { | |
return call_user_func_array([$this, camel_case('get_' . $var)], []); | |
} | |
// Check the presenter for a getter. | |
if (method_exists($this, camel_case('is_' . $var))) { | |
return call_user_func_array([$this, camel_case('is_' . $var)], []); | |
} | |
// Check the presenter for a method. | |
if (method_exists($this, camel_case($var))) { | |
return call_user_func_array([$this->object, camel_case($var)], []); | |
} | |
// Check the object for a getter. | |
if (method_exists($this->object, camel_case('get_' . $var))) { | |
return call_user_func_array([$this->object, camel_case('get_' . $var)], []); | |
} | |
// Check the object for a getter. | |
if (method_exists($this->object, camel_case('is_' . $var))) { | |
return call_user_func_array([$this->object, camel_case('is_' . $var)], []); | |
} | |
// Check the object for a method. | |
if (method_exists($this->object, camel_case($var))) { | |
return call_user_func_array([$this->object, camel_case($var)], []); | |
} | |
// Check the for a getter style hook. | |
if (method_exists($this->object, 'call') && $this->object->hasHook('get_' . $var)) { | |
return $this->object->call('get_' . $var); | |
} | |
// Check the for a normal style hook. | |
if (method_exists($this->object, 'call') && $this->object->hasHook($var)) { | |
return $this->object->call($var); | |
} | |
try { | |
// Lastly try generic property access. | |
return $this->__getDecorator()->decorate( | |
is_array($this->object) ? $this->object[$var] : $this->object->$var | |
); | |
} catch (\Exception $e) { | |
return null; | |
} | |
} | |
/** | |
* Fetch the presenter method name for the given variable. | |
* | |
* @param string $variable | |
* @return string|null | |
*/ | |
protected function getPresenterMethodFromVariable($variable) | |
{ | |
$method = camel_case($variable); | |
if (method_exists($this, $method)) { | |
return $method; | |
} | |
} | |
/** | |
* Call unknown methods if safe. | |
* | |
* @param string $method | |
* @param array $arguments | |
* @return mixed | |
*/ | |
public function __call($method, $arguments) | |
{ | |
if (in_array(snake_case($method), $this->protected)) { | |
return null; | |
} | |
return parent::__call($method, $arguments); // TODO: Change the autogenerated stub | |
} | |
/** | |
* Return the objects string method. | |
* | |
* @return string | |
*/ | |
public function __toString() | |
{ | |
if (method_exists($this->object, '__toString')) { | |
return $this->object->__toString(); | |
} | |
return json_encode($this->object); | |
} | |
} |
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 namespace Fesor\CatalogModule\Product; | |
use Anomaly\Streams\Platform\Entry\EntryPresenter; | |
class ProductPresenter extends EntryPresenter | |
{ | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment