Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions word-count.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php
class Phrase
{
protected $phrase;
protected $wordCount = [];
public function __construct($phrase) {
$this->phrase = $phrase;
$this->setWordCount();
}
public function wordCount() {
return $this->wordCount;
}
protected function setWordCount() {
$this->wordCount = PhraseUtils::getCount($this->phrase);
}
}
final class PhraseUtils
{
public static function lower($phrase) {
return strtolower($phrase);
}
public static function normalize($phrase) {
return trim(preg_replace( "/[^0-9a-z]+/i", " ", $phrase));
}
public static function toArray($normalizedPhrase) {
return explode(' ', $normalizedPhrase);
}
public static function getCount($phrase) {
$wordCount = [];
$words = self::toArray(self::normalize(self::lower($phrase)));
foreach($words as $word) {
if (!in_array($word, array_keys($wordCount))) {
$wordCount[$word] = 0;
}
$wordCount[$word]++;
}
return $wordCount;
}
}
function wordCount($phrase) {
$phraseInstance = new Phrase($phrase);
return $phraseInstance->wordCount();
}