Skip to content

Commit 6f013dd

Browse files
author
miladimos
committed
Add some traits
1 parent bd24d82 commit 6f013dd

15 files changed

+242
-10
lines changed

src/Traits/HasSlug.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
<?php
22

3-
namespace Miladimos\Toolkit\Traits;
3+
namespace App\Traits;
44

55
use Illuminate\Support\Str;
66

7-
trait HasSlug
7+
trait hasSlug
88
{
99
public function slug(): string
1010
{
@@ -28,7 +28,7 @@ private function generateUniqueSlug(string $value): string
2828

2929
while ($this->slugExists($slug, $this->exists ? $this->id() : null)) {
3030
$counter++;
31-
$slug = $originalSlug . '-' . $counter;
31+
$slug = $originalSlug.'-'.$counter;
3232
}
3333

3434
return $slug;

src/Traits/HasUUID.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,8 @@
33
namespace Miladimos\Toolkit\Traits;
44

55
use Webpatser\Uuid\Uuid;
6-
7-
trait HasUUID
6+
trait hasUUID
87
{
9-
108
protected static function boot()
119
{
1210
parent::boot();

src/Traits/RouteKeyNameUUID.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Miladimos\Toolkit\Traits;
44

5-
trait RouteKeyNameUUID
5+
trait routeKeyNameUUID
66
{
77
/**
88
* Get the route key for the model.

src/Traits/ValidateFiles.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
namespace Miladimos\Toolkit\Traits;
44

55

6-
trait ValidateFiles
6+
trait validateFiles
77
{
88

99
protected function ensureHelperDoesntAlreadytExist($file)

src/Traits/apiResponder.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace App\Traits;
4+
5+
trait apiResponder
6+
{
7+
protected function responseSuccess($data, $statusCode = 200, $statusMessage = "Ok")
8+
{
9+
return response()->json([
10+
'success' => true,
11+
'status_code' => $statusCode,
12+
'status_message' => $statusMessage,
13+
'data' => $data,
14+
], $statusCode);
15+
}
16+
17+
protected function responseError($data, $statusCode = 500, $statusMessage = "Error")
18+
{
19+
return response()->json([
20+
'success' => false,
21+
'status_code' => $statusCode,
22+
'status_message' => $statusMessage,
23+
'data' => $data,
24+
], $statusCode);
25+
}
26+
}

src/Traits/getConstantsEnum.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace App\Traits;
4+
5+
use ReflectionClass;
6+
7+
trait getConstantsEnum
8+
{
9+
public static function getConstants()
10+
{
11+
$reflectionClass = new ReflectionClass(static::class); // __CLASS__
12+
return $reflectionClass->getConstants();
13+
}
14+
}

src/Traits/getStubs.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Miladimos\Toolkit\Traits;
44

5-
trait GetStubs
5+
trait getStubs
66
{
77
protected static function getHelperStub()
88
{

src/Traits/hasAuthor.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace App\Traits;
4+
5+
use Illuminate\Database\Eloquent\Relations\BelongsTo;
6+
7+
use App\Models\User;
8+
9+
trait hasAuthor
10+
{
11+
public function author(): User
12+
{
13+
return $this->authorRelation;
14+
}
15+
16+
public function authorRelation(): BelongsTo
17+
{
18+
return $this->belongsTo(User::class, 'author_id');
19+
}
20+
21+
public function authoredBy(User $author)
22+
{
23+
$this->authorRelation()->associate($author);
24+
}
25+
26+
public function isAuthoredBy(User $user): bool
27+
{
28+
return $this->author()->matches($user);
29+
}
30+
}

src/Traits/hasComment.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace App\Traits;
4+
5+
use App\Models\Comment;
6+
use Illuminate\Database\Eloquent\Relations\MorphMany;
7+
8+
trait hasComment
9+
{
10+
11+
public function comments()
12+
{
13+
return $this->commentsRelation;
14+
}
15+
16+
public function commentsRelation(): MorphMany
17+
{
18+
return $this->morphMany(Comment::class, 'commentable', 'commentables')->withTimestamps();
19+
}
20+
21+
// /**
22+
// * @param \App\Models\Comment[]|int[] $comments
23+
// */
24+
// public function syncComments(array $comments)
25+
// {
26+
// $this->save();
27+
// $this->commentsRelation()->sync($comments);
28+
// }
29+
30+
// public function removeComments()
31+
// {
32+
// $this->commentsRelation()->detach();
33+
// }
34+
}

src/Traits/hasJWT.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace App\Traits;
4+
5+
trait hasJWT
6+
{
7+
public function getJWTIdentifier(): int
8+
{
9+
return $this->getKey();
10+
}
11+
12+
public function getJWTCustomClaims(): array
13+
{
14+
return [];
15+
}
16+
}

0 commit comments

Comments
 (0)