Файловый менеджер - Редактировать - /home/clickysoft/public_html/jmapi5.clickysoft.net/Rules.tar
Назад
ValidateDate.php 0000644 00000001340 15021222274 0007577 0 ustar 00 <?php namespace App\Rules; use Carbon\Carbon; use Illuminate\Contracts\Validation\InvokableRule; class ValidateDate implements InvokableRule { public function __invoke($attribute, $value, $fail) { if ($attribute == 'date_scheduled') $message = 'The date scheduled can not be weekend.'; else if ($attribute == 'delivery_date') $message = 'The delivery date can not be weekend.'; else $message = 'The date pick up / ship by can not be weekend.'; try { $date = Carbon::make($value); if ($date->isWeekend()) $fail($message); } catch (\Exception $e){ // $fail('The :attribute is not a valid date.'); } } } SupplierProductNumberBelongsToProductPrice.php 0000644 00000002665 15021222274 0015741 0 ustar 00 <?php namespace App\Rules; use App\Models\ProductPrice; use Illuminate\Contracts\Validation\Rule; class SupplierProductNumberBelongsToProductPrice implements Rule { /** * Create a new rule instance. * * @return void */ public function __construct() { // } /** * Determine if the validation rule passes. * * @param string $attribute * @param mixed $value * @return bool */ public function passes($attribute, $value) { /*$product_prod_number = request()->route('product')->prices()?->where('supplier_prod_number', $value)->first(); $prod_number = ProductPrice::where('supplier_prod_number', $value)->first(); $flag = false; if ($product_prod_number && $prod_number){ $flag = $product_prod_number->id == $prod_number->id; } else if ($product_prod_number || !$prod_number){ $flag = true; } // return true if attribute belongs to the supplier_prod_number, false otherwise return $flag;*/ $prod_number = ProductPrice::where('supplier_prod_number', $value)->where('product_id', '<>', request()->route('product')?->id)->first(); return !$prod_number; } /** * Get the validation error message. * * @return string */ public function message() { return 'Invalid supplier production number for combination #:position.'; } } ValidateProductPrice.php 0000644 00000002162 15021222274 0011330 0 ustar 00 <?php namespace App\Rules; use App\Models\ProductPrice; use Illuminate\Contracts\Validation\DataAwareRule; use Illuminate\Contracts\Validation\InvokableRule; class ValidateProductPrice implements InvokableRule, DataAwareRule { /** * All of the data under validation. * * @var array */ protected $data = []; /** * Set the data under validation. * * @param array $data * @return $this */ public function setData($data) { $this->data = $data; return $this; } /** * Run the validation rule. * * @param string $attribute * @param mixed $value * @param \Closure(string): \Illuminate\Translation\PotentiallyTranslatedString $fail * @return void */ public function __invoke($attribute, $value, $fail) { $product_id = str_replace('price_id', 'product_id', $attribute); $price = ProductPrice::where(['id' => $value, 'product_id' => request($product_id)])->first(); if (!$price) { return $fail('Invalid price id selected for product #:position.'); } } } ValidatePriceRangeQuantity.php 0000644 00000002715 15021222274 0012507 0 ustar 00 <?php namespace App\Rules; use Illuminate\Contracts\Validation\DataAwareRule; use Illuminate\Contracts\Validation\InvokableRule; class ValidatePriceRangeQuantity implements InvokableRule, DataAwareRule { /** * All of the data under validation. * * @var array */ protected $data = []; /** * Set the data under validation. * * @param array $data * @return $this */ public function setData($data) { $this->data = $data; return $this; } /** * Run the validation rule. * * @param string $attribute * @param mixed $value * @param \Closure(string): \Illuminate\Translation\PotentiallyTranslatedString $fail * @return void */ public function __invoke($attribute, $value, $fail) { $from_arr = explode('.', $attribute); $ranges = $this->data[$from_arr[0]][$from_arr[1]][$from_arr[2]]; if ($from_arr[3] > 0){ $previous_key = $this->getPreviousKey($from_arr[3], $ranges); if ($value <= $ranges[$previous_key]['qty_to']){ return $fail('Quantity from for price range of combination #:position must be greater than previous quantity to.'); // -> "quantity is invalid" } } } private function getPreviousKey($current_key, $ranges = []): int|string { $keys = array_keys($ranges); return $keys[array_search($current_key, $keys) -1]; } } ValidateCoupon.php 0000644 00000001545 15021222274 0010174 0 ustar 00 <?php namespace App\Rules; use App\Models\Coupon; use Illuminate\Contracts\Validation\InvokableRule; class ValidateCoupon implements InvokableRule { public function __invoke($attribute, $value, $fail) { $coupon = Coupon::where('code', $value)->where('status', 1)->first(); $message = ""; if ($coupon) { if ($coupon->expiry_date) { $day_end = $coupon->expiry_date->endOfDay(); if (!now()->lte($day_end)) { $message = "Coupon has expired."; } } if ($coupon->number_of_usage > 0 && $coupon->redemption_count >= $coupon->number_of_usage) { $message = "Coupon usage limit reached."; } } else { $message = "Invalid coupon code"; } if ($message) $fail($message); } } ValidateRange.php 0000644 00000003001 15021222274 0007752 0 ustar 00 <?php namespace App\Rules; use App\Models\ProductPrice; use App\Models\ProductVariationRange; use Illuminate\Contracts\Validation\DataAwareRule; use Illuminate\Contracts\Validation\InvokableRule; class ValidateRange implements DataAwareRule, InvokableRule { /** * All the data under validation. * * @var array */ protected $data = []; /** * Set the data under validation. * * @param array $data * @return $this */ public function setData($data) { $this->data = $data; return $this; } /** * Run the validation rule. * * @param string $attribute * @param mixed $value * @param \Closure(string): \Illuminate\Translation\PotentiallyTranslatedString $fail * @return void */ public function __invoke($attribute, $value, $fail) { $product_id = str_replace('quantity', 'product_id', $attribute); $price_id = str_replace('quantity', 'price_id', $attribute); $price = ProductPrice::where(['id' => request($price_id), 'product_id' => request($product_id)])->first(); if (!$price) return $fail('Invalid quantity for product #:position.'); $price_range = ProductVariationRange::where('product_price_id', $price->id) ->where('qty_from', '<=', $value) ->where('qty_to', '>=', $value)->first(); if (!$price_range) return $fail('Invalid quantity for product #:position. We don\'t have enough quantity to fulfill the request.'); } } LimitProductQuantityIncrease.php 0000644 00000002606 15021222274 0013106 0 ustar 00 <?php namespace App\Rules; use App\Models\OrderItems; use App\Models\ProductVariationRange; use Illuminate\Contracts\Validation\InvokableRule; use Illuminate\Support\Facades\Log; use Psr\Container\ContainerExceptionInterface; use Psr\Container\NotFoundExceptionInterface; class LimitProductQuantityIncrease implements InvokableRule { /** * @throws ContainerExceptionInterface * @throws NotFoundExceptionInterface */ public function __invoke($attribute, $value, $fail) { if ($value) { $index = explode('.', $attribute)[1]; $item = OrderItems::find($value); $price_range = ProductVariationRange::where('product_price_id', $item->price_id) ->where('qty_from', '<=', $item->quantity) ->where('qty_to', '>=', $item->quantity) ->first(); if (!$price_range) { $fail("Product combination for product #:position not found."); } else { if ($price_range->price != $item->price) { if (isset(request()->get('products')[$index]['quantity']) && request()->get('products')[$index]['quantity'] > $item->quantity) { $fail("Product quantity can not be increased due to change in price. Please add the item as a new product to proceed."); } } } } } } ValidateImage.php 0000644 00000001617 15021222274 0007753 0 ustar 00 <?php namespace App\Rules; use Illuminate\Contracts\Validation\InvokableRule; class ValidateImage implements InvokableRule { /** * Run the validation rule. * * @param string $attribute * @param mixed $value * @param \Closure(string): \Illuminate\Translation\PotentiallyTranslatedString $fail * @return void */ public function __invoke($attribute, $value, $fail) { $allowed_formats = ['jpg', 'jpeg', 'png']; if (is_file($value)){ $ext = $value->getClientOriginalExtension(); if (!in_array($ext, $allowed_formats)) $fail('Image for product #:position should be a valid image/base64 of type jpg, jpeg, png'); }else if (!preg_match('/^data:image\/(jpeg|png|jpg);base64,/', $value)) $fail('Image for product #:position should be a valid image/base64 of type jpg, jpeg, png'); } } AddressBelongsToUser.php 0000644 00000001147 15021222274 0011316 0 ustar 00 <?php namespace App\Rules; use App\Models\AddressBook; use Illuminate\Contracts\Validation\DataAwareRule; use Illuminate\Contracts\Validation\InvokableRule; class AddressBelongsToUser implements DataAwareRule, InvokableRule { protected $data = []; public function setData($data) { $this->data = $data; } public function __invoke($attribute, $value, $fail) { $address_book = AddressBook::where('id', $value) ->where('user_id', $this->data['user_id']) ->exists(); if (!$address_book) $fail('The selected :attribute is invalid.'); } }
| ver. 1.4 |
Github
|
.
| PHP 8.1.29 | Генерация страницы: 0 |
proxy
|
phpinfo
|
Настройка