Repository navigation
Expand file tree
/
Copy pathCreateScheduledCommitRequest.php
More file actions
134 lines (113 loc) · 3.63 KB
/
Copy pathCreateScheduledCommitRequest.php
File metadata and controls
134 lines (113 loc) · 3.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
<?php
declare(strict_types=1);
namespace App\Http\Requests;
use App\Dto\NewPoll;
use App\Enums\PollDuration;
use App\Models\Guild;
use App\Models\Organization;
use App\Models\User;
use App\Rules\OrganizationToPostAs;
use App\Rules\ScheduledCommitLimit;
use App\Services\Urls;
use Illuminate\Contracts\Validation\ValidationRule;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Http\UploadedFile;
use Illuminate\Validation\Rule;
use Illuminate\Validation\Rules\Dimensions;
use Illuminate\Validation\Rules\Enum;
use Illuminate\Validation\Rules\File;
use Illuminate\Validation\Rules\In;
final class CreateScheduledCommitRequest extends FormRequest
{
public function authorize(): bool
{
return $this->user() !== null;
}
/** @return array<string, array<int, string|ValidationRule|File|Dimensions|Enum|In>> */
public function rules(): array
{
$user = $this->user();
assert($user instanceof User);
$body = $this->input('body');
return [
'body' => CreateCommitRequest::bodyRules($this->hasFile('images')),
'author' => ['nullable', 'string', new OrganizationToPostAs($user)],
'scheduled_at' => ['required', 'date_format:Y-m-d\TH:i:sP', 'after:now', 'before_or_equal:'.now()->addDays(30)->toIso8601String(), new ScheduledCommitLimit($user)],
'parent_id' => ['prohibited'],
'link_preview_url' => ['nullable', 'string', Rule::in(is_string($body) ? resolve(Urls::class)->all($body) : [])],
...CreateCommitRequest::guildRules($this),
...CreateCommitRequest::attachmentRules(),
];
}
/**
* @return array<string, string>
*/
public function attributes(): array
{
return [
'image_descriptions.*' => 'image description',
];
}
/**
* @return array<string, string>
*/
public function messages(): array
{
return [
'link_preview_url.in' => 'The link card must be one of the links in the body.',
];
}
/**
* The first link in the body gets the link preview, unless the author chose another link or none at all.
*/
public function linkPreview(): bool|string
{
if (! $this->has('link_preview_url')) {
return true;
}
return $this->filled('link_preview_url') ? $this->string('link_preview_url')->value() : false;
}
/**
* The organization the commit goes out as, or null when it goes out as the user.
*/
public function organization(): ?Organization
{
$user = $this->user();
assert($user instanceof User);
return OrganizationToPostAs::of($user, $this->input('author'));
}
public function poll(): ?NewPoll
{
if (! $this->filled('poll')) {
return null;
}
/** @var list<string> $options */
$options = $this->array('poll.options');
return new NewPoll($options, PollDuration::from($this->integer('poll.duration')));
}
/**
* @return list<UploadedFile>
*/
public function images(): array
{
$images = $this->file('images');
return is_array($images) ? array_values($images) : [];
}
/**
* @return list<string|null>
*/
public function imageDescriptions(): array
{
/** @var list<string|null> $descriptions */
$descriptions = $this->array('image_descriptions');
return $descriptions;
}
public function prepareForValidation(): void
{
CreateCommitRequest::normalizeBody($this);
}
public function guild(): ?Guild
{
return CreateCommitRequest::guildOf($this);
}
}