Skip to content

Commit

Permalink
Refactoring of Comment Preprocessing
Browse files Browse the repository at this point in the history
  • Loading branch information
iooe committed May 2, 2019
1 parent f9c15f7 commit 1f42df3
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 56 deletions.
12 changes: 2 additions & 10 deletions config/comments.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,8 @@
'api' => [
'get' => [
'preprocessor' => [
'user' => [
'avatar' => [
'class' => null,
'method' => 'default'
]
],
'comment' => [
'class' => null,
'method' => 'default'
]
'user' => null,
'comment' => null
]
]
]
Expand Down
54 changes: 27 additions & 27 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -174,54 +174,54 @@ This package fires events to let you know when things happen.
## Api preprocessing
Supported preprocessors for attributes of get api:
- user > avatar
- comment
- user **[Object]**
- comment **[String]**
#### Description
#### 1. Description
Sometimes additional processing of content is necessary before transmission over API.
For the comment preprocessing, you can use the config "preprocessor".
#### Config:
#### 2. Config:
```
'api' => [
'get' => [
'preprocessor' => [
'comment' => [
'class' => App\Helpers\CommentPreprocessor::class,
'method' => 'commentPreprocessor'
]
'comment' => App\Helpers\CommentPreprocessor\Comment::class
]
]
]
```
#### Example:
Without preprocessing:
```
$comment = 1;
echo $comment; // 1
```
With preprocessing:
```
<?php
#### 3. Contact
Create preprocessor class and implement `ICommentPreprocessor` interface:
Example:
```
namespace App\Helpers\CommentPreprocessor;
namespace App\Helpers;
use tizis\laraComments\Contracts\ICommentPreprocessor;
class CommentPreprocessor
class Comment implements ICommentPreprocessor
{
/**
* @param $comment
* @return string
*/
public static function commentPreprocessor($comment)
public function process($comment): array
{
return 'Hi, ' . $comment . '!';
}
}
}
```
#### 4. Example:
Without preprocessing:
```
$comment = 1;
echo $comment; // 1
```
With preprocessing:
```
$comment = 1;
Expand Down
14 changes: 14 additions & 0 deletions src/Contracts/ICommentPreprocessor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace tizis\laraComments\Contracts;

/**
* Preprocessor class Interface
*
* Interface ICommentPreprocessor
* @package tizis\laraComments\Contracts
*/
interface ICommentPreprocessor
{
public function process($object);
}
50 changes: 31 additions & 19 deletions src/Http/Resources/CommentResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace tizis\laraComments\Http\Resources;

use Illuminate\Http\Resources\Json\JsonResource;
use tizis\laraComments\Contracts\ICommentPreprocessor;

class CommentResource extends JsonResource
{
Expand All @@ -14,16 +15,11 @@ public function toArray($request): array
{
return [
'id' => $this->id,
'comment' => self::preprocessor($this->comment),
'comment' => self::comment($this->comment),
'created_at' => $this->created_at->timestamp,
'commenter_id' => $this->commenter_id,
'rating' => $this->rating,
'commenter' => [
'id' => $this->commenter->id,
'avatar' => $this->getAvatar(),
'name' => $this->commenter->name,
'email' => $this->commenter->email
],
'commenter' => self::user($this->commenter),
'children' => self::collection($this->children)
];
}
Expand All @@ -32,27 +28,43 @@ public function toArray($request): array
* @param string $comment
* @return string
*/
protected static function preprocessor(string $comment): string
protected static function comment(string $comment): string
{
$config = config('comments.api.get.preprocessor.comment');
$class = $config['class'];
$method = $config['method'];

if (\class_exists($config['class']) && \method_exists($config['class'], $config['method'])) {
$comment = $class::$method($comment);
if (!\class_exists($config)) {
return $comment;
}

$preprocessor = new $config;

if ($preprocessor instanceof ICommentPreprocessor) {
$comment = $preprocessor->process($comment);
}

return $comment;
}

protected function getAvatar()
protected static function user($user)
{
$config = config('comments.api.get.preprocessor.user.avatar');
$class = $config['class'];
$method = $config['method'];
if (\class_exists($config['class']) && \method_exists($config['class'], $config['method'])) {
return $class::$method($this->commenter);
$config = config('comments.api.get.preprocessor.user');
$default = [
'id' => $user->id,
'name' => $user->name,
'email' => $user->email
];

if (!\class_exists($config)) {
return $default;
}
return null;


$preprocessor = new $config;

if (\class_exists($config) && ($preprocessor instanceof ICommentPreprocessor)) {
return $preprocessor->process($user);
}

return $default;
}
}

0 comments on commit 1f42df3

Please sign in to comment.