Skip to content

Commit

Permalink
Merge branch 'master' into sub-anonymous-l5.5
Browse files Browse the repository at this point in the history
# Conflicts:
#	src/CanComment.php
#	src/Models/Comment.php
  • Loading branch information
Igor Karpov committed Oct 2, 2021
2 parents d432c3d + b557772 commit e51ed6b
Show file tree
Hide file tree
Showing 22 changed files with 476 additions and 359 deletions.
31 changes: 31 additions & 0 deletions .scrutinizer.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
build:
nodes:
analysis:
project_setup:
override: true
tests:
override: [php-scrutinizer-run]

filter:
excluded_paths: [tests/*]

checks:
php:
remove_extra_empty_lines: true
remove_php_closing_tag: true
remove_trailing_whitespace: true
fix_use_statements:
remove_unused: true
preserve_multiple: false
preserve_blanklines: true
order_alphabetically: true
fix_php_opening_tag: true
fix_linefeed: true
fix_line_ending: true
fix_identation_4spaces: true
fix_doc_comments: true

tools:
external_code_coverage:
timeout: 600
runs: 1
9 changes: 7 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
language: php

php:
- 7.0
- 7.1
- 7.2

before_script:
- composer self-update
- composer install --prefer-source --no-interaction

script: vendor/bin/phpunit
script:
- vendor/bin/phpunit --coverage-text --coverage-clover=coverage.clover

after_script:
- php vendor/bin/ocular code-coverage:upload --format=php-clover coverage.clover
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
109 changes: 75 additions & 34 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
[![Software License][ico-license]](LICENSE.md)
[![Build Status][ico-travis]][link-travis]
[![Total Downloads][ico-downloads]][link-downloads]
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/actuallymab/laravel-comment/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/actuallymab/laravel-comment/?branch=master)
[![Code Coverage](https://scrutinizer-ci.com/g/actuallymab/laravel-comment/badges/coverage.png?b=master)](https://scrutinizer-ci.com/g/actuallymab/laravel-comment/?branch=master)

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

## Version Compatibility

Expand All @@ -16,7 +18,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

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

## Install

Expand All @@ -26,98 +29,136 @@ Via Composer
$ composer require actuallymab/laravel-comment
```

Add service provider to your app.php file
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;

class User extends Model
{
use 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
25 changes: 13 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,18 @@
"authors": [
{
"name": "Mehmet Aydin Bahadir",
"email": "[email protected]",
"homepage": "http://www.actuallymab.me",
"role": "Software Engineer"
"email": "[email protected]"
}
],
"require": {
"illuminate/database": "5.5.*",
"php" : ">=7.0.0"
"php": "^7.1.3",
"illuminate/database": "~5.5.0|~5.6.0|~5.7.0|~5.8.0|^6.0|^7.0|^8.0"
},
"require-dev": {
"fzaninotto/faker": "~1.4",
"squizlabs/php_codesniffer": "^2.3",
"phpunit/phpunit" : "6.*",
"mockery/mockery": "~0.9.0",
"orchestra/testbench": "~3.5",
"orchestra/database": "3.5.x@dev"
"phpunit/phpunit": "^7.4",
"orchestra/testbench": "~3.5.0|~3.6.0|~3.7.0|~3.8.0|^4.0|^5.0|^6.0",
"fzaninotto/faker": "^1.8",
"scrutinizer/ocular": "^1.5"
},
"autoload": {
"psr-4": {
Expand All @@ -46,6 +42,11 @@
"extra": {
"branch-alias": {
"dev-master": "1.0-dev"
},
"laravel": {
"providers": [
"\\Actuallymab\\LaravelComment\\LaravelCommentServiceProvider"
]
}
}
}
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,
];
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 @@ -26,8 +26,15 @@ public function up()
});
}

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();
}
}
10 changes: 7 additions & 3 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,15 @@
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
syntaxCheck="false">
stopOnFailure="false">
<testsuites>
<testsuite name="Laravel Comment Test Suite">
<directory suffix=".php">./tests/</directory>
</testsuite>
</testsuites>
</phpunit>
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">./src</directory>
</whitelist>
</filter>
</phpunit>
Loading

0 comments on commit e51ed6b

Please sign in to comment.