1
0

SearchResultEntry.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2020 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCP\Search;
  8. use JsonSerializable;
  9. /**
  10. * Represents an entry in a list of results an app returns for a unified search
  11. * query.
  12. *
  13. * The app providing the results has to extend this class for customization. In
  14. * most cases apps do not have to add any additional code.
  15. *
  16. * @example ``class MailResultEntry extends SearchResultEntry {}`
  17. *
  18. * This approach was chosen over a final class as it allows Nextcloud to later
  19. * add new optional properties of an entry without having to break the usage of
  20. * this class in apps.
  21. *
  22. * @since 20.0.0
  23. */
  24. class SearchResultEntry implements JsonSerializable {
  25. /**
  26. * @var string
  27. * @since 20.0.0
  28. */
  29. protected $thumbnailUrl;
  30. /**
  31. * @var string
  32. * @since 20.0.0
  33. */
  34. protected $title;
  35. /**
  36. * @var string
  37. * @since 20.0.0
  38. */
  39. protected $subline;
  40. /**
  41. * @var string
  42. * @since 20.0.0
  43. */
  44. protected $resourceUrl;
  45. /**
  46. * @var string
  47. * @since 20.0.0
  48. */
  49. protected $icon;
  50. /**
  51. * @var boolean
  52. * @since 20.0.0
  53. */
  54. protected $rounded;
  55. /**
  56. * @var string[]
  57. * @psalm-var array<string, string>
  58. * @since 20.0.0
  59. */
  60. protected $attributes = [];
  61. /**
  62. * @param string $thumbnailUrl a relative or absolute URL to the thumbnail or icon of the entry
  63. * @param string $title a main title of the entry
  64. * @param string $subline the secondary line of the entry
  65. * @param string $resourceUrl the URL where the user can find the detail, like a deep link inside the app
  66. * @param string $icon the icon class or url to the icon
  67. * @param boolean $rounded is the thumbnail rounded
  68. *
  69. * @since 20.0.0
  70. */
  71. public function __construct(string $thumbnailUrl,
  72. string $title,
  73. string $subline,
  74. string $resourceUrl,
  75. string $icon = '',
  76. bool $rounded = false) {
  77. $this->thumbnailUrl = $thumbnailUrl;
  78. $this->title = $title;
  79. $this->subline = $subline;
  80. $this->resourceUrl = $resourceUrl;
  81. $this->icon = $icon;
  82. $this->rounded = $rounded;
  83. }
  84. /**
  85. * Add optional attributes to the result entry, e.g. an ID or some other
  86. * context information that can be read by the client application
  87. *
  88. * @param string $key
  89. * @param string $value
  90. *
  91. * @since 20.0.0
  92. */
  93. public function addAttribute(string $key, string $value): void {
  94. $this->attributes[$key] = $value;
  95. }
  96. /**
  97. * @return array
  98. *
  99. * @since 20.0.0
  100. */
  101. public function jsonSerialize(): array {
  102. return [
  103. 'thumbnailUrl' => $this->thumbnailUrl,
  104. 'title' => $this->title,
  105. 'subline' => $this->subline,
  106. 'resourceUrl' => $this->resourceUrl,
  107. 'icon' => $this->icon,
  108. 'rounded' => $this->rounded,
  109. 'attributes' => $this->attributes,
  110. ];
  111. }
  112. }