ProfileConfig.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2021 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OC\Core\Db;
  8. use JsonSerializable;
  9. use OCP\AppFramework\Db\Entity;
  10. use OCP\Profile\ParameterDoesNotExistException;
  11. use function json_decode;
  12. use function json_encode;
  13. /**
  14. * @method string getUserId()
  15. * @method void setUserId(string $userId)
  16. * @method string getConfig()
  17. * @method void setConfig(string $config)
  18. */
  19. class ProfileConfig extends Entity implements JsonSerializable {
  20. /** @var string */
  21. protected $userId;
  22. /** @var string */
  23. protected $config;
  24. public function __construct() {
  25. $this->addType('userId', 'string');
  26. $this->addType('config', 'string');
  27. }
  28. /**
  29. * Returns the config in an associative array
  30. */
  31. public function getConfigArray(): array {
  32. return json_decode($this->config, true);
  33. }
  34. /**
  35. * Set the config
  36. */
  37. public function setConfigArray(array $config): void {
  38. $this->setConfig(json_encode($config));
  39. }
  40. /**
  41. * Returns the visibility map in an associative array
  42. */
  43. public function getVisibilityMap(): array {
  44. $config = $this->getConfigArray();
  45. $visibilityMap = [];
  46. foreach ($config as $paramId => $paramConfig) {
  47. $visibilityMap[$paramId] = $paramConfig['visibility'];
  48. }
  49. return $visibilityMap;
  50. }
  51. /**
  52. * Set the visibility map
  53. */
  54. public function setVisibilityMap(array $visibilityMap): void {
  55. $config = $this->getConfigArray();
  56. foreach ($visibilityMap as $paramId => $visibility) {
  57. $config[$paramId] = array_merge(
  58. $config[$paramId] ?: [],
  59. ['visibility' => $visibility],
  60. );
  61. }
  62. $this->setConfigArray($config);
  63. }
  64. /**
  65. * Returns the visibility of the parameter
  66. *
  67. * @throws ParameterDoesNotExistException
  68. */
  69. public function getVisibility(string $paramId): string {
  70. $visibilityMap = $this->getVisibilityMap();
  71. if (isset($visibilityMap[$paramId])) {
  72. return $visibilityMap[$paramId];
  73. }
  74. throw new ParameterDoesNotExistException($paramId);
  75. }
  76. /**
  77. * Set the visibility of the parameter
  78. */
  79. public function setVisibility(string $paramId, string $visibility): void {
  80. $visibilityMap = $this->getVisibilityMap();
  81. $visibilityMap[$paramId] = $visibility;
  82. $this->setVisibilityMap($visibilityMap);
  83. }
  84. public function jsonSerialize(): array {
  85. return [
  86. 'userId' => $this->userId,
  87. 'config' => $this->getConfigArray(),
  88. ];
  89. }
  90. }