Navigation.php 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. declare(strict_types=1);
  7. namespace Magento\Customer\Block\Account;
  8. use \Magento\Framework\View\Element\Html\Links;
  9. use \Magento\Customer\Block\Account\SortLinkInterface;
  10. /**
  11. * Class for sorting links in navigation panels.
  12. *
  13. * @api
  14. * @since 101.0.0
  15. */
  16. class Navigation extends Links
  17. {
  18. /**
  19. * {@inheritdoc}
  20. * @since 101.0.0
  21. */
  22. public function getLinks()
  23. {
  24. $links = $this->_layout->getChildBlocks($this->getNameInLayout());
  25. $sortableLink = [];
  26. foreach ($links as $key => $link) {
  27. if ($link instanceof SortLinkInterface) {
  28. $sortableLink[] = $link;
  29. unset($links[$key]);
  30. }
  31. }
  32. usort($sortableLink, [$this, "compare"]);
  33. return array_merge($sortableLink, $links);
  34. }
  35. /**
  36. * Compare sortOrder in links.
  37. *
  38. * @param SortLinkInterface $firstLink
  39. * @param SortLinkInterface $secondLink
  40. * @return int
  41. * @SuppressWarnings(PHPMD.UnusedPrivateMethod)
  42. */
  43. private function compare(SortLinkInterface $firstLink, SortLinkInterface $secondLink): int
  44. {
  45. return $secondLink->getSortOrder() <=> $firstLink->getSortOrder();
  46. }
  47. }