-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
142 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php | ||
|
||
/* | ||
* Symfony Anti-Spam Bundle | ||
* (c) Omines Internetbureau B.V. - https://omines.nl/ | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Omines\AntiSpamBundle\Utility; | ||
|
||
class StringCounter | ||
{ | ||
/** @var array<string, int> */ | ||
private array $scores = []; | ||
|
||
public function add(string $string): void | ||
{ | ||
array_key_exists($string, $this->scores) ? $this->scores[$string]++ : ($this->scores[$string] = 1); | ||
} | ||
|
||
/** | ||
* @return array{string, int}[] | ||
*/ | ||
public function getScores(bool $sortByKey = true): array | ||
{ | ||
if ($sortByKey) { | ||
ksort($this->scores); | ||
} | ||
|
||
return array_map(fn ($k, $v) => [$k, $v], array_keys($this->scores), $this->scores); | ||
} | ||
|
||
/** | ||
* @return array{string, int}[] | ||
*/ | ||
public function getRanking(int $max = null): array | ||
{ | ||
arsort($this->scores, SORT_NUMERIC); | ||
|
||
$slice = array_slice($this->scores, 0, $max); | ||
|
||
return array_map(fn ($k, $v) => [$k, $v], array_keys($slice), $slice); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php | ||
|
||
/* | ||
* Symfony Anti-Spam Bundle | ||
* (c) Omines Internetbureau B.V. - https://omines.nl/ | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tests\Unit; | ||
|
||
use Omines\AntiSpamBundle\Utility\StringCounter; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class UtilityTest extends TestCase | ||
{ | ||
public function testStringCounter(): void | ||
{ | ||
$test = new StringCounter(); | ||
$test->add('foo'); | ||
$test->add('bar'); | ||
$test->add('bar'); | ||
$test->add('baz'); | ||
$test->add('baz'); | ||
$test->add('baz'); | ||
|
||
$unsorted = $test->getScores(false); | ||
$this->assertSame(['foo', 1], $unsorted[0]); | ||
|
||
$sorted = $test->getScores(); | ||
$this->assertSame(['bar', 2], $sorted[0]); | ||
|
||
$ranking = $test->getRanking(2); | ||
$this->assertCount(2, $ranking); | ||
$this->assertSame(['baz', 3], $ranking[0]); | ||
} | ||
} |