Personal_Website/app/Http/Requests/BlogArticleRequest.php

78 lines
1.9 KiB
PHP

<?php
namespace App\Http\Requests;
use App\Http\Services\AuthService;
use Carbon\Carbon;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Str;
class BlogArticleRequest extends FormRequest
{
private $authService;
public function __construct(AuthService $authService, array $query = [], array $request = [], array $attributes = [], array $cookies = [], array $files = [], array $server = [], $content = null)
{
parent::__construct($query, $request, $attributes, $cookies, $files, $server, $content);
$this->authService = $authService;
}
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return $this->authService->isAuthenticated();
}
/**
* Run before validating data
*/
protected function prepareForValidation()
{
if (!$this->exists('slug')) {
$this->merge([
'slug' => Str::slug($this->get('title')),
]);
}
if (!$this->exists('date')) {
$this->merge([
'date' => Carbon::now()->toDateTime(),
]);
}
if (!$this->exists('summary')) {
$content = $this->get('content');
$pos = -1;
if (strlen($content) > 200) {
$pos = strpos($content, ' ', 200);
}
$summary = substr($content, 0, $pos);
$this->merge([
'summary' => $summary,
]);
}
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'title' => 'required|string|max:255',
'slug' => 'required|string|max:255',
'date' => 'required|date',
'summary' => 'required|string|max:1023',
'content' => 'required|string',
];
}
}