Completed
Push — master ( ab8332...e88ec9 )
by Rémi
03:04
created

Word::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Hangman;
4
5
class Word
6
{
7
    private $word;
8
9
    /**
10
     * Word constructor.
11
     *
12
     * @param $word
13
     */
14 111
    public function __construct($word)
15
    {
16 111
        $this->word = strtoupper($word);
17 111
    }
18
19
    /**
20
     * Get the letters of the word
21
     *
22
     * @return string[]
23
     */
24 42
    public function getLetters()
25
    {
26 42
        return array_unique(str_split(strtoupper($this->word)));
27
    }
28
29
    /**
30
     * Build the word from played letters
31
     *
32
     * @param string[] $playedLetters
33
     *
34
     * @return string
35
     */
36 42
    public function buildWord($playedLetters)
37
    {
38 42
        $wordLetters = $this->getLetters();
39 42
        $goodLetters = array_intersect($wordLetters, $playedLetters);
40 42
        $splitWord = str_split($this->word);
41
42 42
        $word = '';
43 42
        foreach ($splitWord as $letter) {
44 42
            $word .= (in_array($letter, $goodLetters) ? $letter : '_') . ' ';
45 28
        }
46
47 42
        return trim($word);
48
    }
49
50
    /**
51
     * Checks if the answer is valid
52
     *
53
     * @param string $answer
54
     *
55
     * @return bool
56
     */
57 12
    public function isValid($answer)
58
    {
59 12
        return strlen($answer) === strlen($this->word);
60
    }
61
62
    /**
63
     * Returns if the letter is contained in the word
64
     *
65
     * @param string $letter
66
     *
67
     * @return boolean
68
     */
69 15
    public function contains($letter)
70
    {
71 15
        return strpos(strtoupper($this->word), strtoupper($letter)) !== false;
72
    }
73
74
    /**
75
     * @param string $word
76
     *
77
     * @return bool
78
     */
79 9
    public function equals($word)
80
    {
81 9
        return ($this->word === strtoupper($word));
82
    }
83
84
    /**
85
     * @return string
86
     */
87 27
    public function __toString()
88
    {
89 27
        return $this->word;
90
    }
91
}
92