2021-10-23 11:29:16 +02:00
|
|
|
<?php
|
2022-01-02 10:49:50 +01:00
|
|
|
/**
|
2023-01-01 15:36:24 +01:00
|
|
|
* @copyright Copyright (C) 2010-2023, the Friendica project
|
2022-01-02 10:49:50 +01:00
|
|
|
*
|
|
|
|
* @license GNU AGPL version 3 or any later version
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
2021-10-23 11:29:16 +02:00
|
|
|
|
2022-12-28 00:20:20 +01:00
|
|
|
namespace Friendica\Core\PConfig\Util;
|
2021-10-23 11:29:16 +02:00
|
|
|
|
2021-10-26 21:44:29 +02:00
|
|
|
/**
|
|
|
|
* Util class to help to convert from/to (p)config values
|
|
|
|
*/
|
|
|
|
class ValueConversion
|
2021-10-23 11:29:16 +02:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Formats a DB value to a config value
|
|
|
|
* - null = The db-value isn't set
|
|
|
|
* - bool = The db-value is either '0' or '1'
|
|
|
|
* - array = The db-value is a serialized array
|
|
|
|
* - string = The db-value is a string
|
|
|
|
*
|
|
|
|
* Keep in mind that there aren't any numeric/integer config values in the database
|
|
|
|
*
|
2021-10-26 21:44:29 +02:00
|
|
|
* @param string|null $value
|
2021-10-23 11:29:16 +02:00
|
|
|
*
|
|
|
|
* @return null|array|string
|
|
|
|
*/
|
2021-10-26 21:44:29 +02:00
|
|
|
public static function toConfigValue(?string $value)
|
2021-10-23 11:29:16 +02:00
|
|
|
{
|
|
|
|
if (!isset($value)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (true) {
|
|
|
|
// manage array value
|
|
|
|
case preg_match("|^a:[0-9]+:{.*}$|s", $value):
|
|
|
|
return unserialize($value);
|
|
|
|
|
|
|
|
default:
|
|
|
|
return $value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Formats a config value to a DB value (string)
|
|
|
|
*
|
|
|
|
* @param mixed $value
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public static function toDbValue($value): string
|
|
|
|
{
|
|
|
|
// if not set, save an empty string
|
|
|
|
if (!isset($value)) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (true) {
|
|
|
|
// manage arrays
|
|
|
|
case is_array($value):
|
|
|
|
return serialize($value);
|
|
|
|
|
|
|
|
default:
|
|
|
|
return (string)$value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|