Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Laravel-emojione enhancements #15

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,11 @@ So if someone leaves a comment like `This is an awesome comment 😄🔥` it wil
In your view where you display your comments you can use

```php
@emojione($comment->content)
@emojione($comment->content, 'toImage')
```
and that will convert `:smile:` and `😄` to the emojione equivalent.

> You can use any emojione method you wish as second argument, checkout more [joypixels/emojione](https://github.com/joypixels/emojione/blob/master/lib/php/src/Client.php)

## Assets
By default it will use the assets from JSDelivr.
Expand Down
70 changes: 65 additions & 5 deletions src/LaravelEmojiOne.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,14 @@

class LaravelEmojiOne
{
/**
* @var \Emojione\Client
*/
private $client;

/**
* LaravelEmojiOne constructor.
*/
public function __construct()
{
$this->client = new Client(new Ruleset());
Expand All @@ -20,39 +26,93 @@ public function __construct()

if (config('emojione.imagePathPNG')) {
$this->client->imagePathPNG = url(config('emojione.imagePathPNG')) . '/';
}
else {
} else {
// Use the CDN if 'imagePathPNG' config is not set
$this->client->imagePathPNG = 'https://cdn.jsdelivr.net/emojione/assets' . '/' . $this->client->emojiVersion . '/png/' . $this->client->emojiSize . '/';
}
// config ascii option added ternary incase option isn't part of config

// config ascii option added ternary in case option isn't part of config
$this->client->ascii = config('emojione.ascii') ? true : false;

}

/**
* @param $str
*
* @return string
*/
public function toImage($str)
{
return $this->client->toImage($str);
}

/**
* @param $str
*
* @return string
*/
public function toShort($str)
{
return $this->client->toShort($str);
}

/**
* @param $str
*
* @return string
*/
public function shortnameToImage($str)
{
return $this->client->shortnameToImage($str);
}

/**
* @param $str
*
* @return string
*/
public function unicodeToImage($str)
{
return $this->client->unicodeToImage($str);
}

/**
* @param $size
*
* @return \ChristofferOK\LaravelEmojiOne\LaravelEmojiOne
*/
public function setEmojiSize($size)
{
$this->client->emojiSize = $size;

return $this;
}

/**
* @return \Emojione\Client
*/
public function getClient()
{
return $this->client;
}

/**
* @param $name
* @param $arguments
*
* @return mixed
* @throws \Exception
*/
public function __call($name, $arguments)
{
$str = $arguments[0];
if (!is_string($str)) {
throw new \Exception('The argument must be a string');
}

if (!method_exists($this->client, $name)) {
throw new \Exception('Method ' . $name . ' does not exist');
}

return $this->client->{$name}($str);
}
}
4 changes: 4 additions & 0 deletions src/LaravelEmojiOneFacade.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
<?php

namespace ChristofferOK\LaravelEmojiOne;

use Illuminate\Support\Facades\Facade;

class LaravelEmojiOneFacade extends Facade
{
/**
* @return string
*/
protected static function getFacadeAccessor()
{
return LaravelEmojiOne::class;
Expand Down
16 changes: 11 additions & 5 deletions src/LaravelEmojiOneServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@

namespace ChristofferOK\LaravelEmojiOne;

use Illuminate\Contracts\Container\Container;
use Illuminate\Support\ServiceProvider;
use Illuminate\Contracts\Container\Container;

class LaravelEmojiOneServiceProvider extends ServiceProvider
{

/**
* Boot the service provider.
*/
public function boot()
{
$this->publishes([__DIR__.'/../config/emojione.php' => config_path('emojione.php')], 'config');
$this->publishes([__DIR__ . '/../config/emojione.php' => config_path('emojione.php')], 'config');

$this->publishes([
base_path('vendor/emojione/assets/png') => public_path('vendor/emojione/png'),
Expand All @@ -21,13 +23,17 @@ public function boot()
], 'sprites');

\Blade::directive('emojione', function ($expression) {
return "<?php echo \App::make('".LaravelEmojiOne::class."')->toImage($expression); ?>";
list($string, $method) = explode(',', $expression);
return "<?php echo \App::make('" . LaravelEmojiOne::class . "')->{{$method}}($string); ?>";
});
}

/**
* Register the service provider.
*/
public function register()
{
$this->mergeConfigFrom(__DIR__.'/../config/emojione.php', 'emojione');
$this->mergeConfigFrom(__DIR__ . '/../config/emojione.php', 'emojione');
$this->app->singleton(LaravelEmojiOne::class, function (Container $app) {
return new LaravelEmojiOne();
});
Expand Down