ConfigLexiconEntry.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace NCU\Config\Lexicon;
  8. use NCU\Config\ValueType;
  9. /**
  10. * Model that represent config values within an app config lexicon.
  11. *
  12. * @see IConfigLexicon
  13. * @experimental 31.0.0
  14. */
  15. class ConfigLexiconEntry {
  16. private string $definition = '';
  17. private ?string $default = null;
  18. /**
  19. * @param string $key config key
  20. * @param ValueType $type type of config value
  21. * @param string $definition optional description of config key available when using occ command
  22. * @param bool $lazy set config value as lazy
  23. * @param int $flags set flags
  24. * @param bool $deprecated set config key as deprecated
  25. *
  26. * @experimental 31.0.0
  27. * @psalm-suppress PossiblyInvalidCast
  28. * @psalm-suppress RiskyCast
  29. */
  30. public function __construct(
  31. private readonly string $key,
  32. private readonly ValueType $type,
  33. null|string|int|float|bool|array $default = null,
  34. string $definition = '',
  35. private readonly bool $lazy = false,
  36. private readonly int $flags = 0,
  37. private readonly bool $deprecated = false,
  38. ) {
  39. if ($default !== null) {
  40. // in case $default is array but is not expected to be an array...
  41. $default = ($type !== ValueType::ARRAY && is_array($default)) ? json_encode($default) : $default;
  42. $this->default = match ($type) {
  43. ValueType::MIXED => (string)$default,
  44. ValueType::STRING => $this->convertFromString((string)$default),
  45. ValueType::INT => $this->convertFromInt((int)$default),
  46. ValueType::FLOAT => $this->convertFromFloat((float)$default),
  47. ValueType::BOOL => $this->convertFromBool((bool)$default),
  48. ValueType::ARRAY => $this->convertFromArray((array)$default)
  49. };
  50. }
  51. /** @psalm-suppress UndefinedClass */
  52. if (\OC::$CLI) { // only store definition if ran from CLI
  53. $this->definition = $definition;
  54. }
  55. }
  56. /**
  57. * @inheritDoc
  58. *
  59. * @return string config key
  60. * @experimental 31.0.0
  61. */
  62. public function getKey(): string {
  63. return $this->key;
  64. }
  65. /**
  66. * @inheritDoc
  67. *
  68. * @return ValueType
  69. * @experimental 31.0.0
  70. */
  71. public function getValueType(): ValueType {
  72. return $this->type;
  73. }
  74. /**
  75. * @param string $default
  76. * @return string
  77. * @experimental 31.0.0
  78. */
  79. private function convertFromString(string $default): string {
  80. return $default;
  81. }
  82. /**
  83. * @param int $default
  84. * @return string
  85. * @experimental 31.0.0
  86. */
  87. private function convertFromInt(int $default): string {
  88. return (string)$default;
  89. }
  90. /**
  91. * @param float $default
  92. * @return string
  93. * @experimental 31.0.0
  94. */
  95. private function convertFromFloat(float $default): string {
  96. return (string)$default;
  97. }
  98. /**
  99. * @param bool $default
  100. * @return string
  101. * @experimental 31.0.0
  102. */
  103. private function convertFromBool(bool $default): string {
  104. return ($default) ? '1' : '0';
  105. }
  106. /**
  107. * @param array $default
  108. * @return string
  109. * @experimental 31.0.0
  110. */
  111. private function convertFromArray(array $default): string {
  112. return json_encode($default);
  113. }
  114. /**
  115. * @inheritDoc
  116. *
  117. * @return string|null NULL if no default is set
  118. * @experimental 31.0.0
  119. */
  120. public function getDefault(): ?string {
  121. return $this->default;
  122. }
  123. /**
  124. * @inheritDoc
  125. *
  126. * @return string
  127. * @experimental 31.0.0
  128. */
  129. public function getDefinition(): string {
  130. return $this->definition;
  131. }
  132. /**
  133. * @inheritDoc
  134. *
  135. * @see IAppConfig for details on lazy config values
  136. * @return bool TRUE if config value is lazy
  137. * @experimental 31.0.0
  138. */
  139. public function isLazy(): bool {
  140. return $this->lazy;
  141. }
  142. /**
  143. * @inheritDoc
  144. *
  145. * @see IAppConfig for details on sensitive config values
  146. * @return int bitflag about the config value
  147. * @experimental 31.0.0
  148. */
  149. public function getFlags(): int {
  150. return $this->flags;
  151. }
  152. /**
  153. * @param int $flag
  154. *
  155. * @return bool TRUE is config value bitflag contains $flag
  156. * @experimental 31.0.0
  157. */
  158. public function isFlagged(int $flag): bool {
  159. return (($flag & $this->getFlags()) === $flag);
  160. }
  161. /**
  162. * @inheritDoc
  163. *
  164. * @return bool TRUE if config si deprecated
  165. * @experimental 31.0.0
  166. */
  167. public function isDeprecated(): bool {
  168. return $this->deprecated;
  169. }
  170. }