Skip to content

Commit d8f0213

Browse files
committed
improvements
1 parent 56f8e57 commit d8f0213

File tree

18 files changed

+248
-23
lines changed

18 files changed

+248
-23
lines changed

config/groupable.php

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,34 @@
2222
# models...
2323
],
2424
],
25+
'relation' => [
26+
'name' => 'groupable',
27+
],
2528
'observer' => Yuges\Groupable\Observers\GroupableObserver::class,
2629
],
30+
'grouperator' => [
31+
'key' => Yuges\Package\Enums\KeyType::BigInteger,
32+
'default' => [
33+
'class' => \App\Models\User::class,
34+
],
35+
'allowed' => [
36+
'classes' => [
37+
\App\Models\User::class,
38+
],
39+
],
40+
'relation' => [
41+
'name' => 'grouperator',
42+
],
43+
],
2744
],
2845

29-
'permissions' => [],
46+
'permissions' => [
47+
'anonymous' => false,
48+
],
3049

3150
'actions' => [
32-
// 'sync' => Yuges\Topicable\Actions\SyncTopicAction::class,
33-
// 'attach' => Yuges\Topicable\Actions\AttachTopicAction::class,
34-
// 'detach' => Yuges\Topicable\Actions\DetachTopicAction::class,
51+
'sync' => Yuges\Groupable\Actions\SyncGroupsAction::class,
52+
'attach' => Yuges\Groupable\Actions\AttachGroupsAction::class,
53+
'detach' => Yuges\Groupable\Actions\DetachGroupsAction::class,
3554
],
3655
];

database/migrations/000_create_groups_table.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,16 @@ public function up(): void
2222

2323
Schema::create($this->table, function (Blueprint $table) {
2424
$table->key(Config::getGroupKeyType(KeyType::BigInteger));
25+
26+
Config::getPermissionsAnonymous(false)
27+
? $table->nullableKeyMorphs(
28+
Config::getGrouperatorKeyType(KeyType::BigInteger),
29+
Config::getGrouperatorRelationName('grouperator')
30+
)
31+
: $table->keyMorphs(
32+
Config::getGrouperatorKeyType(KeyType::BigInteger),
33+
Config::getGrouperatorRelationName('grouperator')
34+
);
2535
});
2636

2737
Schema::table($this->table, function (Blueprint $table) {

database/migrations/001_create_groupables_table.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,22 @@ public function up(): void
2828
->cascadeOnUpdate()
2929
->cascadeOnDelete();
3030

31-
$table->keyMorphs(Config::getGroupableKeyType(KeyType::BigInteger), 'groupable');
31+
$table->keyMorphs(
32+
Config::getGroupableKeyType(KeyType::BigInteger),
33+
Config::getGroupableRelationName('groupable')
34+
);
3235

36+
Config::getPermissionsAnonymous(false)
37+
? $table->nullableKeyMorphs(
38+
Config::getGrouperatorKeyType(KeyType::BigInteger),
39+
Config::getGrouperatorRelationName('grouperator')
40+
)
41+
: $table->keyMorphs(
42+
Config::getGrouperatorKeyType(KeyType::BigInteger),
43+
Config::getGrouperatorRelationName('grouperator')
44+
);
45+
46+
$table->order();
3347
$table->unique(['group_id', 'groupable_id', 'groupable_type']);
3448

3549
$table->timestamps();

src/Actions/AttachGroupsAction.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@
55
use Yuges\Groupable\Models\Group;
66
use Illuminate\Support\Collection;
77
use Yuges\Groupable\Config\Config;
8+
use Illuminate\Database\Eloquent\Model;
89
use Yuges\Groupable\Interfaces\Groupable;
10+
use Yuges\Groupable\Interfaces\Grouperator;
11+
use Yuges\Groupable\Models\Groupable as GroupableModel;
912

1013
class AttachGroupsAction
1114
{
@@ -22,8 +25,10 @@ public static function create(Groupable $groupable): self
2225
/**
2326
* @param Collection<array-key, Group> $groups
2427
*/
25-
public function execute(Collection $groups): Groupable
28+
public function execute(Collection $groups, ?Grouperator $grouperator = null): Groupable
2629
{
30+
$grouperator ??= $this->groupable->defaultGrouperator();
31+
2732
$ids = $groups
2833
->map(function (Group $group) {
2934
return $group->id;
@@ -34,8 +39,19 @@ public function execute(Collection $groups): Groupable
3439

3540
$groups = Config::getGroupClass(Group::class)::query()->getQuery()->whereIn('id', $ids)->get();
3641

37-
$this->groupable->groups()->sync($groups->pluck('id'), false);
42+
$this->groupable->groups()->syncWithPivotValues($groups->pluck('id'), $this->pivotValues($grouperator), false);
3843

3944
return $this->groupable;
4045
}
46+
47+
public function pivotValues(?Grouperator $grouperator = null): array
48+
{
49+
$pivot = new GroupableModel();
50+
$relation = $pivot->grouperator();
51+
52+
return [
53+
$relation->getForeignKeyName() => $grouperator instanceof Model ? $grouperator->getKey() : null,
54+
$relation->getMorphType() => $grouperator instanceof Model ? $grouperator->getMorphClass() : null,
55+
];
56+
}
4157
}

src/Config/Config.php

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Yuges\Groupable\Models\Group;
77
use Illuminate\Support\Collection;
88
use Yuges\Groupable\Models\Groupable;
9+
use Yuges\Groupable\Interfaces\Grouperator;
910
use Yuges\Groupable\Observers\GroupObserver;
1011
use Yuges\Groupable\Actions\SyncGroupsAction;
1112
use Yuges\Groupable\Actions\AttachGroupsAction;
@@ -55,6 +56,11 @@ public static function getGroupableKeyType(mixed $default = null): KeyType
5556
return self::get('models.groupable.key', $default);
5657
}
5758

59+
public static function getGroupableRelationName(mixed $default = null): string
60+
{
61+
return self::get('models.groupable.relation.name', $default);
62+
}
63+
5864
/** @return Collection<array-key, class-string<Groupable>> */
5965
public static function getGroupableAllowedClasses(mixed $default = null): Collection
6066
{
@@ -66,7 +72,36 @@ public static function getGroupableAllowedClasses(mixed $default = null): Collec
6672
/** @return class-string<GroupableObserver> */
6773
public static function getGroupableObserverClass(mixed $default = null): string
6874
{
69-
return self::get('models.Groupable.observer', $default);
75+
return self::get('models.groupable.observer', $default);
76+
}
77+
78+
public static function getGrouperatorKeyType(mixed $default = null): KeyType
79+
{
80+
return self::get('models.grouperator.key', $default);
81+
}
82+
83+
public static function getGrouperatorRelationName(mixed $default = null): string
84+
{
85+
return self::get('models.grouperator.relation.name', $default);
86+
}
87+
88+
/** @return class-string<Grouperator> */
89+
public static function getGrouperatorDefaultClass(mixed $default = null): string
90+
{
91+
return self::get('models.grouperator.default.class', $default);
92+
}
93+
94+
/** @return Collection<array-key, class-string<Grouperator>> */
95+
public static function getGrouperatorAllowedClasses(mixed $default = null): Collection
96+
{
97+
return Collection::make(
98+
self::get('models.grouperator.allowed.classes', $default)
99+
);
100+
}
101+
102+
public static function getPermissionsAnonymous(mixed $default = false): bool
103+
{
104+
return self::get('permissions.anonymous', $default);
70105
}
71106

72107
public static function getSyncGroupsAction(

src/Interfaces/Groupable.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,6 @@ public function detachGroup(Group $group): static;
2323
public function detachGroups(Collection $groups): static;
2424

2525
public function syncGroups(Collection $groups): static;
26+
27+
public function defaultGrouperator(): ?Grouperator;
2628
}

src/Interfaces/Grouperator.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace Yuges\Groupable\Interfaces;
4+
5+
use Illuminate\Database\Eloquent\Relations\MorphToMany;
6+
7+
interface Grouperator
8+
{
9+
public function groups(): MorphToMany;
10+
}

src/Models/Group.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Yuges\Groupable\Traits\HasParent;
99
use Yuges\Sluggable\Options\SlugOptions;
1010
use Yuges\Sluggable\Interfaces\Sluggable;
11+
use Yuges\Groupable\Traits\HasGrouperator;
1112
use Illuminate\Database\Eloquent\Factories\HasFactory;
1213

1314
/**
@@ -17,7 +18,7 @@
1718
*/
1819
class Group extends Model implements Sluggable
1920
{
20-
use HasFactory, HasParent, HasSlug;
21+
use HasFactory, HasParent, HasGrouperator, HasSlug;
2122

2223
protected $table = 'groups';
2324

src/Models/Groupable.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@
55
use Yuges\Groupable\Config\Config;
66
use Yuges\Package\Traits\HasTable;
77
use Yuges\Package\Traits\HasTimestamps;
8+
use Yuges\Groupable\Traits\HasGrouperator;
89
use Illuminate\Database\Eloquent\Relations\MorphTo;
910
use Illuminate\Database\Eloquent\Factories\HasFactory;
1011
use Illuminate\Database\Eloquent\Relations\MorphPivot;
1112

1213
class Groupable extends MorphPivot
1314
{
14-
use HasFactory, HasTable, HasTimestamps;
15+
use HasFactory, HasTable, HasTimestamps, HasGrouperator;
1516

1617
public $table = 'groupables';
1718

src/Observers/GroupObserver.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,17 @@
22

33
namespace Yuges\Groupable\Observers;
44

5-
use Yuges\Package\Models\Model;
5+
use Yuges\Groupable\Models\Group;
6+
use Yuges\Groupable\Config\Config;
67

78
class GroupObserver
89
{
9-
public function saving(Model $model): void
10+
public function saving(Group $group): void
1011
{
1112

1213
}
1314

14-
public function deleted(Model $model): void
15+
public function deleted(Group $group): void
1516
{
1617

1718
}

0 commit comments

Comments
 (0)