You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

115 lines
2.5 KiB

3 years ago
  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) <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. use function array_values;
  28. /**
  29. * @since 20.0.0
  30. */
  31. final class SearchResult implements JsonSerializable {
  32. /** @var string */
  33. private $name;
  34. /** @var bool */
  35. private $isPaginated;
  36. /** @var SearchResultEntry[] */
  37. private $entries;
  38. /** @var int|string|null */
  39. private $cursor;
  40. /**
  41. * @param string $name the translated name of the result section or group, e.g. "Mail"
  42. * @param bool $isPaginated
  43. * @param SearchResultEntry[] $entries
  44. * @param null $cursor
  45. *
  46. * @since 20.0.0
  47. */
  48. private function __construct(string $name,
  49. bool $isPaginated,
  50. array $entries,
  51. $cursor = null) {
  52. $this->name = $name;
  53. $this->isPaginated = $isPaginated;
  54. $this->entries = $entries;
  55. $this->cursor = $cursor;
  56. }
  57. /**
  58. * @param SearchResultEntry[] $entries
  59. *
  60. * @return static
  61. *
  62. * @since 20.0.0
  63. */
  64. public static function complete(string $name, array $entries): self {
  65. return new self(
  66. $name,
  67. false,
  68. $entries
  69. );
  70. }
  71. /**
  72. * @param SearchResultEntry[] $entries
  73. * @param int|string $cursor
  74. *
  75. * @return static
  76. *
  77. * @since 20.0.0
  78. */
  79. public static function paginated(string $name,
  80. array $entries,
  81. $cursor): self {
  82. return new self(
  83. $name,
  84. true,
  85. $entries,
  86. $cursor
  87. );
  88. }
  89. /**
  90. * @return array
  91. *
  92. * @since 20.0.0
  93. */
  94. public function jsonSerialize(): array {
  95. return [
  96. 'name' => $this->name,
  97. 'isPaginated' => $this->isPaginated,
  98. 'entries' => array_values($this->entries),
  99. 'cursor' => $this->cursor,
  100. ];
  101. }
  102. }