Skip to content

Commit

Permalink
Clean up JsonApiSerializer
Browse files Browse the repository at this point in the history
  • Loading branch information
nilportugues committed Oct 20, 2015
1 parent be61d2d commit 7a6f603
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
trait JsonApiResponseTrait
{
/**
* @param \Psr\Http\Message\ResponseInterface $response
* @param \Psr\Http\Message\ResponseInterface $response
* @return \Psr\Http\Message\ResponseInterface
*/
protected function addHeaders(\Psr\Http\Message\ResponseInterface $response)
Expand All @@ -28,7 +28,8 @@ protected function addHeaders(\Psr\Http\Message\ResponseInterface $response)
*
* @return \Symfony\Component\HttpFoundation\Response
*/
private function errorResponse($json) {
private function errorResponse($json)
{
return (new HttpFoundationFactory())
->createResponse($this->addHeaders(new \NilPortugues\Api\JsonApi\Http\Message\ErrorResponse($json)));
}
Expand Down
30 changes: 16 additions & 14 deletions src/NilPortugues/Laravel5/JsonApiSerializer/JsonApiSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,17 @@ protected function serializeObject($value)

if (is_subclass_of($value, Model::class, true)) {


$stdClass = (object) $value->getAttributes();
$data = $this->serializeData($stdClass);
$stdClass = (object) $value->getAttributes();
$data = $this->serializeData($stdClass);
$data[self::CLASS_IDENTIFIER_KEY] = get_class($value);

$methods = $this->getRelationshipMethodsAsPropertyName($value, get_class($value), new ReflectionClass($value));
$methods = $this->getRelationshipMethodsAsPropertyName(
$value,
get_class($value),
new ReflectionClass($value)
);

if(!empty($methods)) {
if (!empty($methods)) {
$data = array_merge($data, $methods);
}

Expand All @@ -67,7 +70,6 @@ protected function serializeObject($value)
return parent::serializeObject($value);
}


/**
* @param $value
* @param string $className
Expand All @@ -82,7 +84,7 @@ protected function getRelationshipMethodsAsPropertyName($value, $className, Refl
foreach ($reflection->getMethods(ReflectionMethod::IS_PUBLIC) as $method) {
if (ltrim($method->class, "\\") === ltrim($className, "\\")) {

$name = $method->name;
$name = $method->name;
$reflectionMethod = $reflection->getMethod($name);

// Eloquent relations do not include parameters, so we'll be filtering based on this criteria.
Expand All @@ -97,28 +99,28 @@ protected function getRelationshipMethodsAsPropertyName($value, $className, Refl
if (false !== strpos(get_class($returned), 'Illuminate\Database\Eloquent\Relations')) {

$items = [];
foreach($returned->getResults() as $model) {
foreach ($returned->getResults() as $model) {

if(is_object($model)) {
$stdClass = (object) $model->getAttributes();
$data = $this->serializeData($stdClass);
if (is_object($model)) {
$stdClass = (object) $model->getAttributes();
$data = $this->serializeData($stdClass);
$data[self::CLASS_IDENTIFIER_KEY] = get_class($model);

$items[] = $data;
}
}
if(!empty($items)) {
if (!empty($items)) {
$methods[$name] = [self::MAP_TYPE => 'array', self::SCALAR_VALUE => $items];
}

}
}
} catch(ErrorException $e) {}
} catch (ErrorException $e) {
}
}
}
}


return $methods;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,32 +31,32 @@ class Laravel5JsonApiSerializerServiceProvider extends ServiceProvider
*/
public function boot()
{
$this->publishes([__DIR__.self::PATH => config('jsonapi.php')]);
$this->publishes([__DIR__ . self::PATH => config('jsonapi.php')]);
}

/**
* Register the service provider.
*/
public function register()
{
$this->mergeConfigFrom(__DIR__.self::PATH, 'jsonapi');
$this->app->singleton(\NilPortugues\Laravel5\JsonApiSerializer\JsonApiSerializer::class, function ($app) {
$this->mergeConfigFrom(__DIR__ . self::PATH, 'jsonapi');
$this->app->singleton(
\NilPortugues\Laravel5\JsonApiSerializer\JsonApiSerializer::class,
function ($app) {

$mapping = $app['config']->get('jsonapi');
$key = md5(json_encode($mapping));
$key = md5(json_encode($mapping));

$cachedMapping = Cache::get($key);
if (!empty($cachedMapping)) {
return unserialize($cachedMapping);
}

self::parseNamedRoutes($mapping);
return Cache::rememberForever(
$key,
function () use ($mapping) {
self::parseNamedRoutes($mapping);

$serializer = new JsonApiSerializer(new JsonApiTransformer(new Mapper($mapping)));
Cache::put($key, serialize($serializer),60*60*24);

return $serializer;
});
return new JsonApiSerializer(new JsonApiTransformer(new Mapper($mapping)));
}
);
}
);
}

/**
Expand All @@ -75,7 +75,7 @@ private static function parseNamedRoutes(array &$mapping)
/**
* @param array $map
*/
private static function parseUrls(array &$map)
private static function parseUrls(&$map)
{
if (!empty($map['urls'])) {
foreach ($map['urls'] as &$namedUrl) {
Expand All @@ -87,7 +87,7 @@ private static function parseUrls(array &$map)
/**
* @param array $map
*/
private static function parseRelationshipUrls(array &$map)
private static function parseRelationshipUrls(&$map)
{
if (!empty($map['relationships'])) {
foreach ($map['relationships'] as &$relationship) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ protected static function getClassProperties($className)
{
if (class_exists($className, true)) {
$reflection = new ReflectionClass($className);
$value = $reflection->newInstanceWithoutConstructor();
$value = $reflection->newInstanceWithoutConstructor();

if (is_subclass_of($value, Model::class, true)) {
$attributes = array_merge(
$attributes = array_merge(
Schema::getColumnListing($value->getTable()),
self::getRelationshipMethodsAsPropertyName($value, $className, $reflection)
);
Expand All @@ -54,7 +54,6 @@ protected static function getClassProperties($className)
return parent::getClassProperties($className);
}


/**
* @param $value
* @param string $className
Expand All @@ -69,7 +68,7 @@ protected static function getRelationshipMethodsAsPropertyName($value, $classNam

if (ltrim($method->class, "\\") === ltrim($className, "\\")) {

$name = $method->name;
$name = $method->name;
$reflectionMethod = $reflection->getMethod($name);

// Eloquent relations do not include parameters, so we'll be filtering based on this criteria.
Expand All @@ -86,7 +85,8 @@ protected static function getRelationshipMethodsAsPropertyName($value, $classNam
}

}
} catch(ErrorException $e) {}
} catch (ErrorException $e) {
}
}
}
}
Expand Down

0 comments on commit 7a6f603

Please sign in to comment.