Passed
Push — master ( ca71fa...a18c2c )
by Sebastian
02:51
created

LipsumDetector::detect()   A

Complexity

Conditions 5
Paths 6

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 11
dl 0
loc 20
rs 9.6111
c 1
b 0
f 0
cc 5
nc 6
nop 0
1
<?php
2
/**
3
 * @package Application Utils
4
 * @subpackage LipsumHelper
5
 * @see \AppUtils\LipsumHelper\LipsumDetector
6
 */
7
8
declare(strict_types=1);
9
10
namespace AppUtils\LipsumHelper;
11
12
/**
13
 * Lipsum dummy text detection tool.
14
 *
15
 * @package Application Utils
16
 * @subpackage LipsumHelper
17
 * @author Sebastian Mordziol <[email protected]>
18
 */
19
class LipsumDetector
20
{
21
    private static array $words = array(
22
        'exercitationem',
23
        'reprehenderit',
24
        'perspiciatis',
25
        'exercitation',
26
        'consequuntur',
27
        'accusantium',
28
        'consectetur',
29
        'consequatur',
30
        'laudantium',
31
        'incididunt',
32
        'doloremque',
33
        'laboriosam',
34
        'voluptatem',
35
        'adipiscing',
36
        'aspernatur',
37
        'architecto',
38
        'cupidatat',
39
        'inventore',
40
        'explicabo',
41
        'Excepteur',
42
        'molestiae',
43
        'voluptate',
44
        'veritatis',
45
        'consequat',
46
        'proident',
47
        'adipisci',
48
        'incidunt',
49
        'deserunt',
50
        'quisquam',
51
        'nesciunt',
52
        'voluptas',
53
        'suscipit',
54
        'occaecat',
55
        'corporis',
56
        'pariatur',
57
        'aliquam',
58
        'laborum',
59
        'laboris',
60
        'numquam',
61
        'nostrum',
62
        'nostrud',
63
        'aliquid',
64
        'quaerat',
65
        'aperiam',
66
        'eiusmod',
67
        'dolores',
68
        'dolorem',
69
        'ullamco',
70
        'aliquip',
71
        'aliqua',
72
        'veniam',
73
        'cillum',
74
        'labore',
75
        'mollit',
76
        'beatae',
77
        'fugiat',
78
        'totam',
79
        'ullam',
80
        'velit',
81
        'dolor',
82
        'dicta',
83
        'culpa',
84
        'vitae',
85
        'autem',
86
        'sequi',
87
        'natus',
88
        'fugit',
89
        'Neque',
90
        'quasi',
91
        'ipsam',
92
        'ipsum',
93
        'porro',
94
        'irure',
95
        'Lorem',
96
        'magna',
97
        'magni',
98
        'minim',
99
        'nihil',
100
        'illum',
101
        'dolor',
102
        'sit',
103
        'amet',
104
        'elit'
105
    );
106
107
    private int $minWords = 2;
108
    private string $subject;
109
    private bool $detected = false;
110
    private int $count = 0;
111
112
    /**
113
     * @var string[]
114
     */
115
    private array $found = array();
116
117
    public function __construct(string $subject)
118
    {
119
        $this->subject = $subject;
120
    }
121
122
    public function setMinWords(int $min) : self
123
    {
124
        // Avoid a reset if the value is the same
125
        if($this->minWords === $min)
126
        {
127
            return $this;
128
        }
129
130
        $this->minWords = $min;
131
132
        $this->reset();
133
134
        return $this;
135
    }
136
137
    private function reset() : void
138
    {
139
        $this->found = array();
140
        $this->detected = false;
141
    }
142
143
    private function detect() : void
144
    {
145
        if($this->detected) {
146
            return;
147
        }
148
149
        $this->detected = true;
150
        $this->found = array();
151
        $this->count = 0;
152
153
        foreach(self::$words as $word)
154
        {
155
            if(stripos($this->subject, $word) !== false)
156
            {
157
                $this->count++;
158
                $this->found[] = $word;
159
            }
160
161
            if($this->count >= $this->minWords) {
162
                break;
163
            }
164
        }
165
    }
166
167
    public function isDetected() : bool
168
    {
169
        return $this->count >= $this->minWords;
170
    }
171
172
    public function getDetectedWords() : array
173
    {
174
        $this->detect();
175
176
        return $this->found;
177
    }
178
179
    public function countDetectedWords() : int
180
    {
181
        $this->detect();
182
183
        return $this->count;
184
    }
185
}
186