Skip to content

Commit

Permalink
Optimize transformers
Browse files Browse the repository at this point in the history
  • Loading branch information
jcc committed Jan 7, 2018
1 parent bd51406 commit c2065c0
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 21 deletions.
12 changes: 6 additions & 6 deletions app/Transformers/ArticleTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ public function transform(Article $article)
*/
public function includeCategory(Article $article)
{
$category = $article->category;

return $this->item($category, new CategoryTransformer);
if ($category = $article->category) {
return $this->item($category, new CategoryTransformer);
}
}

/**
Expand All @@ -52,8 +52,8 @@ public function includeCategory(Article $article)
*/
public function includeTags(Article $article)
{
$tags = $article->tags;

return $this->collection($tags, new TagTransformer);
if ($tags = $article->tags) {
return $this->collection($tags, new TagTransformer);
}
}
}
6 changes: 3 additions & 3 deletions app/Transformers/CommentTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ public function transform(Comment $comment)
*/
public function includeUser(Comment $comment)
{
$user = $comment->user;

return $this->item($user, new UserTransformer);
if ($user = $comment->user) {
return $this->item($user, new UserTransformer);
}
}

}
25 changes: 13 additions & 12 deletions app/Transformers/DiscussionTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,17 @@ class DiscussionTransformer extends TransformerAbstract
'user'
];

protected $defaultIncludes = [
'user',
];

public function transform(Discussion $discussion)
{
$content = json_decode($discussion->content);

return [
'id' => $discussion->id,
'user_id' => $discussion->user_id,
'user' => [
'name' => $discussion->user->name
],
'title' => $discussion->title,
'content_raw' => $content->raw,
'content_html' => $content->html,
Expand All @@ -40,9 +41,9 @@ public function transform(Discussion $discussion)
*/
public function includeComments(Discussion $discussion)
{
$comments = $discussion->comments;

return $this->collection($comments, new CommentTransformer);
if ($comments = $discussion->comments) {
return $this->collection($comments, new CommentTransformer);
}
}

/**
Expand All @@ -53,9 +54,9 @@ public function includeComments(Discussion $discussion)
*/
public function includeTags(Discussion $discussion)
{
$tags = $discussion->tags;

return $this->collection($tags, new TagTransformer);
if ($tags = $discussion->tags) {
return $this->collection($tags, new TagTransformer);
}
}

/**
Expand All @@ -66,8 +67,8 @@ public function includeTags(Discussion $discussion)
*/
public function includeUser(Discussion $discussion)
{
$user = $discussion->user;

return $this->item($user, new UserTransformer);
if ($user = $discussion->user) {
return $this->item($user, new UserTransformer);
}
}
}

0 comments on commit c2065c0

Please sign in to comment.