SearchResult.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. use function array_values;
  10. /**
  11. * @since 20.0.0
  12. */
  13. final class SearchResult implements JsonSerializable {
  14. /** @var string */
  15. private $name;
  16. /** @var bool */
  17. private $isPaginated;
  18. /** @var SearchResultEntry[] */
  19. private $entries;
  20. /** @var int|string|null */
  21. private $cursor;
  22. /**
  23. * @param string $name the translated name of the result section or group, e.g. "Mail"
  24. * @param bool $isPaginated
  25. * @param SearchResultEntry[] $entries
  26. * @param ?int|?string $cursor
  27. *
  28. * @since 20.0.0
  29. */
  30. private function __construct(string $name,
  31. bool $isPaginated,
  32. array $entries,
  33. $cursor = null) {
  34. $this->name = $name;
  35. $this->isPaginated = $isPaginated;
  36. $this->entries = $entries;
  37. $this->cursor = $cursor;
  38. }
  39. /**
  40. * @param SearchResultEntry[] $entries
  41. *
  42. * @return static
  43. *
  44. * @since 20.0.0
  45. */
  46. public static function complete(string $name, array $entries): self {
  47. return new self(
  48. $name,
  49. false,
  50. $entries
  51. );
  52. }
  53. /**
  54. * @param SearchResultEntry[] $entries
  55. * @param int|string $cursor
  56. *
  57. * @return static
  58. *
  59. * @since 20.0.0
  60. */
  61. public static function paginated(string $name,
  62. array $entries,
  63. $cursor): self {
  64. return new self(
  65. $name,
  66. true,
  67. $entries,
  68. $cursor
  69. );
  70. }
  71. /**
  72. * @return array
  73. *
  74. * @since 20.0.0
  75. */
  76. public function jsonSerialize(): array {
  77. return [
  78. 'name' => $this->name,
  79. 'isPaginated' => $this->isPaginated,
  80. 'entries' => array_values($this->entries),
  81. 'cursor' => $this->cursor,
  82. ];
  83. }
  84. }