feat(lexicon): specify default in constructor

Signed-off-by: Maxence Lange <maxence@artificial-owl.com>
feature/noid/wrapped-appconfig
Maxence Lange 4 weeks ago
parent d9c400d345
commit 0d58b3b0bb

@ -217,6 +217,7 @@ return array(
'OCP\\Comments\\NotFoundException' => $baseDir . '/lib/public/Comments/NotFoundException.php',
'OCP\\Common\\Exception\\NotFoundException' => $baseDir . '/lib/public/Common/Exception/NotFoundException.php',
'OCP\\ConfigLexicon\\ConfigLexiconEntry' => $baseDir . '/lib/public/ConfigLexicon/ConfigLexiconEntry.php',
'OCP\\ConfigLexicon\\ConfigLexiconValueType' => $baseDir . '/lib/public/ConfigLexicon/ConfigLexiconValueType.php',
'OCP\\ConfigLexicon\\IConfigLexicon' => $baseDir . '/lib/public/ConfigLexicon/IConfigLexicon.php',
'OCP\\ConfigLexicon\\IConfigLexiconEntry' => $baseDir . '/lib/public/ConfigLexicon/IConfigLexiconEntry.php',
'OCP\\Config\\BeforePreferenceDeletedEvent' => $baseDir . '/lib/public/Config/BeforePreferenceDeletedEvent.php',

@ -250,6 +250,7 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2
'OCP\\Comments\\NotFoundException' => __DIR__ . '/../../..' . '/lib/public/Comments/NotFoundException.php',
'OCP\\Common\\Exception\\NotFoundException' => __DIR__ . '/../../..' . '/lib/public/Common/Exception/NotFoundException.php',
'OCP\\ConfigLexicon\\ConfigLexiconEntry' => __DIR__ . '/../../..' . '/lib/public/ConfigLexicon/ConfigLexiconEntry.php',
'OCP\\ConfigLexicon\\ConfigLexiconValueType' => __DIR__ . '/../../..' . '/lib/public/ConfigLexicon/ConfigLexiconValueType.php',
'OCP\\ConfigLexicon\\IConfigLexicon' => __DIR__ . '/../../..' . '/lib/public/ConfigLexicon/IConfigLexicon.php',
'OCP\\ConfigLexicon\\IConfigLexiconEntry' => __DIR__ . '/../../..' . '/lib/public/ConfigLexicon/IConfigLexiconEntry.php',
'OCP\\Config\\BeforePreferenceDeletedEvent' => __DIR__ . '/../../..' . '/lib/public/Config/BeforePreferenceDeletedEvent.php',

@ -46,11 +46,23 @@ class ConfigLexiconEntry implements IConfigLexiconEntry {
public function __construct(
private readonly string $key,
private readonly ConfigLexiconValueType $type,
null|string|int|float|bool|array $default = null,
string $definition = '',
private readonly bool $lazy = false,
private readonly bool $sensitive = false,
private readonly bool $deprecated = false
) {
if ($default !== null) {
/** @psalm-suppress InvalidArgument */
$this->default = match ($type) {
ConfigLexiconValueType::STRING => $this->convertFromString($default),
ConfigLexiconValueType::INT => $this->convertFromInt($default),
ConfigLexiconValueType::FLOAT => $this->convertFromFloat($default),
ConfigLexiconValueType::BOOL => $this->convertFromBool($default),
ConfigLexiconValueType::ARRAY => $this->convertFromArray($default)
};
}
/** @psalm-suppress UndefinedClass */
if (\OC::$CLI) { // only store definition if ran from CLI
$this->definition = $definition;
@ -79,68 +91,48 @@ class ConfigLexiconEntry implements IConfigLexiconEntry {
}
/**
* @inheritDoc
*
* @param string $default
*
* @return self
* @return string
* @since 30.0.0
*/
public function withDefaultString(string $default): self {
$this->default = $default;
return $this;
private function convertFromString(string $default): string {
return $default;
}
/**
* @inheritDoc
*
* @param int $default
*
* @return self
* @return string
* @since 30.0.0
*/
public function withDefaultInt(int $default): self {
$this->default = (string) $default;
return $this;
private function convertFromInt(int $default): string {
return (string) $default;
}
/**
* @inheritDoc
*
* @param float $default
*
* @return self
* @return string
* @since 30.0.0
*/
public function withDefaultFloat(float $default): self {
$this->default = (string) $default;
return $this;
private function convertFromFloat(float $default): string {
return (string) $default;
}
/**
* @inheritDoc
*
* @param bool $default
*
* @return self
* @return string
* @since 30.0.0
*/
public function withDefaultBool(bool $default): self {
$this->default = ($default) ? '1' : '0';
return $this;
private function convertFromBool(bool $default): string {
return ($default) ? '1' : '0';
}
/**
* @inheritDoc
*
* @param array $default
*
* @return self
* @return string
* @since 30.0.0
*/
public function withDefaultArray(array $default): self {
$this->default = json_encode($default);
return $this;
private function convertFromArray(array $default): string {
return json_encode($default);
}
/**

@ -0,0 +1,44 @@
<?php
declare(strict_types=1);
/**
* @copyright Copyright (c) 2024 Maxence Lange <maxence@artificial-owl.com>
*
* @author Maxence Lange <maxence@artificial-owl.com>
*
* @license AGPL-3.0 or later
*
* This code is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* 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, version 3,
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/
namespace OCP\ConfigLexicon;
/**
* Listing of available value type for config lexicon
*
* @see IConfigLexicon
* @since 30.0.0
*/
enum ConfigLexiconValueType: string {
/** @since 30.0.0 */
case STRING = 'string';
/** @since 30.0.0 */
case INT = 'int';
/** @since 30.0.0 */
case FLOAT = 'float';
/** @since 30.0.0 */
case BOOL = 'bool';
/** @since 30.0.0 */
case ARRAY = 'array';
}

@ -26,19 +26,6 @@ namespace OCP\ConfigLexicon;
use OCP\IAppConfig;
enum ConfigLexiconValueType {
/** @since 30.0.0 */
case STRING;
/** @since 30.0.0 */
case INT;
/** @since 30.0.0 */
case FLOAT;
/** @since 30.0.0 */
case BOOL;
/** @since 30.0.0 */
case ARRAY;
}
/**
* Model that represent config values within an app config lexicon.
*
@ -46,7 +33,6 @@ enum ConfigLexiconValueType {
* @since 30.0.0
*/
interface IConfigLexiconEntry {
/**
* returns the config key.
*
@ -64,56 +50,6 @@ interface IConfigLexiconEntry {
*/
public function getValueType(): ConfigLexiconValueType;
/**
* set default value (as string) for config value.
*
* @param string $default
*
* @return self
* @since 30.0.0
*/
public function withDefaultString(string $default): self;
/**
* set default value (as int) for config value.
*
* @param int $default
*
* @return self
* @since 30.0.0
*/
public function withDefaultInt(int $default): self;
/**
* set default value (as float) for config value.
*
* @param float $default
*
* @return self
* @since 30.0.0
*/
public function withDefaultFloat(float $default): self;
/**
* set default value (as bool) for config value.
*
* @param bool $default
*
* @return self
* @since 30.0.0
*/
public function withDefaultBool(bool $default): self;
/**
* set default value (as array) for config value.
*
* @param array $default
*
* @return self
* @since 30.0.0
*/
public function withDefaultArray(array $default): self;
/**
* returns the default value set for this config key.
* default value is returned as string or NULL if not set.
@ -123,7 +59,6 @@ interface IConfigLexiconEntry {
*/
public function getDefault(): ?string;
/**
* returns the description for config key, only available when process is initiated from occ.
* returns empty string if not set or if process is not initiated from occ.

Loading…
Cancel
Save