String convert class added
Currently with one method: splitFormatString Converts a string to a string with separater characters based on a format string
This commit is contained in:
142
4dev/tests/CoreLibsConvertStringsTest.php
Normal file
142
4dev/tests/CoreLibsConvertStringsTest.php
Normal file
@@ -0,0 +1,142 @@
|
|||||||
|
<?php // phpcs:disable Generic.Files.LineLength
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace tests;
|
||||||
|
|
||||||
|
use PHPUnit\Framework\TestCase;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test class for Convert\Strings
|
||||||
|
* @coversDefaultClass \CoreLibs\Convert\Strings
|
||||||
|
* @testdox \CoreLibs\Convert\Strings method tests
|
||||||
|
*/
|
||||||
|
final class CoreLibsConvertStringsTest extends TestCase
|
||||||
|
{
|
||||||
|
public function splitFormatStringProvider(): array
|
||||||
|
{
|
||||||
|
// 0: input
|
||||||
|
// 1: format
|
||||||
|
// 2: split characters as string, null for default
|
||||||
|
// 3: expected
|
||||||
|
return [
|
||||||
|
'all empty string' => [
|
||||||
|
'',
|
||||||
|
'',
|
||||||
|
null,
|
||||||
|
''
|
||||||
|
],
|
||||||
|
'empty input string' => [
|
||||||
|
'',
|
||||||
|
'2-2',
|
||||||
|
null,
|
||||||
|
''
|
||||||
|
],
|
||||||
|
'empty format string string' => [
|
||||||
|
'1234',
|
||||||
|
'',
|
||||||
|
null,
|
||||||
|
'1234'
|
||||||
|
],
|
||||||
|
'string format match' => [
|
||||||
|
'1234',
|
||||||
|
'2-2',
|
||||||
|
null,
|
||||||
|
'12-34'
|
||||||
|
],
|
||||||
|
'string format short first' => [
|
||||||
|
'1',
|
||||||
|
'2-2',
|
||||||
|
null,
|
||||||
|
'1'
|
||||||
|
],
|
||||||
|
'string format match first' => [
|
||||||
|
'12',
|
||||||
|
'2-2',
|
||||||
|
null,
|
||||||
|
'12'
|
||||||
|
],
|
||||||
|
'string format short second' => [
|
||||||
|
'123',
|
||||||
|
'2-2',
|
||||||
|
null,
|
||||||
|
'12-3'
|
||||||
|
],
|
||||||
|
'string format too long' => [
|
||||||
|
'1234567',
|
||||||
|
'2-2',
|
||||||
|
null,
|
||||||
|
'12-34-567'
|
||||||
|
],
|
||||||
|
'string format invalid format string' => [
|
||||||
|
'1234',
|
||||||
|
'2_2',
|
||||||
|
null,
|
||||||
|
'1234'
|
||||||
|
],
|
||||||
|
'different split character' => [
|
||||||
|
'1234',
|
||||||
|
'2_2',
|
||||||
|
'_',
|
||||||
|
'12_34'
|
||||||
|
],
|
||||||
|
'mixed split characters' => [
|
||||||
|
'123456',
|
||||||
|
'2-2_2',
|
||||||
|
'-_',
|
||||||
|
'12-34_56'
|
||||||
|
],
|
||||||
|
'length mixed' => [
|
||||||
|
'ABCD12345568ABC13',
|
||||||
|
'2-4_5-2#4',
|
||||||
|
'-_#',
|
||||||
|
'AB-CD12_34556-8A#BC13'
|
||||||
|
],
|
||||||
|
'split with split chars in string' => [
|
||||||
|
'12-34',
|
||||||
|
'2-2',
|
||||||
|
null,
|
||||||
|
'12--3-4'
|
||||||
|
]
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* split format string
|
||||||
|
*
|
||||||
|
* @covers ::splitFormatString
|
||||||
|
* @dataProvider splitFormatStringProvider
|
||||||
|
* @testdox splitFormatString $input with format $format and splitters $split_characters will be $expected [$_dataName]
|
||||||
|
*
|
||||||
|
* @param string $input
|
||||||
|
* @param string $format
|
||||||
|
* @param string|null $split_characters
|
||||||
|
* @param string $expected
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function testSplitFormatString(
|
||||||
|
string $input,
|
||||||
|
string $format,
|
||||||
|
?string $split_characters,
|
||||||
|
string $expected
|
||||||
|
): void {
|
||||||
|
if ($split_characters === null) {
|
||||||
|
$output = \CoreLibs\Convert\Strings::splitFormatString(
|
||||||
|
$input,
|
||||||
|
$format
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
$output = \CoreLibs\Convert\Strings::splitFormatString(
|
||||||
|
$input,
|
||||||
|
$format,
|
||||||
|
$split_characters
|
||||||
|
);
|
||||||
|
}
|
||||||
|
$this->assertEquals(
|
||||||
|
$expected,
|
||||||
|
$output
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// __END__
|
||||||
@@ -70,6 +70,7 @@ print '<div><a href="class_test.hash.php">Class Test: HASH</a></div>';
|
|||||||
print '<div><a href="class_test.encoding.php">Class Test: ENCODING (CHECK/CONVERT/MIME)</a></div>';
|
print '<div><a href="class_test.encoding.php">Class Test: ENCODING (CHECK/CONVERT/MIME)</a></div>';
|
||||||
print '<div><a href="class_test.image.php">Class Test: IMAGE</a></div>';
|
print '<div><a href="class_test.image.php">Class Test: IMAGE</a></div>';
|
||||||
print '<div><a href="class_test.byte.php">Class Test: BYTE CONVERT</a></div>';
|
print '<div><a href="class_test.byte.php">Class Test: BYTE CONVERT</a></div>';
|
||||||
|
print '<div><a href="class_test.strings.php">Class Test: STRING CONVERT</a></div>';
|
||||||
print '<div><a href="class_test.datetime.php">Class Test: DATE/TIME</a></div>';
|
print '<div><a href="class_test.datetime.php">Class Test: DATE/TIME</a></div>';
|
||||||
print '<div><a href="class_test.array.php">Class Test: ARRAY HANDLER</a></div>';
|
print '<div><a href="class_test.array.php">Class Test: ARRAY HANDLER</a></div>';
|
||||||
print '<div><a href="class_test.file.php">Class Test: FILE</a></div>';
|
print '<div><a href="class_test.file.php">Class Test: FILE</a></div>';
|
||||||
|
|||||||
73
www/admin/class_test.strings.php
Normal file
73
www/admin/class_test.strings.php
Normal file
@@ -0,0 +1,73 @@
|
|||||||
|
<?php // phpcs:ignore warning
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
$DEBUG_ALL_OVERRIDE = 0; // set to 1 to debug on live/remote server locations
|
||||||
|
$DEBUG_ALL = 1;
|
||||||
|
$PRINT_ALL = 1;
|
||||||
|
$DB_DEBUG = 1;
|
||||||
|
|
||||||
|
if ($DEBUG_ALL) {
|
||||||
|
error_reporting(E_ALL | E_STRICT | E_ERROR | E_WARNING | E_PARSE | E_COMPILE_ERROR);
|
||||||
|
}
|
||||||
|
|
||||||
|
ob_start();
|
||||||
|
|
||||||
|
// basic class test file
|
||||||
|
define('USE_DATABASE', false);
|
||||||
|
// sample config
|
||||||
|
require 'config.php';
|
||||||
|
// define log file id
|
||||||
|
$LOG_FILE_ID = 'classTest-string';
|
||||||
|
ob_end_flush();
|
||||||
|
|
||||||
|
use CoreLibs\Convert\Byte;
|
||||||
|
|
||||||
|
$log = new CoreLibs\Debug\Logging([
|
||||||
|
'log_folder' => BASE . LOG,
|
||||||
|
'file_id' => $LOG_FILE_ID,
|
||||||
|
// add file date
|
||||||
|
'print_file_date' => true,
|
||||||
|
// set debug and print flags
|
||||||
|
'debug_all' => $DEBUG_ALL ?? false,
|
||||||
|
'echo_all' => $ECHO_ALL ?? false,
|
||||||
|
'print_all' => $PRINT_ALL ?? false,
|
||||||
|
]);
|
||||||
|
$byte_class = 'CoreLibs\Convert\Strings';
|
||||||
|
|
||||||
|
$PAGE_NAME = 'TEST CLASS: STRINGS CONVERT';
|
||||||
|
print "<!DOCTYPE html>";
|
||||||
|
print "<html><head><title>" . $PAGE_NAME . "</title><head>";
|
||||||
|
print "<body>";
|
||||||
|
print '<div><a href="class_test.php">Class Test Master</a></div>';
|
||||||
|
print '<div><h1>' . $PAGE_NAME . '</h1></div>';
|
||||||
|
|
||||||
|
$split = '4-4-4';
|
||||||
|
$test_strings = [
|
||||||
|
'13',
|
||||||
|
'1234',
|
||||||
|
'12341',
|
||||||
|
'12341234',
|
||||||
|
'123412341',
|
||||||
|
'123412341234',
|
||||||
|
'1234123412341234512345',
|
||||||
|
];
|
||||||
|
|
||||||
|
foreach ($test_strings as $string) {
|
||||||
|
print "Convert: $string with $split to: "
|
||||||
|
. \CoreLibs\Convert\Strings::splitFormatString($string, $split)
|
||||||
|
. "<br>";
|
||||||
|
}
|
||||||
|
|
||||||
|
$split = '2_2';
|
||||||
|
$string = '1234';
|
||||||
|
print "Convert: $string with $split to: "
|
||||||
|
. \CoreLibs\Convert\Strings::splitFormatString($string, $split)
|
||||||
|
. "<br>";
|
||||||
|
|
||||||
|
// error message
|
||||||
|
print $log->printErrorMsg();
|
||||||
|
|
||||||
|
print "</body></html>";
|
||||||
|
|
||||||
|
// __END__
|
||||||
81
www/lib/CoreLibs/Convert/Strings.php
Normal file
81
www/lib/CoreLibs/Convert/Strings.php
Normal file
@@ -0,0 +1,81 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/*
|
||||||
|
* string convert and transform functions
|
||||||
|
*/
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace CoreLibs\Convert;
|
||||||
|
|
||||||
|
class Strings
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* split format a string base on a split format string
|
||||||
|
* split format string is eg
|
||||||
|
* 4-4-4 that means 4 characters DASH 4 characters DASH 4 characters
|
||||||
|
* So a string in the format of
|
||||||
|
* ABCD1234EFGH will be ABCD-1234-EFGH
|
||||||
|
* Note a string LONGER then the maxium will be attached with the LAST
|
||||||
|
* split character. In above exmaple
|
||||||
|
* ABCD1234EFGHTOOLONG will be ABCD-1234-EFGH-TOOLONG
|
||||||
|
*
|
||||||
|
* @param string $value string value to split
|
||||||
|
* @param string $split_format split format
|
||||||
|
* @param string $split_characters list of charcters with which we split
|
||||||
|
* if not set uses dash ('-')
|
||||||
|
* @return string split formatted string or original value if not chnaged
|
||||||
|
*/
|
||||||
|
public static function splitFormatString(
|
||||||
|
string $value,
|
||||||
|
string $split_format,
|
||||||
|
string $split_charcters = '-'
|
||||||
|
): string {
|
||||||
|
// abort if split format is empty
|
||||||
|
if (empty($split_format)) {
|
||||||
|
return $value;
|
||||||
|
}
|
||||||
|
$split_list = preg_split(
|
||||||
|
// allowed split characters
|
||||||
|
"/([" . $split_charcters . "]{1})/",
|
||||||
|
$split_format,
|
||||||
|
-1,
|
||||||
|
PREG_SPLIT_DELIM_CAPTURE
|
||||||
|
);
|
||||||
|
// if this is false, or only one array, abort split
|
||||||
|
if (!is_array($split_list) || count($split_list) == 1) {
|
||||||
|
return $value;
|
||||||
|
}
|
||||||
|
$out = '';
|
||||||
|
$pos = 0;
|
||||||
|
$last_split = '';
|
||||||
|
foreach ($split_list as $offset) {
|
||||||
|
if (is_numeric($offset)) {
|
||||||
|
$_part = substr($value, $pos, (int)$offset);
|
||||||
|
if (empty($_part)) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
$out .= $_part;
|
||||||
|
$pos += (int)$offset;
|
||||||
|
} else {
|
||||||
|
$out .= $offset;
|
||||||
|
$last_split = $offset;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!empty($out) && $pos < strlen($value)) {
|
||||||
|
$out .= $last_split . substr($value, $pos);
|
||||||
|
}
|
||||||
|
// if last is not alphanumeric remove, remove
|
||||||
|
if (!strcspn(substr($out, -1, 1), $split_charcters)) {
|
||||||
|
$out = substr($out, 0, -1);
|
||||||
|
}
|
||||||
|
// overwrite only if out is set
|
||||||
|
if (!empty($out)) {
|
||||||
|
return $out;
|
||||||
|
} else {
|
||||||
|
return $value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// __END__
|
||||||
1
www/vendor/composer/autoload_classmap.php
vendored
1
www/vendor/composer/autoload_classmap.php
vendored
@@ -27,6 +27,7 @@ return array(
|
|||||||
'CoreLibs\\Convert\\Math' => $baseDir . '/lib/CoreLibs/Convert/Math.php',
|
'CoreLibs\\Convert\\Math' => $baseDir . '/lib/CoreLibs/Convert/Math.php',
|
||||||
'CoreLibs\\Convert\\MimeAppName' => $baseDir . '/lib/CoreLibs/Convert/MimeAppName.php',
|
'CoreLibs\\Convert\\MimeAppName' => $baseDir . '/lib/CoreLibs/Convert/MimeAppName.php',
|
||||||
'CoreLibs\\Convert\\MimeEncode' => $baseDir . '/lib/CoreLibs/Convert/MimeEncode.php',
|
'CoreLibs\\Convert\\MimeEncode' => $baseDir . '/lib/CoreLibs/Convert/MimeEncode.php',
|
||||||
|
'CoreLibs\\Convert\\Strings' => $baseDir . '/lib/CoreLibs/Convert/Strings.php',
|
||||||
'CoreLibs\\Create\\Email' => $baseDir . '/lib/CoreLibs/Create/Email.php',
|
'CoreLibs\\Create\\Email' => $baseDir . '/lib/CoreLibs/Create/Email.php',
|
||||||
'CoreLibs\\Create\\Hash' => $baseDir . '/lib/CoreLibs/Create/Hash.php',
|
'CoreLibs\\Create\\Hash' => $baseDir . '/lib/CoreLibs/Create/Hash.php',
|
||||||
'CoreLibs\\Create\\RandomKey' => $baseDir . '/lib/CoreLibs/Create/RandomKey.php',
|
'CoreLibs\\Create\\RandomKey' => $baseDir . '/lib/CoreLibs/Create/RandomKey.php',
|
||||||
|
|||||||
1
www/vendor/composer/autoload_static.php
vendored
1
www/vendor/composer/autoload_static.php
vendored
@@ -92,6 +92,7 @@ class ComposerStaticInit10fe8fe2ec4017b8644d2b64bcf398b9
|
|||||||
'CoreLibs\\Convert\\Math' => __DIR__ . '/../..' . '/lib/CoreLibs/Convert/Math.php',
|
'CoreLibs\\Convert\\Math' => __DIR__ . '/../..' . '/lib/CoreLibs/Convert/Math.php',
|
||||||
'CoreLibs\\Convert\\MimeAppName' => __DIR__ . '/../..' . '/lib/CoreLibs/Convert/MimeAppName.php',
|
'CoreLibs\\Convert\\MimeAppName' => __DIR__ . '/../..' . '/lib/CoreLibs/Convert/MimeAppName.php',
|
||||||
'CoreLibs\\Convert\\MimeEncode' => __DIR__ . '/../..' . '/lib/CoreLibs/Convert/MimeEncode.php',
|
'CoreLibs\\Convert\\MimeEncode' => __DIR__ . '/../..' . '/lib/CoreLibs/Convert/MimeEncode.php',
|
||||||
|
'CoreLibs\\Convert\\Strings' => __DIR__ . '/../..' . '/lib/CoreLibs/Convert/Strings.php',
|
||||||
'CoreLibs\\Create\\Email' => __DIR__ . '/../..' . '/lib/CoreLibs/Create/Email.php',
|
'CoreLibs\\Create\\Email' => __DIR__ . '/../..' . '/lib/CoreLibs/Create/Email.php',
|
||||||
'CoreLibs\\Create\\Hash' => __DIR__ . '/../..' . '/lib/CoreLibs/Create/Hash.php',
|
'CoreLibs\\Create\\Hash' => __DIR__ . '/../..' . '/lib/CoreLibs/Create/Hash.php',
|
||||||
'CoreLibs\\Create\\RandomKey' => __DIR__ . '/../..' . '/lib/CoreLibs/Create/RandomKey.php',
|
'CoreLibs\\Create\\RandomKey' => __DIR__ . '/../..' . '/lib/CoreLibs/Create/RandomKey.php',
|
||||||
|
|||||||
Reference in New Issue
Block a user