ProfileConfig.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright 2021 Christopher Ng <chrng8@gmail.com>
  5. *
  6. * @author Christopher Ng <chrng8@gmail.com>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OC\Core\Db;
  25. use JsonSerializable;
  26. use OCP\AppFramework\Db\Entity;
  27. use OCP\Profile\ParameterDoesNotExistException;
  28. use function json_decode;
  29. use function json_encode;
  30. /**
  31. * @method string getUserId()
  32. * @method void setUserId(string $userId)
  33. * @method string getConfig()
  34. * @method void setConfig(string $config)
  35. */
  36. class ProfileConfig extends Entity implements JsonSerializable {
  37. /** @var string */
  38. protected $userId;
  39. /** @var string */
  40. protected $config;
  41. public function __construct() {
  42. $this->addType('userId', 'string');
  43. $this->addType('config', 'string');
  44. }
  45. /**
  46. * Returns the config in an associative array
  47. */
  48. public function getConfigArray(): array {
  49. return json_decode($this->config, true);
  50. }
  51. /**
  52. * Set the config
  53. */
  54. public function setConfigArray(array $config): void {
  55. $this->setConfig(json_encode($config));
  56. }
  57. /**
  58. * Returns the visibility map in an associative array
  59. */
  60. public function getVisibilityMap(): array {
  61. $config = $this->getConfigArray();
  62. $visibilityMap = [];
  63. foreach ($config as $paramId => $paramConfig) {
  64. $visibilityMap[$paramId] = $paramConfig['visibility'];
  65. }
  66. return $visibilityMap;
  67. }
  68. /**
  69. * Set the visibility map
  70. */
  71. public function setVisibilityMap(array $visibilityMap): void {
  72. $config = $this->getConfigArray();
  73. foreach ($visibilityMap as $paramId => $visibility) {
  74. $config[$paramId] = array_merge(
  75. $config[$paramId] ?: [],
  76. ['visibility' => $visibility],
  77. );
  78. }
  79. $this->setConfigArray($config);
  80. }
  81. /**
  82. * Returns the visibility of the parameter
  83. *
  84. * @throws ParameterDoesNotExistException
  85. */
  86. public function getVisibility(string $paramId): string {
  87. $visibilityMap = $this->getVisibilityMap();
  88. if (isset($visibilityMap[$paramId])) {
  89. return $visibilityMap[$paramId];
  90. }
  91. throw new ParameterDoesNotExistException($paramId);
  92. }
  93. /**
  94. * Set the visibility of the parameter
  95. */
  96. public function setVisibility(string $paramId, string $visibility): void {
  97. $visibilityMap = $this->getVisibilityMap();
  98. $visibilityMap[$paramId] = $visibility;
  99. $this->setVisibilityMap($visibilityMap);
  100. }
  101. public function jsonSerialize(): array {
  102. return [
  103. 'userId' => $this->userId,
  104. 'config' => $this->getConfigArray(),
  105. ];
  106. }
  107. }