File "ValidatePriceRangeQuantity.php"
Full Path: /home/clickysoft/public_html/jmapi5.clickysoft.net/app/Rules/ValidatePriceRangeQuantity.php
File size: 1.45 KB
MIME-type: text/x-php
Charset: utf-8
<?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];
}
}