2019-11-11 13:21:19 +01:00
|
|
|
<?php
|
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
2019-11-11 13:21:19 +01:00
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
2020-01-07 01:13:47 +01:00
|
|
|
* @copyright Copyright (c) 2020. Invoice Ninja LLC (https://invoiceninja.com)
|
2019-11-11 13:21:19 +01:00
|
|
|
*
|
|
|
|
* @license https://opensource.org/licenses/AAL
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\Utils\Traits;
|
|
|
|
|
2020-02-26 09:04:20 +01:00
|
|
|
use App\DataMapper\BaseSettings;
|
2019-11-11 13:21:19 +01:00
|
|
|
use App\DataMapper\FeesAndLimits;
|
|
|
|
|
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Class CompanyGatewayFeesAndLimitsSaver.
|
2019-11-11 13:21:19 +01:00
|
|
|
*/
|
|
|
|
trait CompanyGatewayFeesAndLimitsSaver
|
|
|
|
{
|
2019-12-30 22:59:12 +01:00
|
|
|
public function validateFeesAndLimits($fees_and_limits)
|
|
|
|
{
|
2020-09-06 11:38:10 +02:00
|
|
|
$fees_and_limits = (object) $fees_and_limits;
|
2019-12-30 22:59:12 +01:00
|
|
|
$casts = FeesAndLimits::$casts;
|
|
|
|
|
|
|
|
foreach ($fees_and_limits as $fee_and_limit) {
|
2020-09-06 11:38:10 +02:00
|
|
|
$fee_and_limit = (object) $fee_and_limit;
|
2019-12-30 22:59:12 +01:00
|
|
|
|
|
|
|
foreach ($casts as $key => $value) {
|
|
|
|
|
|
|
|
/* Handles unset settings or blank strings */
|
2020-09-06 11:38:10 +02:00
|
|
|
if (! property_exists($fee_and_limit, $key) || is_null($fee_and_limit->{$key}) || ! isset($fee_and_limit->{$key}) || $fee_and_limit->{$key} == '') {
|
2019-12-30 22:59:12 +01:00
|
|
|
continue;
|
|
|
|
}
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
/*Catch all filter */
|
2020-09-06 11:38:10 +02:00
|
|
|
if (! $this->checkAttribute($value, $fee_and_limit->{$key})) {
|
2019-12-30 22:59:12 +01:00
|
|
|
return [$key, $value];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Type checks a object property.
|
|
|
|
* @param string $key The type
|
|
|
|
* @param string $value The object property
|
|
|
|
* @return bool TRUE if the property is the expected type
|
|
|
|
*/
|
|
|
|
private function checkAttribute($key, $value) :bool
|
|
|
|
{
|
|
|
|
switch ($key) {
|
|
|
|
case 'int':
|
|
|
|
case 'integer':
|
|
|
|
return ctype_digit(strval($value));
|
|
|
|
case 'real':
|
|
|
|
case 'float':
|
|
|
|
case 'double':
|
|
|
|
return is_float($value) || is_numeric(strval($value));
|
|
|
|
case 'string':
|
|
|
|
return method_exists($value, '__toString') || is_null($value) || is_string($value);
|
|
|
|
case 'bool':
|
|
|
|
case 'boolean':
|
|
|
|
return is_bool($value) || (int) filter_var($value, FILTER_VALIDATE_BOOLEAN);
|
|
|
|
case 'object':
|
|
|
|
return is_object($value);
|
|
|
|
case 'array':
|
|
|
|
return is_array($value);
|
|
|
|
case 'json':
|
2020-11-01 06:09:09 +01:00
|
|
|
json_decode($value);
|
2020-09-06 11:38:10 +02:00
|
|
|
|
|
|
|
return json_last_error() == JSON_ERROR_NONE;
|
2019-12-30 22:59:12 +01:00
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-19 12:29:58 +02:00
|
|
|
// public function cleanFeesAndLimits($fees_and_limits)
|
|
|
|
// {
|
|
|
|
// $new_arr = [];
|
|
|
|
|
|
|
|
// foreach ($fees_and_limits as $key => $value) {
|
|
|
|
// $fal = new FeesAndLimits;
|
|
|
|
|
|
|
|
// foreach ($value as $k => $v) {
|
|
|
|
// $fal->{$k} = $v;
|
|
|
|
// $fal->{$k} = BaseSettings::castAttribute(FeesAndLimits::$casts[$k], $v);
|
|
|
|
// }
|
|
|
|
|
|
|
|
// $new_arr[$key] = (array)$fal;
|
|
|
|
// }
|
|
|
|
|
|
|
|
// return $new_arr;
|
|
|
|
// }
|
2020-09-06 11:38:10 +02:00
|
|
|
//
|
2019-12-30 22:59:12 +01:00
|
|
|
public function cleanFeesAndLimits($fees_and_limits)
|
|
|
|
{
|
|
|
|
$new_arr = [];
|
|
|
|
|
|
|
|
foreach ($fees_and_limits as $key => $value) {
|
2020-09-06 11:38:10 +02:00
|
|
|
$fal = new FeesAndLimits;
|
2020-07-07 14:33:11 +02:00
|
|
|
// $fal->{$key} = $value;
|
2019-12-30 22:59:12 +01:00
|
|
|
|
2020-07-07 14:33:11 +02:00
|
|
|
foreach ($value as $k => $v) {
|
|
|
|
$fal->{$k} = $v;
|
|
|
|
$fal->{$k} = BaseSettings::castAttribute(FeesAndLimits::$casts[$k], $v);
|
|
|
|
}
|
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
$new_arr[$key] = (array) $fal;
|
2019-12-30 22:59:12 +01:00
|
|
|
}
|
|
|
|
|
2020-04-02 02:17:30 +02:00
|
|
|
return $new_arr;
|
2020-04-01 10:54:22 +02:00
|
|
|
}
|
2019-12-30 22:59:12 +01:00
|
|
|
}
|