SearchResultEntry.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright 2020 Christoph Wurst <christoph@winzerhof-wurst.at>
  5. *
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author John Molakvoæ <skjnldsv@protonmail.com>
  8. *
  9. * @license GNU AGPL version 3 or any later version
  10. *
  11. * This program is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License as
  13. * published by the Free Software Foundation, either version 3 of the
  14. * License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. *
  24. */
  25. namespace OCP\Search;
  26. use JsonSerializable;
  27. /**
  28. * Represents an entry in a list of results an app returns for a unified search
  29. * query.
  30. *
  31. * The app providing the results has to extend this class for customization. In
  32. * most cases apps do not have to add any additional code.
  33. *
  34. * @example ``class MailResultEntry extends SearchResultEntry {}`
  35. *
  36. * This approach was chosen over a final class as it allows Nextcloud to later
  37. * add new optional properties of an entry without having to break the usage of
  38. * this class in apps.
  39. *
  40. * @since 20.0.0
  41. */
  42. class SearchResultEntry implements JsonSerializable {
  43. /**
  44. * @var string
  45. * @since 20.0.0
  46. */
  47. protected $thumbnailUrl;
  48. /**
  49. * @var string
  50. * @since 20.0.0
  51. */
  52. protected $title;
  53. /**
  54. * @var string
  55. * @since 20.0.0
  56. */
  57. protected $subline;
  58. /**
  59. * @var string
  60. * @since 20.0.0
  61. */
  62. protected $resourceUrl;
  63. /**
  64. * @var string
  65. * @since 20.0.0
  66. */
  67. protected $icon;
  68. /**
  69. * @var boolean
  70. * @since 20.0.0
  71. */
  72. protected $rounded;
  73. /**
  74. * @var string[]
  75. * @psalm-var array<string, string>
  76. * @since 20.0.0
  77. */
  78. protected $attributes = [];
  79. /**
  80. * @param string $thumbnailUrl a relative or absolute URL to the thumbnail or icon of the entry
  81. * @param string $title a main title of the entry
  82. * @param string $subline the secondary line of the entry
  83. * @param string $resourceUrl the URL where the user can find the detail, like a deep link inside the app
  84. * @param string $icon the icon class or url to the icon
  85. * @param boolean $rounded is the thumbnail rounded
  86. *
  87. * @since 20.0.0
  88. */
  89. public function __construct(string $thumbnailUrl,
  90. string $title,
  91. string $subline,
  92. string $resourceUrl,
  93. string $icon = '',
  94. bool $rounded = false) {
  95. $this->thumbnailUrl = $thumbnailUrl;
  96. $this->title = $title;
  97. $this->subline = $subline;
  98. $this->resourceUrl = $resourceUrl;
  99. $this->icon = $icon;
  100. $this->rounded = $rounded;
  101. }
  102. /**
  103. * Add optional attributes to the result entry, e.g. an ID or some other
  104. * context information that can be read by the client application
  105. *
  106. * @param string $key
  107. * @param string $value
  108. *
  109. * @since 20.0.0
  110. */
  111. public function addAttribute(string $key, string $value): void {
  112. $this->attributes[$key] = $value;
  113. }
  114. /**
  115. * @return array
  116. *
  117. * @since 20.0.0
  118. */
  119. public function jsonSerialize(): array {
  120. return [
  121. 'thumbnailUrl' => $this->thumbnailUrl,
  122. 'title' => $this->title,
  123. 'subline' => $this->subline,
  124. 'resourceUrl' => $this->resourceUrl,
  125. 'icon' => $this->icon,
  126. 'rounded' => $this->rounded,
  127. 'attributes' => $this->attributes,
  128. ];
  129. }
  130. }