This commit is contained in:
2026-02-03 15:31:29 +05:00
commit 326c677e8d
2800 changed files with 1489388 additions and 0 deletions

View File

@@ -0,0 +1,61 @@
<?php
namespace App\Rules;
use Closure;
use Illuminate\Contracts\Validation\ValidationRule;
use Illuminate\Validation\Rule;
class CommaSeparatedFields implements ValidationRule
{
public function __construct(
protected mixed $model = null,
protected bool $checkIfFieldExists = false
) {}
/**
* Run the validation rule.
*
* @param \Closure(string): \Illuminate\Translation\PotentiallyTranslatedString $fail
*/
public function validate(string $attribute, mixed $value, Closure $fail): void
{
$values = explode(',', $value);
foreach ($values as $value) {
if (empty($value) || is_numeric($value)) {
} else {
if (validator(
data: ['key' => $value],
rules: [
'key' => ['required', 'string', 'alpha_dash:ascii'],
]
)->fails()
) {
info([
'failed' => $value,
]);
} else {
info([
'passed' => $value,
]);
}
}
}
// $fields = (new $this->model)->getFillable();
// if ($this->checkIfFieldExists) {
// foreach ($values as $value) {
// if (! empty($value) && ! in_array($value, $fields)) {
// $fail(sprintf(
// "Field '%s' does not exit in table, available fields: %s",
// $value,
// implode(', ', $fields),
// ));
// }
// }
// }
}
}

View File

@@ -0,0 +1,25 @@
<?php
namespace App\Rules;
use Closure;
use Illuminate\Contracts\Validation\ValidationRule;
class CommaSeparatedIntegers implements ValidationRule
{
/**
* Run the validation rule.
*
* @param \Closure(string): \Illuminate\Translation\PotentiallyTranslatedString $fail
*/
public function validate(string $attribute, mixed $value, Closure $fail): void
{
// Remove any leading/trailing whitespace
$input = trim($value);
// Check if the input matches the format "int,int,int..." or "int"
if (! preg_match('/^(\d+,)*\d+$/', $input)) {
$fail(__('Integers should be comma seperated'));
}
}
}

View File

@@ -0,0 +1,44 @@
<?php
namespace App\Rules;
use Closure;
use Illuminate\Contracts\Validation\ValidationRule;
use Illuminate\Support\Facades\DB;
class ProductStockIsAvailable implements ValidationRule
{
/**
* Check given request of items is available in stock
*/
public function __construct(
public int $product_quantity
) {}
/**
* Run the validation rule.
*
* @param \Closure(string): \Illuminate\Translation\PotentiallyTranslatedString $fail
*/
public function validate(string $attribute, mixed $value, Closure $fail): void
{
if (is_null($value) || ! is_int($value)) {
return;
}
$product = DB::table('products')->where('id', $value)->select(['id', 'stock', 'security_stock'])->first();
if (is_null($product)) {
$fail(__('product_id is invalid'));
return;
}
// Get the sellable stock
$sellable_stock = ($product->security_stock) ? $product->stock - $product->security_stock : $product->stock;
if ($this->product_quantity > $sellable_stock) {
$fail(sprintf(':attribute (%s) too much requested, product stock is: %s', $this->product_quantity, $product->stock));
}
}
}

View File

@@ -0,0 +1,55 @@
<?php
namespace App\Rules;
use Closure;
use Illuminate\Contracts\Validation\ValidationRule;
class SortableColumnRule implements ValidationRule
{
/**
* Allowed columns for sorting
*
* @var array<string>
*/
protected array $allowedColumns;
/**
* Sortable column rule
*/
public function __construct(array $allowedColumns)
{
$this->allowedColumns = $allowedColumns;
}
/**
* Run the validation rule.
*
* @param \Closure(string): \Illuminate\Translation\PotentiallyTranslatedString $fail
*/
public function validate(string $attribute, mixed $value, Closure $fail): void
{
// Format should be "coloumn-sort", example: 'created_at-descending'
$request_data = explode('-', $value);
if (count($request_data) !== 2) {
$fail(__('Format should be: "coloumn-sort"'));
return;
}
[$column, $sort] = $request_data;
if (! in_array($column, $this->allowedColumns)) {
$fail(__("Sorting by this {$column} is not allowed"));
return;
}
if (! in_array($sort, ['descending', 'ascending'])) {
$fail(__('You can only sort by ascending or descending!'));
return;
}
}
}

View File

@@ -0,0 +1,36 @@
<?php
namespace App\Rules;
use App\Models\Auth\Verification;
use Closure;
use Illuminate\Contracts\Validation\ValidationRule;
class VerificationRule implements ValidationRule
{
/**
* Constructor
*
* @param string $phone
*/
public function __construct(protected null|int|string $phone)
{
//
}
/**
* Run the validation rule.
*
* @param \Closure(string): \Illuminate\Translation\PotentiallyTranslatedString $fail
*/
public function validate(string $attribute, mixed $value, Closure $fail): void
{
if (! $this->phone) {
$fail(__('No phone provided'));
}
Verification::where('username', $this->phone)
->where('code', $value)
->existsOr(fn () => $fail(__('Verification code is incorrect')));
}
}