2019-10-09 01:53:33 +02:00
|
|
|
<?php
|
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
2019-10-09 01:53:33 +02:00
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
2022-04-27 05:20:41 +02:00
|
|
|
* @copyright Copyright (c) 2022. Invoice Ninja LLC (https://invoiceninja.com)
|
2019-10-09 01:53:33 +02:00
|
|
|
*
|
2021-06-16 08:58:16 +02:00
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
2019-10-09 01:53:33 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\Utils\Traits;
|
|
|
|
|
|
|
|
use App\DataMapper\CompanySettings;
|
|
|
|
|
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Class SettingsSaver.
|
2019-10-09 01:53:33 +02:00
|
|
|
*/
|
2019-10-10 04:24:19 +02:00
|
|
|
trait SettingsSaver
|
2019-10-09 01:53:33 +02:00
|
|
|
{
|
2019-12-30 22:59:12 +01:00
|
|
|
/**
|
|
|
|
* Used for custom validation of inbound
|
|
|
|
* settings request.
|
|
|
|
*
|
|
|
|
* Returns an array of errors, or boolean TRUE
|
|
|
|
* on successful validation
|
|
|
|
* @param array $settings The request() settings array
|
|
|
|
* @return array|bool Array on failure, boolean TRUE on success
|
|
|
|
*/
|
|
|
|
public function validateSettings($settings)
|
|
|
|
{
|
2020-09-06 11:38:10 +02:00
|
|
|
$settings = (object) $settings;
|
2019-12-30 22:59:12 +01:00
|
|
|
$casts = CompanySettings::$casts;
|
|
|
|
|
|
|
|
ksort($casts);
|
|
|
|
|
|
|
|
foreach ($casts as $key => $value) {
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2022-03-28 03:46:38 +02:00
|
|
|
//try casting floats here
|
2022-06-21 11:57:17 +02:00
|
|
|
if ($value == 'float' && property_exists($settings, $key)) {
|
2022-03-28 03:46:38 +02:00
|
|
|
$settings->{$key} = floatval($settings->{$key});
|
|
|
|
}
|
|
|
|
|
2020-04-21 07:16:45 +02:00
|
|
|
if (in_array($key, CompanySettings::$string_casts)) {
|
2020-09-06 11:38:10 +02:00
|
|
|
$value = 'string';
|
|
|
|
if (! property_exists($settings, $key)) {
|
2020-03-06 12:10:59 +01:00
|
|
|
continue;
|
2020-09-06 11:38:10 +02:00
|
|
|
} elseif (! $this->checkAttribute($value, $settings->{$key})) {
|
2020-03-06 12:10:59 +01:00
|
|
|
return [$key, $value, $settings->{$key}];
|
|
|
|
}
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
2019-12-30 22:59:12 +01:00
|
|
|
/*Separate loop if it is a _id field which is an integer cast as a string*/
|
2022-06-21 11:57:17 +02:00
|
|
|
elseif (substr($key, -3) == '_id' || substr($key, -14) == 'number_counter' || ($key == 'payment_terms' && strlen($settings->{$key}) >= 1) || ($key == 'valid_until' && strlen($settings->{$key}) >= 1)) {
|
2020-09-06 11:38:10 +02:00
|
|
|
$value = 'integer';
|
2021-02-22 11:35:00 +01:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
if ($key == 'gmail_sending_user_id') {
|
2021-02-22 11:35:00 +01:00
|
|
|
$value = 'string';
|
2022-06-21 11:57:17 +02:00
|
|
|
}
|
2021-02-22 11:35:00 +01:00
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
if (! property_exists($settings, $key)) {
|
2019-12-30 22:59:12 +01:00
|
|
|
continue;
|
2020-09-06 11:38:10 +02:00
|
|
|
} elseif (! $this->checkAttribute($value, $settings->{$key})) {
|
2020-03-06 12:10:59 +01:00
|
|
|
return [$key, $value, $settings->{$key}];
|
2019-12-30 22:59:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
continue;
|
2020-03-21 06:37:30 +01:00
|
|
|
} elseif ($key == 'pdf_variables') {
|
2020-03-10 12:32:45 +01:00
|
|
|
continue;
|
|
|
|
}
|
2019-12-30 22:59:12 +01:00
|
|
|
|
|
|
|
/* Handles unset settings or blank strings */
|
2020-09-06 11:38:10 +02:00
|
|
|
if (! property_exists($settings, $key) || is_null($settings->{$key}) || ! isset($settings->{$key}) || $settings->{$key} == '') {
|
2019-12-30 22:59:12 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*Catch all filter */
|
2020-09-06 11:38:10 +02:00
|
|
|
if (! $this->checkAttribute($value, $settings->{$key})) {
|
2020-03-06 12:10:59 +01:00
|
|
|
return [$key, $value, $settings->{$key}];
|
2019-12-30 22:59:12 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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':
|
2022-04-18 15:10:43 +02:00
|
|
|
return is_numeric($value) && ctype_digit(strval(abs($value)));
|
2019-12-30 22:59:12 +01:00
|
|
|
case 'real':
|
|
|
|
case 'float':
|
|
|
|
case 'double':
|
2022-06-21 11:57:17 +02:00
|
|
|
return ! is_string($value) && (is_float($value) || is_numeric(strval($value)));
|
2019-12-30 22:59:12 +01:00
|
|
|
case 'string':
|
2022-06-21 11:57:17 +02:00
|
|
|
return ! is_int($value) || (is_string($value) && method_exists($value, '__toString')) || is_null($value) || is_string($value);
|
2019-12-30 22:59:12 +01:00
|
|
|
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-10-28 11:10:49 +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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|