Last active
November 1, 2018 07:13
-
-
Save Ellrion/586c376cd07739916bbbb8e74e9ff211 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 declare(strict_types=1); | |
namespace Adteam\Presenters; | |
abstract class BasePresenter extends AbstractPresenter | |
{ | |
/** | |
* Language file fore this presenter. | |
* | |
* @var string | |
*/ | |
public static $langDomain; | |
/** | |
* Get the translation for the given key or key. | |
* | |
* @param $key | |
* @param array $parameters | |
* @param string|null $domain | |
* @param string|null $locale | |
* @return string | |
*/ | |
public static function translate($key, $parameters = [], $domain = null, $locale = null) | |
{ | |
$domain = $domain ?: static::$langDomain; | |
if (!empty($domain) && trans()->has($domain . '.' . $key, $locale)) { | |
return trans($domain . '.' . $key, $parameters, $locale); | |
} | |
return trans($key, $parameters, $locale); | |
} | |
/** | |
* @return \Illuminate\Support\Collection | |
*/ | |
public function bannerAdTypesLabels() | |
{ | |
static $labels; | |
return $labels ?: $labels = app('registry')->load('banner_ad_types_openrtb')->pluck('label', 'id'); | |
} | |
/** | |
* @param string $typeId | |
* @return string | |
*/ | |
public function bannerAdTypePresent($typeId) | |
{ | |
return $this->bannerAdTypesLabels()[$typeId] ?? 'n/a#' . $typeId; | |
} | |
/** | |
* @param \Money\Money $money | |
* @return string | |
*/ | |
public function moneyPresent($money) | |
{ | |
return app('money.formatter')->format($money); | |
} | |
/** | |
* @param \Money\Money $money | |
* @return string | |
*/ | |
public function moneyWithCurrencyPresent($money) | |
{ | |
return app('money.formatter')->format($money) . ' ' . $money->getCurrency(); | |
} | |
} |
This file contains hidden or 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 declare(strict_types=1); | |
namespace App\Presenters; | |
/** | |
* @property-read string money | |
* @method string money(\Money\Money $money) | |
* @property-read string money_with_currency | |
* @method string money_with_currency(\Money\Money $money) | |
* @property-read string banner_ad_type | |
* @method string banner_ad_type($typeId) | |
* .... | |
* | |
* @see \App\Presenters\BasePresenter | |
*/ | |
final class HelperPresenter | |
{ | |
/** | |
* @var mixed | |
*/ | |
protected $data; | |
/** | |
* @param string $name | |
* @return string mixed | |
*/ | |
public function __get($name) | |
{ | |
return $this->{$name}(); | |
} | |
/** | |
* @param string $name | |
* @param array $arguments | |
* @return string| | |
*/ | |
public function __call($name, $arguments) | |
{ | |
$presenter = camel_case($name); | |
$data = [] === $arguments ? $this->data : $arguments; | |
$this->data = null; | |
return call_user_func_array([BasePresenter::class, "{$presenter}Present"], $data); | |
} | |
/** | |
* @param mixed $data | |
* @return $this | |
*/ | |
public function setNextPresentation($data) | |
{ | |
$this->data = [$data]; | |
return $this; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
In ServiceProvider
in helpers.php
Use examplae: