Skip to content

Commit

Permalink
Fixed #25 #31 #32
Browse files Browse the repository at this point in the history
  • Loading branch information
overtrue committed Dec 23, 2020
1 parent ad19381 commit 7a4a9ec
Show file tree
Hide file tree
Showing 15 changed files with 119 additions and 169 deletions.
50 changes: 31 additions & 19 deletions .php_cs
Original file line number Diff line number Diff line change
@@ -1,36 +1,48 @@
<?php

$header = <<<EOF
This file is part of the overtrue/laravel-like
(c) overtrue <i@overtrue.me>
This source file is subject to the MIT license that is bundled
with this source code in the file LICENSE.
EOF;

return PhpCsFixer\Config::create()
->setRules([
'@PSR2' => true,
'header_comment' => ['header' => $header],
'binary_operator_spaces' => true,
'blank_line_after_opening_tag' => true,
'braces' => ['allow_single_line_closure' => true],
'compact_nullable_typehint' => true,
'concat_space' => ['spacing' => 'one'],
'declare_equal_normalize' => ['space' => 'none'],
'function_typehint_space' => true,
'declare_equal_normalize' => true,
'lowercase_cast' => true,
'lowercase_static_reference' => true,
'new_with_braces' => true,
'method_argument_space' => ['on_multiline' => 'ensure_fully_multiline'],
'no_empty_statement' => true,
'no_blank_lines_after_class_opening' => true,
'no_leading_import_slash' => true,
'no_leading_namespace_whitespace' => true,
'no_whitespace_in_blank_line' => true,
'return_type_declaration' => ['space_before' => 'none'],
'ordered_class_elements' => [
'order' => [
'use_trait',
],
],
'ordered_imports' => [
'imports_order' => [
'class',
'function',
'const',
],
'sort_algorithm' => 'none',
],
'return_type_declaration' => true,
'short_scalar_cast' => true,
'single_blank_line_before_namespace' => true,
'single_trait_insert_per_statement' => true,
'ternary_operator_spaces' => true,
'unary_operator_spaces' => true,
'visibility_required' => [
'elements' => [
'const',
'method',
'property',
],
],
])
->setFinder(
PhpCsFixer\Finder::create()
->exclude('vendor')
->in([__DIR__.'/src/'])
->in([__DIR__.'/src/', __DIR__.'/tests/'])
)
;
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,18 @@ $post->likers()->count();
List with `*_count` attribute:

```php
// likes_count
$users = User::withCount('likes')->get();

foreach($users as $user) {
echo $user->likes_count;
// $user->likes_count;
}

// likers_count
$posts = User::withCount('likers')->get();

foreach($posts as $post) {
// $post->likes_count;
}
```

Expand Down
20 changes: 8 additions & 12 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
}
],
"require": {
"laravel/framework": "^5.5||~6.0||^7.0"
"laravel/framework": "^7.0 || ^8.0"
},
"autoload": {
"psr-4": {
Expand All @@ -22,12 +22,12 @@
}
},
"require-dev": {
"laravel/framework": "^5.5",
"laravel/framework": "^8.0",
"mockery/mockery": "^1.2",
"phpunit/phpunit": "~8.0",
"orchestra/testbench": "^3.7",
"phpunit/phpunit": "^9.3.8",
"orchestra/testbench": "^6.7.0",
"brainmaestro/composer-git-hooks": "^2.7",
"friendsofphp/php-cs-fixer": "^2.16"
"friendsofphp/php-cs-fixer": "^2.17.2"
},
"extra": {
"laravel": {
Expand All @@ -37,20 +37,16 @@
},
"hooks": {
"pre-commit": [
"composer test",
"composer fix-style"
"composer fix-style",
"composer test"
],
"pre-push": [
"composer test",
"composer check-style"
]
"post-merge": "composer install"
}
},
"scripts": {
"post-update-cmd": [
"cghooks update"
],
"post-merge": "composer install",
"post-install-cmd": [
"cghooks add --ignore-lock",
"cghooks update"
Expand Down
8 changes: 0 additions & 8 deletions config/like.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
<?php

/*
* This file is part of the overtrue/laravel-like.
*
* (c) overtrue <anzhengchao@gmail.com>
*
* This source file is subject to the MIT license that is bundled.
*/

return [
/*
* User tables foreign key name.
Expand Down
8 changes: 0 additions & 8 deletions migrations/2018_12_14_000000_create_likes_table.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
<?php

/*
* This file is part of the overtrue/laravel-like.
*
* (c) overtrue <anzhengchao@gmail.com>
*
* This source file is subject to the MIT license that is bundled.
*/

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
Expand Down
9 changes: 0 additions & 9 deletions src/Events/Event.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,5 @@
<?php

/*
* This file is part of the overtrue/laravel-like
*
* (c) overtrue <i@overtrue.me>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace Overtrue\LaravelLike\Events;

use Illuminate\Database\Eloquent\Model;
Expand Down
9 changes: 0 additions & 9 deletions src/Events/Liked.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,5 @@
<?php

/*
* This file is part of the overtrue/laravel-like
*
* (c) overtrue <i@overtrue.me>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace Overtrue\LaravelLike\Events;

class Liked extends Event
Expand Down
9 changes: 0 additions & 9 deletions src/Events/Unliked.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,5 @@
<?php

/*
* This file is part of the overtrue/laravel-like
*
* (c) overtrue <i@overtrue.me>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace Overtrue\LaravelLike\Events;

class Unliked extends Event
Expand Down
16 changes: 1 addition & 15 deletions src/Like.php
Original file line number Diff line number Diff line change
@@ -1,24 +1,12 @@
<?php

/*
* This file is part of the overtrue/laravel-like
*
* (c) overtrue <i@overtrue.me>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace Overtrue\LaravelLike;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Overtrue\LaravelLike\Events\Liked;
use Overtrue\LaravelLike\Events\Unliked;

/**
* Class Like.
*/
class Like extends Model
{
protected $dispatchesEvents = [
Expand All @@ -27,8 +15,6 @@ class Like extends Model
];

/**
* Like constructor.
*
* @param array $attributes
*/
public function __construct(array $attributes = [])
Expand All @@ -48,7 +34,7 @@ protected static function boot()
});
}

public function likeable()
public function likeable(): \Illuminate\Database\Eloquent\Relations\MorphTo
{
return $this->morphTo();
}
Expand Down
12 changes: 0 additions & 12 deletions src/LikeServiceProvider.php
Original file line number Diff line number Diff line change
@@ -1,21 +1,9 @@
<?php

/*
* This file is part of the overtrue/laravel-like
*
* (c) overtrue <i@overtrue.me>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace Overtrue\LaravelLike;

use Illuminate\Support\ServiceProvider;

/**
* Class LikeServiceProvider.
*/
class LikeServiceProvider extends ServiceProvider
{
/**
Expand Down
27 changes: 3 additions & 24 deletions src/Traits/Likeable.php
Original file line number Diff line number Diff line change
@@ -1,56 +1,35 @@
<?php

/*
* This file is part of the overtrue/laravel-like
*
* (c) overtrue <i@overtrue.me>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace Overtrue\LaravelLike\Traits;

use Illuminate\Database\Eloquent\Model;

/**
* Trait Likeable.
*/
trait Likeable
{
/**
* @param \Illuminate\Database\Eloquent\Model $user
*
* @return bool
*/
public function isLikedBy(Model $user)
public function isLikedBy(Model $user): bool
{
if (\is_a($user, config('auth.providers.users.model'))) {
if ($this->relationLoaded('likers')) {
return $this->likers->contains($user);
}

return ($this->relationLoaded('likes') ? $this->likes : $this->likes())
->where(\config('like.user_foreign_key'), $user->getKey())->count() > 0;
return $this->likers()->where(\config('like.user_foreign_key'), $user->getKey())->exists();
}

return false;
}

/**
* @return \Illuminate\Database\Eloquent\Relations\MorphMany
*/
public function likes()
{
return $this->morphMany(config('like.like_model'), 'likeable');
}

/**
* Return followers.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function likers()
public function likers(): \Illuminate\Database\Eloquent\Relations\BelongsToMany
{
return $this->belongsToMany(
config('auth.providers.users.model'),
Expand Down
Loading

0 comments on commit 7a4a9ec

Please sign in to comment.