first commit
This commit is contained in:
48
app/Rules/BetweenDate.php
Normal file
48
app/Rules/BetweenDate.php
Normal file
@@ -0,0 +1,48 @@
|
||||
<?php
|
||||
|
||||
namespace App\Rules;
|
||||
|
||||
use Illuminate\Contracts\Validation\Rule;
|
||||
|
||||
class BetweenDate implements Rule
|
||||
{
|
||||
|
||||
protected string $afterDate;
|
||||
|
||||
protected string $beforeDate;
|
||||
|
||||
/**
|
||||
* Create a new rule instance.
|
||||
* @param string $afterDate
|
||||
* @param string $beforeDate
|
||||
*/
|
||||
public function __construct(string $afterDate, string $beforeDate)
|
||||
{
|
||||
$this->afterDate = $afterDate;
|
||||
$this->beforeDate = $beforeDate;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if the validation rule passes.
|
||||
*
|
||||
* @param string $attribute
|
||||
* @param mixed $value
|
||||
* @return bool
|
||||
*/
|
||||
public function passes($attribute, $value): bool
|
||||
{
|
||||
$endTimestamp = strtotime($value);
|
||||
$afterTimestamp = strtotime($this->afterDate);
|
||||
$beforeTimestamp = strtotime($this->beforeDate);
|
||||
|
||||
return $endTimestamp > $afterTimestamp && $endTimestamp <= $beforeTimestamp;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the validation error message.
|
||||
*/
|
||||
public function message(): string
|
||||
{
|
||||
return 'The date is not between acceptable boundaries';
|
||||
}
|
||||
}
|
||||
47
app/Rules/DoesNotExist.php
Normal file
47
app/Rules/DoesNotExist.php
Normal file
@@ -0,0 +1,47 @@
|
||||
<?php
|
||||
|
||||
namespace App\Rules;
|
||||
|
||||
use Illuminate\Contracts\Validation\Rule;
|
||||
use DB;
|
||||
|
||||
class DoesNotExist implements Rule
|
||||
{
|
||||
|
||||
protected string $table;
|
||||
|
||||
protected int $id;
|
||||
|
||||
/**
|
||||
* Create a new rule instance.
|
||||
* @param string $table
|
||||
* @param int $id
|
||||
*/
|
||||
public function __construct(string $table, int $id)
|
||||
{
|
||||
$this->table = $table;
|
||||
$this->id = $id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if the validation rule passes.
|
||||
*
|
||||
* @param string $attribute
|
||||
* @param mixed $value
|
||||
* @return bool
|
||||
*/
|
||||
public function passes($attribute, $value): bool
|
||||
{
|
||||
return DB::table($this->table)
|
||||
->where($this->id, $value)
|
||||
->doesntExist();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the validation error message.
|
||||
*/
|
||||
public function message(): string
|
||||
{
|
||||
return 'Item already exists';
|
||||
}
|
||||
}
|
||||
70
app/Rules/TimeIntervalDoesNotExist.php
Normal file
70
app/Rules/TimeIntervalDoesNotExist.php
Normal file
@@ -0,0 +1,70 @@
|
||||
<?php
|
||||
|
||||
namespace App\Rules;
|
||||
|
||||
use App\Models\User;
|
||||
use App\Models\TimeInterval;
|
||||
use Carbon\Carbon;
|
||||
use Closure;
|
||||
use Illuminate\Contracts\Validation\ValidationRule;
|
||||
|
||||
class TimeIntervalDoesNotExist implements ValidationRule
|
||||
{
|
||||
/**
|
||||
* @var User
|
||||
*/
|
||||
private User $user;
|
||||
|
||||
/**
|
||||
* @var Carbon
|
||||
*/
|
||||
private Carbon $startAt;
|
||||
|
||||
/**
|
||||
* @var Carbon
|
||||
*/
|
||||
private Carbon $endAt;
|
||||
|
||||
/**
|
||||
* Create a new rule instance.
|
||||
*
|
||||
* TimeInterval constructor.
|
||||
* @param User|null $user
|
||||
* @param Carbon $startAt
|
||||
* @param Carbon $endAt
|
||||
*/
|
||||
public function __construct(?User $user, Carbon $startAt, Carbon $endAt)
|
||||
{
|
||||
$this->user = $user;
|
||||
$this->startAt = $startAt;
|
||||
$this->endAt = $endAt;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if the validation rule passes.
|
||||
*
|
||||
* @param string $attribute
|
||||
* @param mixed $value
|
||||
* @param Closure $fail
|
||||
* @return void
|
||||
*/
|
||||
public function validate(string $attribute, mixed $value, Closure $fail): void
|
||||
{
|
||||
$alreadyExists = TimeInterval::where('user_id', optional($this->user)->id)
|
||||
->where(function ($query) {
|
||||
$query
|
||||
->whereBetween('start_at', [$this->startAt, $this->endAt])
|
||||
->orWhereBetween('end_at', [$this->startAt, $this->endAt])
|
||||
->orWhere(function ($query) {
|
||||
$query
|
||||
->where('start_at', '<', $this->startAt)
|
||||
->where('end_at', '>', $this->endAt);
|
||||
});
|
||||
})
|
||||
->exists();
|
||||
|
||||
if ($alreadyExists) {
|
||||
$fail('validation.time_interval_already_exist')->translate();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user