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

Commit 51970a0

Browse files
authored
Merge pull request #44 from actuallymab/feature/stable-version
Commentable interface, HasComments trait and configuration file.
2 parents 54161bc + 7fadc01 commit 51970a0

20 files changed

+407
-372
lines changed

CHANGELOG.md

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,19 @@ All Notable changes to `Laravel Comment` will be documented in this file.
44

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

7+
## [1.0.0] - 2018-11-21
8+
9+
### Added
10+
- Initial Release.
11+
- `Commentable` has been reimplemented as a contract.
12+
- `HasComments` trait has been implemented.
13+
- `comment.php` configuration file has been created.
14+
15+
## [0.3.0] - 2017-01-31
16+
17+
### Added
18+
- Laravel 5.4 Support.
19+
720
## [0.2.1] - 2016-10-20
821

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

1730
## [0.1.0] - 2016-06-11
18-
19-
### Added
20-
- Initial Release.
31+
- Initial Release

README.md

Lines changed: 64 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
[![Build Status][ico-travis]][link-travis]
66
[![Total Downloads][ico-downloads]][link-downloads]
77

8-
Just another comment system for laravel projects.
8+
Just another comment system for your awesome Laravel project.
99

1010
## Version Compatibility
1111

@@ -16,9 +16,8 @@ Just another comment system for laravel projects.
1616
5.2.x | 0.1.x
1717
5.3.x | 0.2.x
1818
5.4.x | 0.3.x
19-
5.5.x | 0.4.x
20-
5.6.x | 0.5.x
21-
5.7.x | 0.6.x
19+
20+
For `>5.5` you can use `^1.0.0` version.
2221

2322
## Install
2423

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

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

3432
``` php
3533
\Actuallymab\LaravelComment\LaravelCommentServiceProvider::class
3634
```
3735

38-
Publish & Migrate comments table.
36+
Publish configurations and migrations, then migrate comments table.
37+
3938
``` bash
4039
$ php artisan vendor:publish
4140
$ php artisan migrate
4241
```
4342

4443
Add `CanComment` trait to your User model.
44+
4545
``` php
4646
use Actuallymab\LaravelComment\CanComment;
4747
```
4848

49-
Add `Commentable` trait to your commentable model(s).
49+
Add `Commentable` interface and `HasComments` trait to your commentable model(s).
50+
5051
``` php
51-
use Actuallymab\LaravelComment\Commentable;
52+
use Actuallymab\LaravelComment\Contracts\Commentable;
53+
use Actuallymab\LaravelComment\HasComments;
54+
55+
class Product extends Model implements Commentable
56+
{
57+
use HasComments;
58+
59+
// ...
60+
}
5261
```
5362

5463
If you want to have your own Comment Model create a new one and extend my Comment model.
64+
5565
``` php
56-
class Comment extends Actuallymab\LaravelComment\Comment
66+
use Actuallymab\LaravelComment\Models\Comment as LaravelComment;
67+
68+
class Comment extends LaravelComment
5769
{
58-
...
70+
// ...
5971
}
6072
```
6173

74+
and dont forget to update the model name in the `config/comment.php` file.
75+
6276
Comment package comes with several modes.
6377

64-
1- If you want to Users can rate your model(s) with comment set `canBeRated` to true in your `Commentable` model.
78+
1- If you want to users can rate your commentable models;
79+
6580
``` php
66-
class Product extends Model {
67-
use Commentable;
81+
class Product extends Model implements Commentable
82+
{
83+
use HasComments;
6884

69-
protected $canBeRated = true;
85+
public function canBeRated(): bool
86+
{
87+
return true; // default false
88+
}
7089

71-
...
90+
//...
7291
}
7392
```
7493

75-
2- If you want to approve comments for your commentable models, you must set `mustBeApproved` to true in your `Commentable` model.
94+
2- If you want to approve comments for your commentable models;
95+
7696
``` php
77-
class Product extends Model {
78-
use Commentable;
97+
class Product extends Model implements Commentable
98+
{
99+
use HasComments;
79100

80-
protected $mustBeApproved = true;
101+
public function mustBeApproved(): bool
102+
{
103+
return true; // default false
104+
}
81105

82-
...
106+
// ...
83107
}
84108
```
85109

86-
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.
110+
3- Sometimes you don't want to approve comments for all users;
87111

88112
``` php
89-
class User extends Model {
90-
use CanComment;
113+
class User extends Model
114+
{
115+
use CanComment;
91116

92-
protected $fillable = [
93-
'isAdmin',
94-
....
95-
];
117+
protected $fillable = [
118+
'isAdmin',
119+
// ..
120+
];
96121

97-
public function isAdmin() {
98-
return $this->isAdmin;
99-
}
122+
public function canCommentWithoutApprove(): bool
123+
{
124+
return $this->isAdmin;
125+
}
100126

101-
...
127+
// ..
102128
}
103129
```
104130

105131
## Usage
106132

107133
``` php
108-
$user = App\User::find(1);
109-
$product = App\Product::find(1);
134+
$user = App\User::first();
135+
$product = App\Product::first();
110136

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

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

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

120-
// get total comment count -- it calculates approved comments count.
121-
$product->totalCommentCount();
146+
// get total comments count -- it calculates approved comments count.
147+
$product->totalCommentsCount();
122148
```
123149

150+
> Tip: You might want to look at the tests/CommentTest.php file to check all potential usages.
151+
124152
## Change log
125153

126154
Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently.

composer.json

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "actuallymab/laravel-comment",
33
"type": "library",
4-
"description": "Just another comment system for laravel projects.",
4+
"description": "Just another comment system for your awesome Laravel project.",
55
"keywords": [
66
"actuallymab",
77
"laravel-comment"
@@ -11,22 +11,17 @@
1111
"authors": [
1212
{
1313
"name": "Mehmet Aydin Bahadir",
14-
"email": "[email protected]",
15-
"homepage": "http://www.actuallymab.me",
16-
"role": "Software Engineer"
14+
"email": "[email protected]"
1715
}
1816
],
1917
"require": {
20-
"illuminate/database": "5.7.*",
21-
"php": "^7.1.3"
18+
"php": "^7.1.3",
19+
"illuminate/database": "~5.5.0|~5.6.0|~5.7.0"
2220
},
2321
"require-dev": {
24-
"fzaninotto/faker": "~1.4",
25-
"squizlabs/php_codesniffer": "^2.3",
26-
"phpunit/phpunit": "~7.0",
27-
"mockery/mockery": "~0.9.0",
28-
"orchestra/testbench": "~3.7",
29-
"orchestra/database": "3.7.x@dev"
22+
"phpunit/phpunit": "^7.4",
23+
"orchestra/testbench": "~3.5.0|~3.6.0|~3.7.0",
24+
"fzaninotto/faker": "^1.8"
3025
},
3126
"autoload": {
3227
"psr-4": {

config/comment.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
return [
4+
/**
5+
* This is the default comment model of the application.
6+
* If you create another comment class with extending this one, you should update this field with that.
7+
*/
8+
'model' => \Actuallymab\LaravelComment\Models\Comment::class,
9+
];

database/migrations/create_comments_table.php.stub

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
<?php
2-
/** actuallymab | 12.06.2016 - 02:00 */
2+
declare(strict_types=1);
33

4-
use Illuminate\Database\Schema\Blueprint;
54
use Illuminate\Database\Migrations\Migration;
5+
use Illuminate\Database\Schema\Blueprint;
66

77
class CreateCommentsTable extends Migration
88
{
9-
public function up()
9+
public function up(): void
1010
{
11-
Schema::create('comments', function (Blueprint $table) {
11+
Schema::create($this->commentsTable(), function (Blueprint $table) {
1212
$table->increments('id');
1313
$table->string('commentable_id')->nullable();
1414
$table->string('commentable_type')->nullable();
@@ -23,8 +23,15 @@ class CreateCommentsTable extends Migration
2323
});
2424
}
2525

26-
public function down()
26+
public function down(): void
27+
{
28+
Schema::drop($this->commentsTable());
29+
}
30+
31+
private function commentsTable(): string
2732
{
28-
Schema::drop('comments');
33+
$model = config('comment.model');
34+
35+
return (new $model)->getTable();
2936
}
3037
}

phpunit.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,9 @@
1313
<directory suffix=".php">./tests/</directory>
1414
</testsuite>
1515
</testsuites>
16+
<filter>
17+
<whitelist processUncoveredFilesFromWhitelist="true">
18+
<directory suffix=".php">./src</directory>
19+
</whitelist>
20+
</filter>
1621
</phpunit>

src/CanComment.php

Lines changed: 30 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,50 @@
11
<?php
2-
/**
3-
* Created by PhpStorm.
4-
* User: actuallymab
5-
* Date: 12.06.2016
6-
* Time: 02:13
7-
*/
2+
declare(strict_types=1);
83

94
namespace Actuallymab\LaravelComment;
105

11-
use Actuallymab\LaravelComment\Models\Comment;
6+
use Actuallymab\LaravelComment\Contracts\Commentable;
7+
use Illuminate\Database\Eloquent\Relations\MorphMany;
128

139
trait CanComment
1410
{
15-
16-
/**
17-
* @param $commentable
18-
* @param string $commentText
19-
* @param int $rate
20-
* @return $this
21-
*/
22-
public function comment($commentable, $commentText = '', $rate = 0)
11+
public function comment(Commentable $commentable, string $commentText = '', int $rate = 0): self
2312
{
24-
$comment = new Comment([
25-
'comment' => $commentText,
26-
'rate' => ($commentable->getCanBeRated()) ? $rate : null,
27-
'approved' => ($commentable->mustBeApproved() && ! $this->isAdmin()) ? false : true,
28-
'commented_id' => $this->id,
29-
'commented_type' => $this->getMorphClass()
30-
]);
13+
$commentModel = config('comment.model');
3114

32-
$commentable->comments()->save($comment);
15+
$commentable->comments()->save(new $commentModel([
16+
'comment' => $commentText,
17+
'rate' => $commentable->canBeRated() ? $rate : null,
18+
'approved' => $commentable->mustBeApproved() && !$this->canCommentWithoutApprove() ? false : true,
19+
'commented_id' => $this->primaryId(),
20+
'commented_type' => get_class(),
21+
]));
3322

3423
return $this;
3524
}
3625

37-
/**
38-
* @return bool
39-
*/
40-
public function isAdmin()
26+
public function canCommentWithoutApprove(): bool
4127
{
4228
return false;
4329
}
4430

45-
/**
46-
* @return \Illuminate\Database\Eloquent\Relations\MorphMany
47-
*/
48-
public function comments()
31+
public function comments(): MorphMany
32+
{
33+
return $this->morphMany(config('comment.model'), 'commented');
34+
}
35+
36+
public function hasCommentsOn(Commentable $commentable): bool
37+
{
38+
return $this->comments()
39+
->where([
40+
'commentable_id' => $commentable->primaryId(),
41+
'commentable_type' => get_class($commentable),
42+
])
43+
->exists();
44+
}
45+
46+
private function primaryId(): string
4947
{
50-
return $this->morphMany(Comment::class, 'commented');
48+
return (string)$this->getAttribute($this->primaryKey);
5149
}
5250
}

0 commit comments

Comments
 (0)