Skip to content
This repository has been archived by the owner on Jul 15, 2021. It is now read-only.

Commentable interface, HasComments trait and configuration file. #44

Merged
merged 1 commit into from
Nov 21, 2018
Merged
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
17 changes: 14 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,19 @@ All Notable changes to `Laravel Comment` will be documented in this file.

Updates should follow the [Keep a CHANGELOG](http://keepachangelog.com/) principles.

## [1.0.0] - 2018-11-21

### Added
- Initial Release.
- `Commentable` has been reimplemented as a contract.
- `HasComments` trait has been implemented.
- `comment.php` configuration file has been created.

## [0.3.0] - 2017-01-31

### Added
- Laravel 5.4 Support.

## [0.2.1] - 2016-10-20

### Added
Expand All @@ -15,6 +28,4 @@ Updates should follow the [Keep a CHANGELOG](http://keepachangelog.com/) princip
- Laravel 5.3 Support.

## [0.1.0] - 2016-06-11

### Added
- Initial Release.
- Initial Release
100 changes: 64 additions & 36 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
[![Build Status][ico-travis]][link-travis]
[![Total Downloads][ico-downloads]][link-downloads]

Just another comment system for laravel projects.
Just another comment system for your awesome Laravel project.

## Version Compatibility

Expand All @@ -16,9 +16,8 @@ Just another comment system for laravel projects.
5.2.x | 0.1.x
5.3.x | 0.2.x
5.4.x | 0.3.x
5.5.x | 0.4.x
5.6.x | 0.5.x
5.7.x | 0.6.x

For `>5.5` you can use `^1.0.0` version.

## Install

Expand All @@ -27,100 +26,129 @@ Via Composer
``` bash
$ composer require actuallymab/laravel-comment
```
Laravel 5.5 uses Package Auto-Discovery, so doesn't require you to manually add the ServiceProvider.

If you don't use auto-discovery, or using Laravel version < 5.5 Add service provider to your app.php file

``` php
\Actuallymab\LaravelComment\LaravelCommentServiceProvider::class
```

Publish & Migrate comments table.
Publish configurations and migrations, then migrate comments table.

``` bash
$ php artisan vendor:publish
$ php artisan migrate
```

Add `CanComment` trait to your User model.

``` php
use Actuallymab\LaravelComment\CanComment;
```

Add `Commentable` trait to your commentable model(s).
Add `Commentable` interface and `HasComments` trait to your commentable model(s).

``` php
use Actuallymab\LaravelComment\Commentable;
use Actuallymab\LaravelComment\Contracts\Commentable;
use Actuallymab\LaravelComment\HasComments;

class Product extends Model implements Commentable
{
use HasComments;

// ...
}
```

If you want to have your own Comment Model create a new one and extend my Comment model.

``` php
class Comment extends Actuallymab\LaravelComment\Comment
use Actuallymab\LaravelComment\Models\Comment as LaravelComment;

class Comment extends LaravelComment
{
...
// ...
}
```

and dont forget to update the model name in the `config/comment.php` file.

Comment package comes with several modes.

1- If you want to Users can rate your model(s) with comment set `canBeRated` to true in your `Commentable` model.
1- If you want to users can rate your commentable models;

``` php
class Product extends Model {
use Commentable;
class Product extends Model implements Commentable
{
use HasComments;

protected $canBeRated = true;
public function canBeRated(): bool
{
return true; // default false
}

...
//...
}
```

2- If you want to approve comments for your commentable models, you must set `mustBeApproved` to true in your `Commentable` model.
2- If you want to approve comments for your commentable models;

``` php
class Product extends Model {
use Commentable;
class Product extends Model implements Commentable
{
use HasComments;

protected $mustBeApproved = true;
public function mustBeApproved(): bool
{
return true; // default false
}

...
// ...
}
```

3- You don't want to approve comments for all users (think this as you really want to approve your own comments?). So add your `User` model an `isAdmin` method and return it true if user is admin.
3- Sometimes you don't want to approve comments for all users;

``` php
class User extends Model {
use CanComment;
class User extends Model
{
use CanComment;

protected $fillable = [
'isAdmin',
....
];
protected $fillable = [
'isAdmin',
// ..
];

public function isAdmin() {
return $this->isAdmin;
}
public function canCommentWithoutApprove(): bool
{
return $this->isAdmin;
}

...
// ..
}
```

## Usage

``` php
$user = App\User::find(1);
$product = App\Product::find(1);
$user = App\User::first();
$product = App\Product::first();

// $user->comment(Commentable $model, $comment = '', $rate = 0);
$user->comment($product, 'Lorem ipsum ..', 3);

// approve it -- if you are admin or you don't use mustBeApproved option, it is not necessary
// approve it -- if the user model `canCommentWithoutApprove()` or you don't use `mustBeApproved()`, it is not necessary
$product->comments[0]->approve();

// get avg rating -- it calculates approved average rate.
$product->averageRate();

// get total comment count -- it calculates approved comments count.
$product->totalCommentCount();
// get total comments count -- it calculates approved comments count.
$product->totalCommentsCount();
```

> Tip: You might want to look at the tests/CommentTest.php file to check all potential usages.

## Change log

Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently.
Expand Down
19 changes: 7 additions & 12 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "actuallymab/laravel-comment",
"type": "library",
"description": "Just another comment system for laravel projects.",
"description": "Just another comment system for your awesome Laravel project.",
"keywords": [
"actuallymab",
"laravel-comment"
Expand All @@ -11,22 +11,17 @@
"authors": [
{
"name": "Mehmet Aydin Bahadir",
"email": "mehmet.aydin.bahadir@gmail.com",
"homepage": "http://www.actuallymab.me",
"role": "Software Engineer"
"email": "mehmet.aydin.bahadir@gmail.com"
}
],
"require": {
"illuminate/database": "5.7.*",
"php": "^7.1.3"
"php": "^7.1.3",
"illuminate/database": "~5.5.0|~5.6.0|~5.7.0"
},
"require-dev": {
"fzaninotto/faker": "~1.4",
"squizlabs/php_codesniffer": "^2.3",
"phpunit/phpunit": "~7.0",
"mockery/mockery": "~0.9.0",
"orchestra/testbench": "~3.7",
"orchestra/database": "3.7.x@dev"
"phpunit/phpunit": "^7.4",
"orchestra/testbench": "~3.5.0|~3.6.0|~3.7.0",
"fzaninotto/faker": "^1.8"
},
"autoload": {
"psr-4": {
Expand Down
9 changes: 9 additions & 0 deletions config/comment.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

return [
/**
* This is the default comment model of the application.
* If you create another comment class with extending this one, you should update this field with that.
*/
'model' => \Actuallymab\LaravelComment\Models\Comment::class,
];
19 changes: 13 additions & 6 deletions database/migrations/create_comments_table.php.stub
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
<?php
/** actuallymab | 12.06.2016 - 02:00 */
declare(strict_types=1);

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;

class CreateCommentsTable extends Migration
{
public function up()
public function up(): void
{
Schema::create('comments', function (Blueprint $table) {
Schema::create($this->commentsTable(), function (Blueprint $table) {
$table->increments('id');
$table->string('commentable_id')->nullable();
$table->string('commentable_type')->nullable();
Expand All @@ -23,8 +23,15 @@ class CreateCommentsTable extends Migration
});
}

public function down()
public function down(): void
{
Schema::drop($this->commentsTable());
}

private function commentsTable(): string
{
Schema::drop('comments');
$model = config('comment.model');

return (new $model)->getTable();
}
}
5 changes: 5 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,9 @@
<directory suffix=".php">./tests/</directory>
</testsuite>
</testsuites>
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">./src</directory>
</whitelist>
</filter>
</phpunit>
62 changes: 30 additions & 32 deletions src/CanComment.php
Original file line number Diff line number Diff line change
@@ -1,52 +1,50 @@
<?php
/**
* Created by PhpStorm.
* User: actuallymab
* Date: 12.06.2016
* Time: 02:13
*/
declare(strict_types=1);

namespace Actuallymab\LaravelComment;

use Actuallymab\LaravelComment\Models\Comment;
use Actuallymab\LaravelComment\Contracts\Commentable;
use Illuminate\Database\Eloquent\Relations\MorphMany;

trait CanComment
{

/**
* @param $commentable
* @param string $commentText
* @param int $rate
* @return $this
*/
public function comment($commentable, $commentText = '', $rate = 0)
public function comment(Commentable $commentable, string $commentText = '', int $rate = 0): self
{
$comment = new Comment([
'comment' => $commentText,
'rate' => ($commentable->getCanBeRated()) ? $rate : null,
'approved' => ($commentable->mustBeApproved() && ! $this->isAdmin()) ? false : true,
'commented_id' => $this->id,
'commented_type' => $this->getMorphClass()
]);
$commentModel = config('comment.model');

$commentable->comments()->save($comment);
$commentable->comments()->save(new $commentModel([
'comment' => $commentText,
'rate' => $commentable->canBeRated() ? $rate : null,
'approved' => $commentable->mustBeApproved() && !$this->canCommentWithoutApprove() ? false : true,
'commented_id' => $this->primaryId(),
'commented_type' => get_class(),
]));

return $this;
}

/**
* @return bool
*/
public function isAdmin()
public function canCommentWithoutApprove(): bool
{
return false;
}

/**
* @return \Illuminate\Database\Eloquent\Relations\MorphMany
*/
public function comments()
public function comments(): MorphMany
{
return $this->morphMany(config('comment.model'), 'commented');
}

public function hasCommentsOn(Commentable $commentable): bool
{
return $this->comments()
->where([
'commentable_id' => $commentable->primaryId(),
'commentable_type' => get_class($commentable),
])
->exists();
}

private function primaryId(): string
{
return $this->morphMany(Comment::class, 'commented');
return (string)$this->getAttribute($this->primaryKey);
}
}
Loading