Passed
Branch master (e882ad)
by Zacchaeus
10:41 queued 05:42
created

Word::checkIs()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
namespace Djunehor\Grammar;
4
5
class Word
6
{
7
    private $string;
8
9
    private $partsOfSpeech = [
10
        'prep.' => 'Preposition',
11
        'n.' => 'Noun',
12
        'v.' => 'Verb',
13
        'a.' => 'Adjective',
14
        'adv.' => 'Adverb',
15
        'pron.' => 'Pronoun',
16
        'interj.' => 'Interjection',
17
        'conj.' => 'Conjunction',
18
    ];
19
20
    private $table;
21
22 14
    public function __construct($string = null)
23
    {
24 14
        if ($string) {
25
            $this->string = $string;
26
        }
27 14
        $this->table = config('laravel-grammar.table', 'entries');
28 14
    }
29
30 1
    public function getPartsOfSpeech()
31
    {
32 1
        return array_values($this->partsOfSpeech);
33
    }
34
35 11
    public function getWordPartOfSpeech($string)
36
    {
37 11
        $this->string = $string;
38 11
        $row = \DB::table($this->table)->where('word', ucfirst($this->string))->first();
39
40 11
        return $row ? $this->parts($row->word_type) : [];
41
    }
42
43 11
    public function parts($string)
44
    {
45 11
        $array = explode(' ', str_replace(',', '', $string));
46
47 11
        $parts = [];
48
49 11
        foreach ($array as $item) {
50 11
            if (array_key_exists($item, $this->partsOfSpeech)) {
51 11
                $parts[] = $this->partsOfSpeech[$item];
52
            }
53
        }
54
55 11
        return $parts;
56
    }
57
58 9
    public function checkIs($part)
59
    {
60 9
        return in_array($part, $this->getWordPartOfSpeech($this->string));
61
    }
62
63 1
    public function isNoun()
64
    {
65 1
        return $this->checkIs('Noun');
66
    }
67
68 1
    public function isPronoun()
69
    {
70 1
        return $this->checkIs('Pronoun');
71
    }
72
73 1
    public function isAdjective()
74
    {
75 1
        return $this->checkIs('Adjective');
76
    }
77
78 1
    public function isAdverb()
79
    {
80 1
        return $this->checkIs('Adverb');
81
    }
82
83 1
    public function isPreposition()
84
    {
85 1
        return $this->checkIs('Preposition');
86
    }
87
88 1
    public function isConjunction()
89
    {
90 1
        return $this->checkIs('Conjunction');
91
    }
92
93 1
    public function isInterjection()
94
    {
95 1
        return $this->checkIs('Interjection');
96
    }
97
98 1
    public function isVerb()
99
    {
100 1
        return $this->checkIs('Verb');
101
    }
102
}
103