1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-06 03:02:34 +01:00
invoiceninja/app/Utils/Traits/SettingsSaver.php
David Bomba 280e42d366
Hosted platform rules (#3650)
* Filter properties which can be saved on free accounts

* Self Updater

* Fixes for tests

* Refactor for settings

* Working on feature permissions - Settings Saver

* Fixes for events on self-updater

* Working on Self Updater

* Working on free /pro settings saver

* Implement free/pro/enterprise saving for settings

* Update company request

* Implement settings saver for hosted platform for clients and group level settings

* Implement quotas for hosted version

* Validation rules for hosted platform"
2020-04-21 15:16:45 +10:00

111 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\CompanySettings;
/**
* Class SettingsSaver
* @package App\Utils\Traits
*/
trait SettingsSaver
{
/**
* 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)
{
$settings = (object)$settings;
$casts = CompanySettings::$casts;
ksort($casts);
foreach ($casts as $key => $value) {
if (in_array($key, CompanySettings::$string_casts)) {
$value = "string";
if (!property_exists($settings, $key)) {
continue;
} elseif (!$this->checkAttribute($value, $settings->{$key})) {
return [$key, $value, $settings->{$key}];
}
continue;
}
/*Separate loop if it is a _id field which is an integer cast as a string*/
elseif (substr($key, -3) == '_id' || substr($key, -14) == 'number_counter') {
$value = "integer";
if (!property_exists($settings, $key)) {
continue;
} elseif (!$this->checkAttribute($value, $settings->{$key})) {
return [$key, $value, $settings->{$key}];
}
continue;
} elseif ($key == 'pdf_variables') {
continue;
}
/* Handles unset settings or blank strings */
if (!property_exists($settings, $key) || is_null($settings->{$key}) || !isset($settings->{$key}) || $settings->{$key} == '') {
continue;
}
/*Catch all filter */
if (!$this->checkAttribute($value, $settings->{$key})) {
return [$key, $value, $settings->{$key}];
}
}
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(abs($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;
}
}
}