garak/bridge

View on GitHub
src/CardSorter.php

Summary

Maintainability
A
30 mins
Test Coverage
A
100%
<?php

namespace Garak\Bridge;

use Garak\Card\Card;
use Garak\Card\Suit;

final class CardSorter
{
    /** @param array<int|string, Card> $cards */
    public static function sort(array &$cards, ?Suit $trump): void
    {
        \usort($cards, static function (Card $card2, Card $card1) use ($trump): int {
            if (null !== $trump && ($card1->getSuit()->isEqual($trump) || $card2->getSuit()->isEqual($trump))) {
                if ($card1->getSuit()->isEqual($trump) && !$card2->getSuit()->isEqual($trump)) {
                    return 1;
                }
                if (!$card1->getSuit()->isEqual($trump) && $card2->getSuit()->isEqual($trump)) {
                    return -1;
                }

                return $card1->getRank()->getInt() <=> $card2->getRank()->getInt();
            }

            if ($card1->getSuit()->isEqual($card2->getSuit())) {
                return $card1->getRank()->getInt() <=> $card2->getRank()->getInt();
            }

            return $card1->getSuit()->getInt() <=> $card2->getSuit()->getInt();
        });
    }
}