WidgetItems.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2023 Richard Steinmetz <richard@steinmetz.cloud>
  5. *
  6. * @author Richard Steinmetz <richard@steinmetz.cloud>
  7. *
  8. * @license AGPL-3.0-or-later
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation, either version 3 of the License, or
  13. * (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 General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OCP\Dashboard\Model;
  25. use JsonSerializable;
  26. use OCP\Dashboard\IAPIWidgetV2;
  27. /**
  28. * Interface WidgetItems
  29. *
  30. * This class is used by {@see IAPIWidgetV2} interface.
  31. * It represents an array of widget items and additional context information that can be provided to clients via the Dashboard API
  32. *
  33. * @see IAPIWidgetV2::getItemsV2
  34. *
  35. * @since 27.1.0
  36. */
  37. class WidgetItems implements JsonSerializable {
  38. /**
  39. * @param $items WidgetItem[]
  40. *
  41. * @since 27.1.0
  42. */
  43. public function __construct(
  44. private array $items = [],
  45. private string $emptyContentMessage = '',
  46. private string $halfEmptyContentMessage = '',
  47. ) {
  48. }
  49. /**
  50. * Items to render in the widgets
  51. *
  52. * @since 27.1.0
  53. *
  54. * @return WidgetItem[]
  55. */
  56. public function getItems(): array {
  57. return $this->items;
  58. }
  59. /**
  60. * The "half" empty content message to show above the list of items.
  61. *
  62. * A non-empty string enables this feature.
  63. * An empty string hides the message and disables this feature.
  64. *
  65. * @since 27.1.0
  66. */
  67. public function getEmptyContentMessage(): string {
  68. return $this->emptyContentMessage;
  69. }
  70. /**
  71. * The empty content message to show in case of no items at all
  72. *
  73. * @since 27.1.0
  74. */
  75. public function getHalfEmptyContentMessage(): string {
  76. return $this->halfEmptyContentMessage;
  77. }
  78. /**
  79. * @since 27.1.0
  80. */
  81. public function jsonSerialize(): array {
  82. $items = array_map(static function (WidgetItem $item) {
  83. return $item->jsonSerialize();
  84. }, $this->getItems());
  85. return [
  86. 'items' => $items,
  87. 'emptyContentMessage' => $this->getEmptyContentMessage(),
  88. 'halfEmptyContentMessage' => $this->getHalfEmptyContentMessage(),
  89. ];
  90. }
  91. }