Skip to content

Commit 56f8e57

Browse files
committed
actions, traits
1 parent 6bac86c commit 56f8e57

File tree

11 files changed

+310
-1
lines changed

11 files changed

+310
-1
lines changed

src/Actions/.gitkeep

Whitespace-only changes.

src/Actions/AttachGroupsAction.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
namespace Yuges\Groupable\Actions;
4+
5+
use Yuges\Groupable\Models\Group;
6+
use Illuminate\Support\Collection;
7+
use Yuges\Groupable\Config\Config;
8+
use Yuges\Groupable\Interfaces\Groupable;
9+
10+
class AttachGroupsAction
11+
{
12+
public function __construct(
13+
protected Groupable $groupable
14+
) {
15+
}
16+
17+
public static function create(Groupable $groupable): self
18+
{
19+
return new static($groupable);
20+
}
21+
22+
/**
23+
* @param Collection<array-key, Group> $groups
24+
*/
25+
public function execute(Collection $groups): Groupable
26+
{
27+
$ids = $groups
28+
->map(function (Group $group) {
29+
return $group->id;
30+
})
31+
->filter(function (mixed $value) {
32+
return (bool) $value;
33+
});
34+
35+
$groups = Config::getGroupClass(Group::class)::query()->getQuery()->whereIn('id', $ids)->get();
36+
37+
$this->groupable->groups()->sync($groups->pluck('id'), false);
38+
39+
return $this->groupable;
40+
}
41+
}

src/Actions/DetachGroupsAction.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
namespace Yuges\Groupable\Actions;
4+
5+
use Yuges\Groupable\Models\Group;
6+
use Illuminate\Support\Collection;
7+
use Yuges\Groupable\Interfaces\Groupable;
8+
9+
class DetachGroupsAction
10+
{
11+
public function __construct(
12+
protected Groupable $groupable
13+
) {
14+
}
15+
16+
public static function create(Groupable $groupable): self
17+
{
18+
return new static($groupable);
19+
}
20+
21+
/**
22+
* @param Collection<array-key, Group> $groups
23+
*/
24+
public function execute(Collection $groups): Groupable
25+
{
26+
$ids = $groups
27+
->map(function (Group $group) {
28+
return $group->id;
29+
})
30+
->filter(function (mixed $value) {
31+
return (bool) $value;
32+
});
33+
34+
$this->groupable->groups()->detach($ids);
35+
36+
return $this->groupable;
37+
}
38+
}

src/Actions/SyncGroupsAction.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
namespace Yuges\Groupable\Actions;
4+
5+
use Yuges\Groupable\Models\Group;
6+
use Illuminate\Support\Collection;
7+
use Yuges\Groupable\Interfaces\Groupable;
8+
9+
class SyncGroupsAction
10+
{
11+
public function __construct(
12+
protected Groupable $groupable
13+
) {
14+
}
15+
16+
public static function create(Groupable $groupable): self
17+
{
18+
return new static($groupable);
19+
}
20+
21+
/**
22+
* @param Collection<array-key, Group> $groups
23+
*/
24+
public function execute(Collection $groups): Groupable
25+
{
26+
$ids = $groups
27+
->map(function (Group $group) {
28+
return $group->id;
29+
})
30+
->filter(function (mixed $value) {
31+
return (bool) $value;
32+
});
33+
34+
$this->groupable->groups()->sync($ids);
35+
36+
return $this->groupable;
37+
}
38+
}

src/Config/Config.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@
77
use Illuminate\Support\Collection;
88
use Yuges\Groupable\Models\Groupable;
99
use Yuges\Groupable\Observers\GroupObserver;
10+
use Yuges\Groupable\Actions\SyncGroupsAction;
11+
use Yuges\Groupable\Actions\AttachGroupsAction;
12+
use Yuges\Groupable\Actions\DetachGroupsAction;
1013
use Yuges\Groupable\Observers\GroupableObserver;
14+
use Yuges\Groupable\Interfaces\Groupable as GroupableInterface;
1115

1216
class Config extends \Yuges\Package\Config\Config
1317
{
@@ -64,4 +68,46 @@ public static function getGroupableObserverClass(mixed $default = null): string
6468
{
6569
return self::get('models.Groupable.observer', $default);
6670
}
71+
72+
public static function getSyncGroupsAction(
73+
GroupableInterface $groupable,
74+
mixed $default = null
75+
): SyncGroupsAction
76+
{
77+
return self::getSyncGroupsActionClass($default)::create($groupable);
78+
}
79+
80+
/** @return class-string<SyncGroupsAction> */
81+
public static function getSyncGroupsActionClass(mixed $default = null): string
82+
{
83+
return self::get('actions.sync', $default);
84+
}
85+
86+
public static function getAttachGroupsAction(
87+
GroupableInterface $groupable,
88+
mixed $default = null
89+
): AttachGroupsAction
90+
{
91+
return self::getAttachGroupsActionClass($default)::create($groupable);
92+
}
93+
94+
/** @return class-string<AttachGroupsAction> */
95+
public static function getAttachGroupsActionClass(mixed $default = null): string
96+
{
97+
return self::get('actions.attach', $default);
98+
}
99+
100+
public static function getDetachGroupsAction(
101+
GroupableInterface $groupable,
102+
mixed $default = null
103+
): DetachGroupsAction
104+
{
105+
return self::getDetachGroupsActionClass($default)::create($groupable);
106+
}
107+
108+
/** @return class-string<DetachGroupsAction> */
109+
public static function getDetachGroupsActionClass(mixed $default = null): string
110+
{
111+
return self::get('actions.detach', $default);
112+
}
67113
}

src/Interfaces/.gitkeep

Whitespace-only changes.

src/Interfaces/Groupable.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace Yuges\Groupable\Interfaces;
4+
5+
use Yuges\Groupable\Models\Group;
6+
use Illuminate\Support\Collection;
7+
use Illuminate\Database\Eloquent\Relations\MorphToMany;
8+
9+
interface Groupable
10+
{
11+
public function groups(): MorphToMany;
12+
13+
public function group(Group $group): static;
14+
15+
public function ungroup(Group $group): static;
16+
17+
public function attachGroup(Group $group): static;
18+
19+
public function attachGroups(Collection $groups): static;
20+
21+
public function detachGroup(Group $group): static;
22+
23+
public function detachGroups(Collection $groups): static;
24+
25+
public function syncGroups(Collection $groups): static;
26+
}

src/Models/Group.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Yuges\Package\Models\Model;
66
use Yuges\Groupable\Config\Config;
77
use Yuges\Sluggable\Traits\HasSlug;
8+
use Yuges\Groupable\Traits\HasParent;
89
use Yuges\Sluggable\Options\SlugOptions;
910
use Yuges\Sluggable\Interfaces\Sluggable;
1011
use Illuminate\Database\Eloquent\Factories\HasFactory;
@@ -16,7 +17,7 @@
1617
*/
1718
class Group extends Model implements Sluggable
1819
{
19-
use HasFactory, HasSlug;
20+
use HasFactory, HasParent, HasSlug;
2021

2122
protected $table = 'groups';
2223

src/Traits/.gitkeep

Whitespace-only changes.

src/Traits/HasGroups.php

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<?php
2+
3+
namespace Yuges\Groupable\Traits;
4+
5+
use Yuges\Groupable\Models\Group;
6+
use Illuminate\Support\Collection;
7+
use Yuges\Groupable\Config\Config;
8+
use Yuges\Groupable\Models\Groupable;
9+
use Illuminate\Database\Eloquent\Model;
10+
use Illuminate\Database\Eloquent\Relations\MorphToMany;
11+
12+
/**
13+
* @property Collection<array-key, Group> $groups
14+
*/
15+
trait HasGroups
16+
{
17+
public function groups(): MorphToMany
18+
{
19+
/** @var Model $this */
20+
return $this
21+
->morphToMany(Config::getGroupClass(Group::class), 'groupable')
22+
->using(Config::getGroupableClass(Groupable::class))
23+
->withTimestamps();
24+
}
25+
26+
public function group(Group $group): static
27+
{
28+
return $this->attachGroup($group);
29+
}
30+
31+
public function ungroup(Group $group): static
32+
{
33+
return $this->detachGroup($group);
34+
}
35+
36+
public function attachGroup(Group $group): static
37+
{
38+
$this->attachGroups(Collection::make([$group]));
39+
40+
return $this;
41+
}
42+
43+
/**
44+
* @param Collection<array-key, Group> $groups
45+
*/
46+
public function attachGroups(Collection $groups): static
47+
{
48+
Config::getAttachGroupsAction($this)->execute($groups);
49+
50+
return $this;
51+
}
52+
53+
public function detachGroup(Group $group): static
54+
{
55+
$this->detachGroups(Collection::make([$group]));
56+
57+
return $this;
58+
}
59+
60+
/**
61+
* @param Collection<array-key, Group> $groups
62+
*/
63+
public function detachGroups(Collection $groups): static
64+
{
65+
Config::getDetachGroupsAction($this)->execute($groups);
66+
67+
return $this;
68+
}
69+
70+
/**
71+
* @param Collection<array-key, Group> $groups
72+
*/
73+
public function syncGroups(Collection $groups): static
74+
{
75+
Config::getSyncGroupsAction($this)->execute($groups);
76+
77+
return $this;
78+
}
79+
}

0 commit comments

Comments
 (0)