1
0

SearchOrder.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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\Files\Search;
  7. use OCP\Files\FileInfo;
  8. use OCP\Files\Search\ISearchOrder;
  9. class SearchOrder implements ISearchOrder {
  10. public function __construct(
  11. private string $direction,
  12. private string $field,
  13. private string $extra = '',
  14. ) {
  15. }
  16. /**
  17. * @return string
  18. */
  19. public function getDirection(): string {
  20. return $this->direction;
  21. }
  22. /**
  23. * @return string
  24. */
  25. public function getField(): string {
  26. return $this->field;
  27. }
  28. /**
  29. * @return string
  30. * @since 28.0.0
  31. */
  32. public function getExtra(): string {
  33. return $this->extra;
  34. }
  35. public function sortFileInfo(FileInfo $a, FileInfo $b): int {
  36. $cmp = $this->sortFileInfoNoDirection($a, $b);
  37. return $cmp * ($this->direction === ISearchOrder::DIRECTION_ASCENDING ? 1 : -1);
  38. }
  39. private function sortFileInfoNoDirection(FileInfo $a, FileInfo $b): int {
  40. switch ($this->field) {
  41. case 'name':
  42. return $a->getName() <=> $b->getName();
  43. case 'mimetype':
  44. return $a->getMimetype() <=> $b->getMimetype();
  45. case 'mtime':
  46. return $a->getMtime() <=> $b->getMtime();
  47. case 'size':
  48. return $a->getSize() <=> $b->getSize();
  49. case 'fileid':
  50. return $a->getId() <=> $b->getId();
  51. case 'permissions':
  52. return $a->getPermissions() <=> $b->getPermissions();
  53. default:
  54. return 0;
  55. }
  56. }
  57. }