1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-09-21 00:41:34 +02:00
invoiceninja/app/DataMapper/BaseSettings.php

61 lines
1.4 KiB
PHP
Raw Normal View History

<?php
2019-05-11 05:32:07 +02:00
/**
* Invoice Ninja (https://invoiceninja.com).
2019-05-11 05:32:07 +02:00
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
2023-01-28 23:21:40 +01:00
* @copyright Copyright (c) 2023. Invoice Ninja LLC (https://invoiceninja.com)
2019-05-11 05:32:07 +02:00
*
2021-06-16 08:58:16 +02:00
* @license https://www.elastic.co/licensing/elastic-license
2019-05-11 05:32:07 +02:00
*/
namespace App\DataMapper;
/**
2023-01-17 01:00:12 +01:00
* BaseSettings.
*/
class BaseSettings
{
2023-07-25 12:04:04 +02:00
// //@deprecated
// public function __construct($obj)
// {
// // foreach ($obj as $key => $value) {
// // $obj->{$key} = $value;
// // }
// }
public static function setCasts($obj, $casts)
{
foreach ($casts as $key => $value) {
$obj->{$key} = self::castAttribute($key, $obj->{$key});
}
return $obj;
}
2019-09-19 07:50:05 +02:00
public static function castAttribute($key, $value)
{
switch ($key) {
case 'int':
case 'integer':
return (int) $value;
case 'real':
case 'float':
case 'double':
return (float) $value;
case 'string':
return is_null($value) ? '' : (string) $value;
case 'bool':
case 'boolean':
2022-02-27 07:49:49 +01:00
return boolval($value);
case 'object':
return json_decode($value);
case 'array':
case 'json':
return json_decode($value, true);
default:
return $value;
}
}
}