1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-14 07:02:34 +01:00
invoiceninja/app/Utils/Traits/CompanyGatewayFeesAndLimitsSaver.php
David Bomba ba75a44eb8
Laravel 7.x Shift (#40)
* Adopt Laravel coding style

The Laravel framework adopts the PSR-2 coding style with some additions.
Laravel apps *should* adopt this coding style as well.

However, Shift allows you to customize the adopted coding style by
adding your own [PHP CS Fixer][1] `.php_cs` config to your project.

You may use [Shift's .php_cs][2] file as a base.

[1]: https://github.com/FriendsOfPHP/PHP-CS-Fixer
[2]: https://gist.github.com/laravel-shift/cab527923ed2a109dda047b97d53c200

* Shift bindings

PHP 5.5.9+ adds the new static `class` property which provides the fully qualified class name. This is preferred over using class name strings as these references are checked by the parser.

* Shift core files

* Shift to Throwable

* Add laravel/ui dependency

* Unindent vendor mail templates

* Shift config files

* Default config files

In an effort to make upgrading the constantly changing config files
easier, Shift defaulted them so you can review the commit diff for
changes. Moving forward, you should use ENV variables or create a
separate config file to allow the core config files to remain
automatically upgradeable.

* Shift Laravel dependencies

* Shift cleanup

* Upgrade to Laravel 7

Co-authored-by: Laravel Shift <shift@laravelshift.com>
2020-09-06 19:38:10 +10:00

119 lines
3.4 KiB
PHP

<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2020. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://opensource.org/licenses/AAL
*/
namespace App\Utils\Traits;
use App\DataMapper\BaseSettings;
use App\DataMapper\CompanySettings;
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) {
/* 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
*/
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':
json_decode($string);
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) {
// $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;
// }
//
public function cleanFeesAndLimits($fees_and_limits)
{
$new_arr = [];
foreach ($fees_and_limits as $key => $value) {
$fal = new FeesAndLimits;
// $fal->{$key} = $value;
foreach ($value as $k => $v) {
$fal->{$k} = $v;
$fal->{$k} = BaseSettings::castAttribute(FeesAndLimits::$casts[$k], $v);
}
$new_arr[$key] = (array) $fal;
}
return $new_arr;
}
}