SearchResult.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2017 Arthur Schiwon <blizzz@arthur-schiwon.de>
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Joas Schilling <coding@schilljs.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 OC\Collaboration\Collaborators;
  26. use OCP\Collaboration\Collaborators\ISearchResult;
  27. use OCP\Collaboration\Collaborators\SearchResultType;
  28. class SearchResult implements ISearchResult {
  29. protected array $result = [
  30. 'exact' => [],
  31. ];
  32. protected array $exactIdMatches = [];
  33. public function addResultSet(SearchResultType $type, array $matches, ?array $exactMatches = null): void {
  34. $type = $type->getLabel();
  35. if (!isset($this->result[$type])) {
  36. $this->result[$type] = [];
  37. $this->result['exact'][$type] = [];
  38. }
  39. $this->result[$type] = array_merge($this->result[$type], $matches);
  40. if (is_array($exactMatches)) {
  41. $this->result['exact'][$type] = array_merge($this->result['exact'][$type], $exactMatches);
  42. }
  43. }
  44. public function markExactIdMatch(SearchResultType $type): void {
  45. $this->exactIdMatches[$type->getLabel()] = 1;
  46. }
  47. public function hasExactIdMatch(SearchResultType $type): bool {
  48. return isset($this->exactIdMatches[$type->getLabel()]);
  49. }
  50. public function hasResult(SearchResultType $type, $collaboratorId): bool {
  51. $type = $type->getLabel();
  52. if (!isset($this->result[$type])) {
  53. return false;
  54. }
  55. $resultArrays = [$this->result['exact'][$type], $this->result[$type]];
  56. foreach ($resultArrays as $resultArray) {
  57. foreach ($resultArray as $result) {
  58. if ($result['value']['shareWith'] === $collaboratorId) {
  59. return true;
  60. }
  61. }
  62. }
  63. return false;
  64. }
  65. public function asArray(): array {
  66. return $this->result;
  67. }
  68. public function unsetResult(SearchResultType $type): void {
  69. $type = $type->getLabel();
  70. $this->result[$type] = [];
  71. if (isset($this->result['exact'][$type])) {
  72. $this->result['exact'][$type] = [];
  73. }
  74. }
  75. public function removeCollaboratorResult(SearchResultType $type, string $collaboratorId): bool {
  76. $type = $type->getLabel();
  77. if (!isset($this->result[$type])) {
  78. return false;
  79. }
  80. $actionDone = false;
  81. $resultArrays = [&$this->result['exact'][$type], &$this->result[$type]];
  82. foreach ($resultArrays as &$resultArray) {
  83. foreach ($resultArray as $k => $result) {
  84. if ($result['value']['shareWith'] === $collaboratorId) {
  85. unset($resultArray[$k]);
  86. $actionDone = true;
  87. }
  88. }
  89. }
  90. return $actionDone;
  91. }
  92. }