Skip to content

Instantly share code, notes, and snippets.

@vanchelo
Last active August 29, 2015 14:16
Show Gist options
  • Save vanchelo/e4cab3bf102fb8d1206b to your computer and use it in GitHub Desktop.
Save vanchelo/e4cab3bf102fb8d1206b to your computer and use it in GitHub Desktop.
<?php
use Illuminate\Support\Facades\Facade;
class Title extends Facade
{
protected static function getFacadeAccessor()
{
return 'title';
}
}
<?php
class Title
{
/**
* Array of values
*
* @var array
*/
protected $values = [];
/**
* Values delimiter
*
* @var string
*/
protected $delimiter;
/**
* Constructor
*
* @param $value
* @param string $delimiter
*/
function __construct($value, $delimiter = ' :: ')
{
$this->values[] = $value;
$this->delimiter = $delimiter;
}
/**
* Put value at the end
*
* @param string $value
* @param null $delimiter
*/
public function append($value = '', $delimiter = null)
{
$this->values[] = $delimiter ?: $this->delimiter;
$this->values[] = $value;
}
/**
* Put value at the start
*
* @param string $value
* @param null $delimiter
*/
public function prepend($value = '', $delimiter = null)
{
array_unshift($this->values, $value, $delimiter ?: $this->delimiter);
}
/**
* Render title
*
* @return string
*/
public function render()
{
return implode('', $this->values);
}
/**
* Get last value
*
* @return mixed
*/
public function last()
{
return end($this->values);
}
/**
* Get first value
*
* @return mixed
*/
public function first()
{
return reset($this->values);
}
/**
* To string implementation
*
* @return string
*/
function __toString()
{
return $this->render();
}
}
<?php
use Illuminate\Support\ServiceProvider;
use Title;
class TitleServiceProvider extends ServiceProvider
{
/**
* Register the service provider.
*
* @return void
*/
public function register()
{
$this->app->bindShared('title', function ()
{
return new Title(
$this->app['config']->get('app.site_name'),
$this->app['config']->get('app.title_delimiter')
);
});
$this->app->singleton('Shop\Commons\Helpers\Title', 'title');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment