SearchResult.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace OC\Collaboration\Collaborators;
  7. use OCP\Collaboration\Collaborators\ISearchResult;
  8. use OCP\Collaboration\Collaborators\SearchResultType;
  9. class SearchResult implements ISearchResult {
  10. protected array $result = [
  11. 'exact' => [],
  12. ];
  13. protected array $exactIdMatches = [];
  14. public function addResultSet(SearchResultType $type, array $matches, ?array $exactMatches = null): void {
  15. $type = $type->getLabel();
  16. if (!isset($this->result[$type])) {
  17. $this->result[$type] = [];
  18. $this->result['exact'][$type] = [];
  19. }
  20. $this->result[$type] = array_merge($this->result[$type], $matches);
  21. if (is_array($exactMatches)) {
  22. $this->result['exact'][$type] = array_merge($this->result['exact'][$type], $exactMatches);
  23. }
  24. }
  25. public function markExactIdMatch(SearchResultType $type): void {
  26. $this->exactIdMatches[$type->getLabel()] = 1;
  27. }
  28. public function hasExactIdMatch(SearchResultType $type): bool {
  29. return isset($this->exactIdMatches[$type->getLabel()]);
  30. }
  31. public function hasResult(SearchResultType $type, $collaboratorId): bool {
  32. $type = $type->getLabel();
  33. if (!isset($this->result[$type])) {
  34. return false;
  35. }
  36. $resultArrays = [$this->result['exact'][$type], $this->result[$type]];
  37. foreach ($resultArrays as $resultArray) {
  38. foreach ($resultArray as $result) {
  39. if ($result['value']['shareWith'] === $collaboratorId) {
  40. return true;
  41. }
  42. }
  43. }
  44. return false;
  45. }
  46. public function asArray(): array {
  47. return $this->result;
  48. }
  49. public function unsetResult(SearchResultType $type): void {
  50. $type = $type->getLabel();
  51. $this->result[$type] = [];
  52. if (isset($this->result['exact'][$type])) {
  53. $this->result['exact'][$type] = [];
  54. }
  55. }
  56. public function removeCollaboratorResult(SearchResultType $type, string $collaboratorId): bool {
  57. $type = $type->getLabel();
  58. if (!isset($this->result[$type])) {
  59. return false;
  60. }
  61. $actionDone = false;
  62. $resultArrays = [&$this->result['exact'][$type], &$this->result[$type]];
  63. foreach ($resultArrays as &$resultArray) {
  64. foreach ($resultArray as $k => $result) {
  65. if ($result['value']['shareWith'] === $collaboratorId) {
  66. unset($resultArray[$k]);
  67. $actionDone = true;
  68. }
  69. }
  70. }
  71. return $actionDone;
  72. }
  73. }