Add clsas for MD_STD_STRINGS more obscure string operations
This commit is contained in:
parent
be437fcf5f
commit
9948ee5d3d
0
phpstan-baseline.neon
Normal file
0
phpstan-baseline.neon
Normal file
12
phpstan.neon
Normal file
12
phpstan.neon
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
parameters:
|
||||||
|
level: 8
|
||||||
|
bootstrapFiles:
|
||||||
|
- ./tests/bootstrap.php
|
||||||
|
paths:
|
||||||
|
- src
|
||||||
|
- tests
|
||||||
|
dynamicConstantNames:
|
||||||
|
- DATABASENAME
|
||||||
|
- DATABASENAME_NODA
|
||||||
|
includes:
|
||||||
|
- phpstan-baseline.neon
|
39
src/MD_STD_STRINGS.php
Normal file
39
src/MD_STD_STRINGS.php
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
<?PHP
|
||||||
|
/**
|
||||||
|
* Gathers wrappers for handling strings.
|
||||||
|
*/
|
||||||
|
declare(strict_types = 1);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Encapsulates functions for handling strings.
|
||||||
|
*/
|
||||||
|
final class MD_STD_STRINGS {
|
||||||
|
/**
|
||||||
|
* Duplicates words ending in a given set of strings (e.g. dots) that serve as bind chars
|
||||||
|
* to allow indexing to then allow searching for them in either form.
|
||||||
|
*
|
||||||
|
* @param string $input Input string.
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public static function duplicate_words_with_dots_for_indexing(string $input):string {
|
||||||
|
|
||||||
|
$charsToDuplicateOn = ',.!-';
|
||||||
|
|
||||||
|
$wordsToAdd = [];
|
||||||
|
$words = explode(' ', $input);
|
||||||
|
foreach ($words as $word) {
|
||||||
|
$trimmed = trim($word, $charsToDuplicateOn);
|
||||||
|
if ($trimmed !== $word) {
|
||||||
|
$wordsToAdd[] = $trimmed;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (empty($wordsToAdd)) {
|
||||||
|
return $input;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $input . ' ' . implode(' ', $wordsToAdd);
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
@ -62,7 +62,7 @@ final class MD_STD_TEST_PROVIDERS {
|
|||||||
/**
|
/**
|
||||||
* Data provider for working mail addresses.
|
* Data provider for working mail addresses.
|
||||||
*
|
*
|
||||||
* @return array<array{0: string, 1: string}>
|
* @return array<array{0: string}>
|
||||||
*/
|
*/
|
||||||
public static function invalid_email_provider():array {
|
public static function invalid_email_provider():array {
|
||||||
|
|
||||||
|
53
tests/MD_STD_STRINGS_Test.php
Normal file
53
tests/MD_STD_STRINGS_Test.php
Normal file
@ -0,0 +1,53 @@
|
|||||||
|
<?PHP
|
||||||
|
/**
|
||||||
|
* Tests for MD_STD_IN.
|
||||||
|
*
|
||||||
|
* @author Joshua Ramon Enslin <joshua@museum-digital.de>
|
||||||
|
*/
|
||||||
|
declare(strict_types = 1);
|
||||||
|
|
||||||
|
use PHPUnit\Framework\TestCase;
|
||||||
|
use PHPUnit\Framework\Attributes\DataProvider;
|
||||||
|
use PHPUnit\Framework\Attributes\Small;
|
||||||
|
use PHPUnit\Framework\Attributes\CoversClass;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tests for MD_STD_STRINGS.
|
||||||
|
*/
|
||||||
|
#[small]
|
||||||
|
#[CoversClass(\MD_STD_STRINGS::class)]
|
||||||
|
final class MD_STD_STRINGS_Test extends TestCase {
|
||||||
|
/**
|
||||||
|
* Data provider for strings to duplicate words with dots in.
|
||||||
|
*
|
||||||
|
* @return array<array{0: string, 1: string}>
|
||||||
|
*/
|
||||||
|
public static function duplicated_words_with_dots_provider():array {
|
||||||
|
|
||||||
|
$values = [
|
||||||
|
["hallo test. hallo", "hallo test. hallo test"],
|
||||||
|
];
|
||||||
|
|
||||||
|
$output = [];
|
||||||
|
foreach ($values as $value) {
|
||||||
|
$output[$value[0] . ' > ' . $value[1]] = $value;
|
||||||
|
}
|
||||||
|
return $output;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Function for testing duplicate_words_with_dots_for_indexing().
|
||||||
|
*
|
||||||
|
* @param string $input Input.
|
||||||
|
* @param string $expected Expected output.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
#[DataProvider('duplicated_words_with_dots_provider')]
|
||||||
|
public function test_duplicate_words_with_dots_for_indexing(string $input, string $expected):void {
|
||||||
|
|
||||||
|
self::assertEquals($expected, MD_STD_STRINGS::duplicate_words_with_dots_for_indexing($input));
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
@ -739,6 +739,7 @@ final class MD_STD_Test extends TestCase {
|
|||||||
self::assertEmpty(MD_STD::strpos_multi($haystack, $needles));
|
self::assertEmpty(MD_STD::strpos_multi($haystack, $needles));
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
self::assertNotEmpty(MD_STD::strpos_multi($haystack, $needles));
|
||||||
self::assertEquals($expected, MD_STD::strpos_multi($haystack, $needles)['position']);
|
self::assertEquals($expected, MD_STD::strpos_multi($haystack, $needles)['position']);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user