Create New Item
Item Type
File
Folder
Item Name
Search file in folder and subfolders...
Are you sure want to rename?
peripherad
/
app
/
Rules
:
ValidatePriceRangeQuantity.php
Advanced Search
Upload
New Item
Settings
Back
Back Up
Advanced Editor
Save
<?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]; } }