Skip to content

Commit 73fd21b

Browse files
authored
Merge pull request #52 from mubbi/develop
Develop to main
2 parents 36f4896 + eea0527 commit 73fd21b

32 files changed

+405
-201
lines changed

app/Http/Controllers/Api/V1/Article/GetCommentsController.php

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,29 @@ public function __invoke(GetCommentsRequest $request, Article $article): JsonRes
3333
$params = $request->withDefaults();
3434

3535
try {
36-
$parentId = $params['parent_id'] !== null ? (int) $params['parent_id'] : null;
36+
/** @var mixed $parentIdInput */
37+
$parentIdInput = $request->input('parent_id');
38+
/** @var mixed $commentIdInput */
39+
$commentIdInput = $request->input('comment_id');
40+
/** @var mixed $userIdInput */
41+
$userIdInput = $request->input('user_id');
42+
$parentId = is_numeric($parentIdInput) ? (int) $parentIdInput : null;
43+
$commentId = is_numeric($commentIdInput) ? (int) $commentIdInput : null;
44+
$userId = is_numeric($userIdInput) ? (int) $userIdInput : null;
3745

46+
/** @var mixed $perPageParam */
47+
$perPageParam = $params['per_page'] ?? 10;
48+
/** @var mixed $pageParam */
49+
$pageParam = $params['page'] ?? 1;
50+
$perPage = (int) $perPageParam;
51+
$page = (int) $pageParam;
3852
$commentsDataResponse = CommentResource::collection($this->articleService->getArticleComments(
3953
$article->id,
4054
$parentId,
41-
(int) $params['per_page'],
42-
(int) $params['page']
55+
$perPage,
56+
$page
4357
));
44-
/** @var array{data: array, meta: array} $commentsData */
58+
/** @var array{data: array<int, mixed>, meta: array<string, mixed>} $commentsData */
4559
$commentsData = $commentsDataResponse->response()->getData(true);
4660

4761
return response()->apiSuccess(

app/Http/Middleware/OptionalSanctumAuthenticate.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public function handle(Request $request, Closure $next): mixed
2727
$token = $request->bearerToken();
2828
if (is_string($token) && $token !== '') {
2929
$accessToken = PersonalAccessToken::findToken($token);
30-
if ($accessToken !== null) {
30+
if ($accessToken !== null && $accessToken instanceof PersonalAccessToken) {
3131
$tokenable = $accessToken->tokenable;
3232
// Check for 'access-api' ability
3333
if ($tokenable instanceof User && $accessToken->can('access-api')) {

app/Http/Resources/V1/Article/ArticleResource.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,11 @@ public function toArray(Request $request): array
3030
'content_markdown' => $this->content_markdown,
3131
'featured_image' => $this->featured_image,
3232
'status' => $this->status,
33-
'published_at' => $this->published_at?->toISOString(),
33+
'published_at' => ($this->published_at instanceof \DateTimeInterface ? $this->published_at->toISOString() : $this->published_at),
3434
'meta_title' => $this->meta_title,
3535
'meta_description' => $this->meta_description,
36-
'created_at' => $this->created_at?->toISOString(),
37-
'updated_at' => $this->updated_at?->toISOString(),
36+
'created_at' => ($this->created_at instanceof \DateTimeInterface ? $this->created_at->toISOString() : $this->created_at),
37+
'updated_at' => ($this->updated_at instanceof \DateTimeInterface ? $this->updated_at->toISOString() : $this->updated_at),
3838

3939
// Relationships
4040
// Original Author

app/Http/Resources/V1/Auth/UserResource.php

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ public function toArray(Request $request): array
4040
$roles = $this->resource->roles;
4141

4242
foreach ($roles as $role) {
43-
/** @var \App\Models\Role $role */
4443
foreach ($role->permissions as $permission) {
4544
/** @var \App\Models\Permission $permission */
4645
$permissionSlugs[] = $permission->slug;
@@ -54,11 +53,23 @@ public function toArray(Request $request): array
5453
fn () => [
5554
'access_token' => $this->resource->getAttributes()['access_token'],
5655
'refresh_token' => $this->resource->getAttributes()['refresh_token'] ?? null,
57-
'access_token_expires_at' => optional($this->resource->getAttributes()['access_token_expires_at'] ?? null)?->toISOString(),
58-
'refresh_token_expires_at' => optional($this->resource->getAttributes()['refresh_token_expires_at'] ?? null)?->toISOString(),
56+
'access_token_expires_at' => $this->formatDateTime($this->resource->getAttributes()['access_token_expires_at'] ?? null),
57+
'refresh_token_expires_at' => $this->formatDateTime($this->resource->getAttributes()['refresh_token_expires_at'] ?? null),
5958
'token_type' => 'Bearer',
6059
]
6160
),
6261
];
6362
}
63+
64+
/**
65+
* Format a datetime value to ISO string if it's a DateTimeInterface.
66+
*/
67+
private function formatDateTime(mixed $value): mixed
68+
{
69+
if ($value instanceof \DateTimeInterface) {
70+
return $value->format('Y-m-d\TH:i:s.v\Z');
71+
}
72+
73+
return $value;
74+
}
6475
}

app/Models/Article.php

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
*
3131
* @mixin \Eloquent
3232
*
33-
* @use HasFactory<Article>
33+
* @phpstan-use \Illuminate\Database\Eloquent\Factories\HasFactory<self>
3434
*/
3535
final class Article extends Model
3636
{
@@ -52,58 +52,79 @@ protected function casts(): array
5252
}
5353

5454
/**
55-
* @return BelongsTo<User,Article>
55+
* @return BelongsTo<User, Article>
5656
*/
5757
public function author(): BelongsTo
5858
{
59-
return $this->belongsTo(User::class, 'created_by');
59+
/** @var BelongsTo<User, Article> $relation */
60+
$relation = $this->belongsTo(User::class, 'created_by');
61+
62+
return $relation;
6063
}
6164

6265
/**
63-
* @return BelongsTo<User,Article>
66+
* @return BelongsTo<User, Article>
6467
*/
6568
public function approver(): BelongsTo
6669
{
67-
return $this->belongsTo(User::class, 'approved_by');
70+
/** @var BelongsTo<User, Article> $relation */
71+
$relation = $this->belongsTo(User::class, 'approved_by');
72+
73+
return $relation;
6874
}
6975

7076
/**
71-
* @return BelongsTo<User,Article>
77+
* @return BelongsTo<User, Article>
7278
*/
7379
public function updater(): BelongsTo
7480
{
75-
return $this->belongsTo(User::class, 'updated_by');
81+
/** @var BelongsTo<User, Article> $relation */
82+
$relation = $this->belongsTo(User::class, 'updated_by');
83+
84+
return $relation;
7685
}
7786

7887
/**
79-
* @return HasMany<Comment,Article>
88+
* @return HasMany<Comment, Article>
8089
*/
8190
public function comments(): HasMany
8291
{
83-
return $this->hasMany(Comment::class);
92+
/** @var HasMany<Comment, Article> $relation */
93+
$relation = $this->hasMany(Comment::class);
94+
95+
return $relation;
8496
}
8597

8698
/**
87-
* @return BelongsToMany<Category,Article>
99+
* @return BelongsToMany<Category, Article, \Illuminate\Database\Eloquent\Relations\Pivot, 'pivot'>
88100
*/
89101
public function categories(): BelongsToMany
90102
{
91-
return $this->belongsToMany(Category::class, 'article_categories');
103+
/** @var BelongsToMany<Category, Article, \Illuminate\Database\Eloquent\Relations\Pivot, 'pivot'> $relation */
104+
$relation = $this->belongsToMany(Category::class, 'article_categories');
105+
106+
return $relation;
92107
}
93108

94109
/**
95-
* @return BelongsToMany<Tag,Article>
110+
* @return BelongsToMany<Tag, Article, \Illuminate\Database\Eloquent\Relations\Pivot, 'pivot'>
96111
*/
97112
public function tags(): BelongsToMany
98113
{
99-
return $this->belongsToMany(Tag::class, 'article_tags');
114+
/** @var BelongsToMany<Tag, Article, \Illuminate\Database\Eloquent\Relations\Pivot, 'pivot'> $relation */
115+
$relation = $this->belongsToMany(Tag::class, 'article_tags');
116+
117+
return $relation;
100118
}
101119

102120
/**
103-
* @return BelongsToMany<User,Article>
121+
* @return BelongsToMany<User, Article, \Illuminate\Database\Eloquent\Relations\Pivot, 'pivot'>
104122
*/
105123
public function authors(): BelongsToMany
106124
{
107-
return $this->belongsToMany(User::class, 'article_authors')->withPivot('role');
125+
/** @var BelongsToMany<User, Article, \Illuminate\Database\Eloquent\Relations\Pivot, 'pivot'> $relation */
126+
$relation = $this->belongsToMany(User::class, 'article_authors')->withPivot('role');
127+
128+
return $relation;
108129
}
109130
}

app/Models/ArticleAuthor.php

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,7 @@
1616
*
1717
* @mixin \Eloquent
1818
*
19-
* @use HasFactory<ArticleAuthor>
20-
*
21-
* @phpstan-use HasFactory<ArticleAuthor>
19+
* @phpstan-use \Illuminate\Database\Eloquent\Factories\HasFactory<self>
2220
*/
2321
final class ArticleAuthor extends Model
2422
{
@@ -41,18 +39,24 @@ protected function casts(): array
4139
}
4240

4341
/**
44-
* @return BelongsTo<Article,ArticleAuthor>
42+
* @return BelongsTo<Article, ArticleAuthor>
4543
*/
4644
public function article(): BelongsTo
4745
{
48-
return $this->belongsTo(Article::class);
46+
/** @var BelongsTo<Article, ArticleAuthor> $relation */
47+
$relation = $this->belongsTo(Article::class);
48+
49+
return $relation;
4950
}
5051

5152
/**
52-
* @return BelongsTo<User,ArticleAuthor>
53+
* @return BelongsTo<User, ArticleAuthor>
5354
*/
5455
public function user(): BelongsTo
5556
{
56-
return $this->belongsTo(User::class);
57+
/** @var BelongsTo<User, ArticleAuthor> $relation */
58+
$relation = $this->belongsTo(User::class);
59+
60+
return $relation;
5761
}
5862
}

app/Models/ArticleCategory.php

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,7 @@
1414
*
1515
* @mixin \Eloquent
1616
*
17-
* @use HasFactory<ArticleCategory>
18-
*
19-
* @phpstan-use HasFactory<ArticleCategory>
17+
* @phpstan-use \Illuminate\Database\Eloquent\Factories\HasFactory<self>
2018
*/
2119
final class ArticleCategory extends Model
2220
{
@@ -37,18 +35,24 @@ protected function casts(): array
3735
}
3836

3937
/**
40-
* @return BelongsTo<Article,ArticleCategory>
38+
* @return BelongsTo<Article, ArticleCategory>
4139
*/
4240
public function article(): BelongsTo
4341
{
44-
return $this->belongsTo(Article::class);
42+
/** @var BelongsTo<Article, ArticleCategory> $relation */
43+
$relation = $this->belongsTo(Article::class);
44+
45+
return $relation;
4546
}
4647

4748
/**
48-
* @return BelongsTo<Category,ArticleCategory>
49+
* @return BelongsTo<Category, ArticleCategory>
4950
*/
5051
public function category(): BelongsTo
5152
{
52-
return $this->belongsTo(Category::class);
53+
/** @var BelongsTo<Category, ArticleCategory> $relation */
54+
$relation = $this->belongsTo(Category::class);
55+
56+
return $relation;
5357
}
5458
}

app/Models/ArticleTag.php

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,7 @@
1414
*
1515
* @mixin \Eloquent
1616
*
17-
* @use HasFactory<ArticleTag>
18-
*
19-
* @phpstan-use HasFactory<ArticleTag>
17+
* @phpstan-use \Illuminate\Database\Eloquent\Factories\HasFactory<self>
2018
*/
2119
final class ArticleTag extends Model
2220
{
@@ -37,18 +35,24 @@ protected function casts(): array
3735
}
3836

3937
/**
40-
* @return BelongsTo<Article,ArticleTag>
38+
* @return BelongsTo<Article, ArticleTag>
4139
*/
4240
public function article(): BelongsTo
4341
{
44-
return $this->belongsTo(Article::class);
42+
/** @var BelongsTo<Article, ArticleTag> $relation */
43+
$relation = $this->belongsTo(Article::class);
44+
45+
return $relation;
4546
}
4647

4748
/**
48-
* @return BelongsTo<Tag,ArticleTag>
49+
* @return BelongsTo<Tag, ArticleTag>
4950
*/
5051
public function tag(): BelongsTo
5152
{
52-
return $this->belongsTo(Tag::class);
53+
/** @var BelongsTo<Tag, ArticleTag> $relation */
54+
$relation = $this->belongsTo(Tag::class);
55+
56+
return $relation;
5357
}
5458
}

app/Models/Category.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*
1616
* @mixin \Eloquent
1717
*
18-
* @phpstan-use HasFactory<Category>
18+
* @phpstan-use \Illuminate\Database\Eloquent\Factories\HasFactory<self>
1919
*/
2020
final class Category extends Model
2121
{
@@ -34,10 +34,13 @@ protected function casts(): array
3434
}
3535

3636
/**
37-
* @return BelongsToMany<Article,Category>
37+
* @return BelongsToMany<Article, Category, \Illuminate\Database\Eloquent\Relations\Pivot, 'pivot'>
3838
*/
3939
public function articles(): BelongsToMany
4040
{
41-
return $this->belongsToMany(Article::class, 'article_categories');
41+
/** @var BelongsToMany<Article, Category, \Illuminate\Database\Eloquent\Relations\Pivot, 'pivot'> $relation */
42+
$relation = $this->belongsToMany(Article::class, 'article_categories');
43+
44+
return $relation;
4245
}
4346
}

app/Models/Comment.php

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,7 @@
1919
*
2020
* @mixin \Eloquent
2121
*
22-
* @use HasFactory<Comment>
23-
*
24-
* @phpstan-use HasFactory<Comment>
22+
* @phpstan-use \Illuminate\Database\Eloquent\Factories\HasFactory<self>
2523
*/
2624
final class Comment extends Model
2725
{
@@ -40,28 +38,37 @@ protected function casts(): array
4038
}
4139

4240
/**
43-
* @return BelongsTo<Article,Comment>
41+
* @return BelongsTo<Article, Comment>
4442
*/
4543
public function article(): BelongsTo
4644
{
47-
return $this->belongsTo(Article::class);
45+
/** @var BelongsTo<Article, Comment> $relation */
46+
$relation = $this->belongsTo(Article::class);
47+
48+
return $relation;
4849
}
4950

5051
/**
51-
* @return BelongsTo<User,Comment>
52+
* @return BelongsTo<User, Comment>
5253
*/
5354
public function user(): BelongsTo
5455
{
55-
return $this->belongsTo(User::class);
56+
/** @var BelongsTo<User, Comment> $relation */
57+
$relation = $this->belongsTo(User::class);
58+
59+
return $relation;
5660
}
5761

5862
/**
5963
* Get the replies (child comments) for this comment.
6064
*
61-
* @return \Illuminate\Database\Eloquent\Relations\HasMany<\App\Models\Comment, Comment>
65+
* @return \Illuminate\Database\Eloquent\Relations\HasMany<Comment, Comment>
6266
*/
6367
public function replies(): \Illuminate\Database\Eloquent\Relations\HasMany
6468
{
65-
return $this->hasMany(Comment::class, 'parent_comment_id');
69+
/** @var \Illuminate\Database\Eloquent\Relations\HasMany<Comment, Comment> $relation */
70+
$relation = $this->hasMany(Comment::class, 'parent_comment_id');
71+
72+
return $relation;
6673
}
6774
}

0 commit comments

Comments
 (0)