mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2024-11-14 07:02:34 +01:00
ba75a44eb8
* 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>
243 lines
7.5 KiB
PHP
243 lines
7.5 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;
|
|
use App\Models\Company;
|
|
use App\Utils\Ninja;
|
|
|
|
/**
|
|
* Class CompanySettingsSaver.
|
|
*
|
|
* Whilst it may appear that this CompanySettingsSaver and ClientGroupSettingsSaver
|
|
* could be duplicates, they are not.
|
|
*
|
|
* Each requires their own approach to saving and attempts to
|
|
* merge the two code paths should be avoided.
|
|
*/
|
|
trait CompanySettingsSaver
|
|
{
|
|
/**
|
|
* Saves a setting object.
|
|
*
|
|
* Works for groups|clients|companies
|
|
* @param array $settings The request input settings array
|
|
* @param object $entity The entity which the settings belongs to
|
|
* @return void
|
|
*/
|
|
public function saveSettings($settings, $entity)
|
|
{
|
|
/* No Settings, No Save!*/
|
|
if (! $settings) {
|
|
return;
|
|
}
|
|
|
|
//Unset Protected Properties.
|
|
foreach (CompanySettings::$protected_fields as $field) {
|
|
unset($settings[$field]);
|
|
}
|
|
|
|
$settings = $this->checkSettingType($settings);
|
|
|
|
$company_settings = CompanySettings::defaults();
|
|
|
|
foreach ($settings as $key => $value) {
|
|
if (is_null($settings->{$key})) {
|
|
$company_settings->{$key} = '';
|
|
} else {
|
|
$company_settings->{$key} = $value;
|
|
}
|
|
}
|
|
|
|
$entity->settings = $company_settings;
|
|
|
|
$entity->save();
|
|
}
|
|
|
|
/**
|
|
* 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;
|
|
}
|
|
|
|
/**
|
|
* Checks the settings object for
|
|
* correct property types.
|
|
*
|
|
* The method will drop invalid types from
|
|
* the object and will also settype() the property
|
|
* so that it can be saved cleanly
|
|
*
|
|
* @param array $settings The settings request() array
|
|
* @return object stdClass object
|
|
*/
|
|
private function checkSettingType($settings) : \stdClass
|
|
{
|
|
$settings = (object) $settings;
|
|
|
|
$casts = CompanySettings::$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})) {
|
|
if (substr($key, -3) == '_id') {
|
|
settype($settings->{$key}, 'string');
|
|
} else {
|
|
settype($settings->{$key}, $value);
|
|
}
|
|
} else {
|
|
unset($settings->{$key});
|
|
}
|
|
|
|
continue;
|
|
}
|
|
/*Separate loop if it is a _id field which is an integer cast as a string*/
|
|
if (substr($key, -3) == '_id' || substr($key, -14) == 'number_counter') {
|
|
$value = 'integer';
|
|
|
|
if (! property_exists($settings, $key)) {
|
|
continue;
|
|
} elseif ($this->checkAttribute($value, $settings->{$key})) {
|
|
if (substr($key, -3) == '_id') {
|
|
settype($settings->{$key}, 'string');
|
|
} else {
|
|
settype($settings->{$key}, $value);
|
|
}
|
|
} else {
|
|
unset($settings->{$key});
|
|
}
|
|
|
|
continue;
|
|
} elseif ($key == 'pdf_variables') {
|
|
settype($settings->{$key}, 'object');
|
|
}
|
|
|
|
/* 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})) {
|
|
if ($value == 'string' && is_null($settings->{$key})) {
|
|
$settings->{$key} = '';
|
|
}
|
|
|
|
settype($settings->{$key}, $value);
|
|
} else {
|
|
unset($settings->{$key});
|
|
}
|
|
}
|
|
|
|
return $settings;
|
|
}
|
|
|
|
/**
|
|
* 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;
|
|
}
|
|
}
|
|
|
|
private function getAccountFromEntity($entity)
|
|
{
|
|
if ($entity instanceof Company) {
|
|
return $entity->account;
|
|
}
|
|
|
|
return $entity->company->account;
|
|
}
|
|
}
|