Passed
Push — main ( a57e19...f52e71 )
by Vedrana
28:10
created

Card::getBlackJackNumericValue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 0
dl 0
loc 8
ccs 7
cts 7
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace App\Card;
4
5
/**
6
 * Class Card represents a standard playing card with a suit and a value.
7
 */
8
class Card
9
{
10
    /** @var string */
11
    private string $suit;
12
13
    /** @var string */
14
    private string $value;
15
16
    /**
17
     * Card constructor.
18
     *
19
     * @param string $value
20
     * @param string $suit
21
     */
22 52
    public function __construct(string $suit, string $value)
23
    {
24 52
        $this->suit = $suit;
25 52
        $this->value = $value;
26
    }
27
28
    /**
29
     * Get the suit of the card (Hearts, Diamonds, Clubs, Spades).
30
     *
31
     * @return string
32
     */
33 7
    public function getSuit(): string
34
    {
35 7
        return $this->suit;
36
    }
37
38
    /**
39
     * Get the value of the card (Ace, 2, 3, King).
40
     *
41
     * @return string
42
     */
43 19
    public function getValue(): string
44
    {
45 19
        return $this->value;
46
    }
47
48
    /**
49
     * Get the numeric/point value of the card.
50
     *
51
     * @return int
52
     */
53 3
    public function getNumericValue(): int
54
    {
55 3
        return match ($this->value) {
56 3
            'King'  => 13,
57 3
            'Queen' => 12,
58 3
            'Jack'  => 11,
59 3
            'Ace'   => 1,
60 3
            default => (int)$this->value,
61 3
        };
62
    }
63
64
    /**
65
     * Get the numeric/point value of the card for black jack game.
66
     *
67
     * @return int
68
     */
69 8
    public function getBlackJackNumericValue(): int
70
    {
71 8
        return match ($this->value) {
72 8
            'King'  => 10,
73 8
            'Queen' => 10,
74 8
            'Jack'  => 10,
75 8
            'Ace'   => 1,
76 8
            default => (int)$this->value,
77 8
        };
78
    }
79
80
    /**
81
     * String representation of the card.
82
     *
83
     * @return string The card in the format "Value of Suit" -> "King of Hearts".
84
    */
85 1
    public function __toString(): string
86
    {
87 1
        return "{$this->value} of {$this->suit}";
88
    }
89
90
91
    /**
92
     * Convert the card to an array.
93
     *
94
     * @return array{value: string, suit: string}
95
     */
96 8
    public function toArray(): array
97
    {
98 8
        return [
99 8
            'value' => $this->value,
100 8
            'suit' => $this->suit,
101 8
        ];
102
    }
103
}
104