-
-
Save vanchelo/a52d17545bd0115adfc5d8aaac5f55fe 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 | |
namespace App\Services\Foundation; | |
use Illuminate\Contracts\Container\Container; | |
use Illuminate\Contracts\Support\Jsonable; | |
use InvalidArgumentException; | |
use JsonSerializable; | |
class JsonFactory | |
{ | |
/** | |
* Base Namespace for JsonGenerator classes. | |
* | |
* @var | |
*/ | |
protected $rootNamespace; | |
/** | |
* @var \Illuminate\Contracts\Container\Container | |
*/ | |
private $app; | |
/** | |
* | |
* @param \Illuminate\Contracts\Container\Container $app | |
*/ | |
public function __construct(Container $app) | |
{ | |
$this->app = $app; | |
} | |
/** | |
* Generate (transform) json from data. | |
* | |
* @param string $generator | |
* @param mixed $data | |
* | |
* @return mixed|string | |
*/ | |
public function make($generator, $data) | |
{ | |
$generatorClass = $this->getGeneratorClass($generator); | |
$generator = $this->app->make($generatorClass, compact('data')); | |
if ($generator instanceof JsonSerializable) { | |
return $generator->jsonSerialize(); | |
} | |
if ($generator instanceof Jsonable) { | |
return $generator->toJson(); | |
} | |
throw new InvalidArgumentException('Builder must implements \Illuminate\Contracts\Support\Jsonable or \JsonSerializable'); | |
} | |
/** | |
* Getting name of generator class. | |
* | |
* @param $generator | |
* @return string | |
*/ | |
protected function getGeneratorClass($generator) | |
{ | |
if (class_exists($generator)) { | |
return $generator; | |
} | |
$path = collect(explode('.', $generator))->map(function ($str) { | |
return ucfirst(camel_case($str)); | |
}); | |
return $this->rootNamespace . '\\' . implode('\\', $path->all()); | |
} | |
/** | |
* Setting up root namespace for generators classes. | |
* | |
* @param $rootNamespace | |
* @return $this | |
*/ | |
public function setRootGeneratorNamespace($rootNamespace) | |
{ | |
$this->rootNamespace = $rootNamespace; | |
return $this; | |
} | |
} |
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 | |
namespace App\Providers; | |
use App\Services\Foundation\JsonFactory; | |
use Illuminate\Support\ServiceProvider; | |
class JsonViewServiceProvider extends ServiceProvider | |
{ | |
/** | |
* Bootstrap any application services. | |
* | |
* @return void | |
*/ | |
public function boot() | |
{ | |
} | |
/** | |
* Register any application services. | |
* | |
* @return void | |
*/ | |
public function register() | |
{ | |
$this->app->singleton(['json.factory' => JsonFactory::class], function ($app) { | |
return (new JsonFactory($app))->setRootGeneratorNamespace('App\\Http\\JsonResponses'); | |
}); | |
$this->registerResponseBuilder(); | |
} | |
protected function registerResponseBuilder() | |
{ | |
$this->app->extend('Illuminate\Contracts\Routing\ResponseFactory', function ($factory, $app) { | |
$factory->macro( | |
'jsonable', | |
function ($generator, $data = [], $status = 200, array $headers = [], $options = 0) use ($app) { | |
return $this->json( | |
$app['json.factory']->make($generator, $data, $status), | |
$status, | |
$headers, | |
$options | |
); | |
} | |
); | |
return $factory; | |
}); | |
} | |
} |
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 | |
namespace App\Http\JsonResponses; | |
use Illuminate\Contracts\Support\Jsonable; | |
class Simple implements Jsonable | |
{ | |
protected $data; | |
/** | |
* Simple constructor. | |
* @param mixed $data | |
*/ | |
public function __construct($data) | |
{ | |
$this->data = $data; | |
if (method_exists($this, 'generate')) { | |
$this->data = $this->{'generate'}($data); | |
} | |
} | |
public function toJson($options = 0) | |
{ | |
return $this->data; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment