1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-15 07:33:04 +01:00
invoiceninja/app/Utils/Traits/CompanyGatewayFeesAndLimitsSaver.php

104 lines
3.2 KiB
PHP
Raw Normal View History

<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
2024-04-12 06:15:41 +02:00
* @copyright Copyright (c) 2024. Invoice Ninja LLC (https://invoiceninja.com)
*
2021-06-16 08:58:16 +02:00
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\Utils\Traits;
2020-02-26 09:04:20 +01:00
use App\DataMapper\BaseSettings;
use App\DataMapper\FeesAndLimits;
/**
* Class CompanyGatewayFeesAndLimitsSaver.
*/
trait CompanyGatewayFeesAndLimitsSaver
{
public function validateFeesAndLimits($fees_and_limits)
{
$fees_and_limits = (object) $fees_and_limits;
$casts = FeesAndLimits::$casts;
foreach ($fees_and_limits as $fee_and_limit) {
$fee_and_limit = (object) $fee_and_limit;
foreach ($casts as $key => $value) {
if ($value == 'float' && property_exists($fee_and_limit, $key)) {
2022-03-28 03:46:38 +02:00
$fee_and_limit->{$key} = floatval($fee_and_limit->{$key});
}
/* Handles unset settings or blank strings */
if (! property_exists($fee_and_limit, $key) || is_null($fee_and_limit->{$key}) || ! isset($fee_and_limit->{$key}) || $fee_and_limit->{$key} == '') {
continue;
}
/*Catch all filter */
if (! $this->checkAttribute($value, $fee_and_limit->{$key})) {
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
*/
2024-01-14 05:05:00 +01:00
private function checkAttribute($key, $value): bool
{
switch ($key) {
case 'int':
case 'integer':
return ctype_digit(strval($value));
case 'real':
case 'float':
case 'double':
2023-02-16 02:36:09 +01:00
return ! is_string($value) && (is_float($value) || is_numeric(strval($value)));
2023-10-26 04:57:44 +02:00
// return is_float($value) || is_numeric(strval($value));
case 'string':
return (is_string($value) && 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':
json_decode($value);
2023-02-16 02:36:09 +01:00
return json_last_error() == JSON_ERROR_NONE;
default:
return false;
}
}
public function cleanFeesAndLimits($fees_and_limits)
{
$new_arr = [];
foreach ($fees_and_limits as $key => $value) {
2024-01-14 05:05:00 +01:00
$fal = new FeesAndLimits();
2020-07-07 14:33:11 +02:00
// $fal->{$key} = $value;
2020-07-07 14:33:11 +02:00
foreach ($value as $k => $v) {
$fal->{$k} = $v;
$fal->{$k} = BaseSettings::castAttribute(FeesAndLimits::$casts[$k], $v);
}
$new_arr[$key] = (array) $fal;
}
return $new_arr;
}
}