akaunting/app/Providers/Validation.php

79 lines
1.9 KiB
PHP
Raw Normal View History

2018-07-01 21:06:52 +03:00
<?php
namespace App\Providers;
use App\Models\Setting\Currency;
2019-11-16 10:21:14 +03:00
use Illuminate\Support\ServiceProvider as Provider;
2018-07-01 21:06:52 +03:00
use Validator;
2019-11-16 10:21:14 +03:00
class Validation extends Provider
2018-07-01 21:06:52 +03:00
{
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
$currency_code = null;
Validator::extend('currency', function ($attribute, $value, $parameters, $validator) use(&$currency_code) {
$status = false;
2019-11-16 10:21:14 +03:00
2018-09-06 01:45:33 +03:00
if (!is_string($value) || (strlen($value) != 3)) {
return $status;
}
2018-07-01 21:06:52 +03:00
2018-09-06 01:45:33 +03:00
$currencies = Currency::enabled()->pluck('code')->toArray();
2018-07-01 21:06:52 +03:00
2018-09-06 01:45:33 +03:00
if (in_array($value, $currencies)) {
$status = true;
}
2018-07-01 21:06:52 +03:00
$currency_code = $value;
2018-07-01 21:06:52 +03:00
return $status;
},
2018-07-01 21:06:52 +03:00
trans('validation.custom.invalid_currency', ['attribute' => $currency_code])
);
$amount = null;
Validator::extend('amount', function ($attribute, $value, $parameters, $validator) use (&$amount) {
$status = false;
if ($value > 0) {
$status = true;
}
2021-05-14 18:29:24 +03:00
if (!preg_match("/^(?=.*?[0-9])[0-9.,]+$/", $value)) {
$status = false;
}
$amount = $value;
return $status;
},
trans('validation.custom.invalid_amount', ['attribute' => $amount])
);
2020-02-28 11:02:25 +03:00
Validator::extend('extension', function ($attribute, $value, $parameters, $validator) {
$extension = $value->getClientOriginalExtension();
return !empty($extension) && in_array($extension, $parameters);
},
trans('validation.custom.invalid_extension')
);
2018-07-01 21:06:52 +03:00
}
/**
* Register any application services.
*
* @return void
*/
public function register()
{
//
}
}