2016-05-11 01:55:04 +05:30
|
|
|
<?php
|
2018-01-07 21:38:40 +05:30
|
|
|
declare(strict_types=1);
|
2016-05-11 01:55:04 +05:30
|
|
|
namespace common\helpers;
|
|
|
|
|
2016-09-05 20:25:38 +05:30
|
|
|
use Ramsey\Uuid\Uuid;
|
|
|
|
|
2016-05-11 01:55:04 +05:30
|
|
|
class StringHelper {
|
|
|
|
|
2018-01-07 21:38:40 +05:30
|
|
|
public static function getEmailMask(string $email): string {
|
2016-05-11 01:55:04 +05:30
|
|
|
$username = explode('@', $email)[0];
|
|
|
|
$usernameLength = mb_strlen($username);
|
|
|
|
$maskChars = '**';
|
|
|
|
|
|
|
|
if ($usernameLength === 1) {
|
|
|
|
$mask = $maskChars;
|
2018-04-18 02:17:25 +05:30
|
|
|
} elseif ($usernameLength === 2) {
|
2016-05-11 01:55:04 +05:30
|
|
|
$mask = mb_substr($username, 0, 1) . $maskChars;
|
2018-04-18 02:17:25 +05:30
|
|
|
} elseif ($usernameLength === 3) {
|
2016-05-11 01:55:04 +05:30
|
|
|
$mask = mb_substr($username, 0, 1) . $maskChars . mb_substr($username, 2, 1);
|
|
|
|
} else {
|
|
|
|
$mask = mb_substr($username, 0, 2) . $maskChars . mb_substr($username, -2, 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $mask . mb_substr($email, $usernameLength);
|
|
|
|
}
|
|
|
|
|
2018-01-07 21:38:40 +05:30
|
|
|
public static function isUuid(string $uuid): bool {
|
2016-09-05 20:25:38 +05:30
|
|
|
try {
|
2024-12-02 15:40:55 +05:30
|
|
|
/** @throws \InvalidArgumentException */
|
|
|
|
Uuid::fromString($uuid); // @phpstan-ignore staticMethod.resultUnused (we don't care about the result, we need only an exception)
|
|
|
|
} catch (\InvalidArgumentException) {
|
2016-09-05 20:25:38 +05:30
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2016-08-04 03:37:21 +05:30
|
|
|
}
|
|
|
|
|
2018-01-07 21:38:40 +05:30
|
|
|
/**
|
|
|
|
* Returns a string with whitespace removed from the start and end of the
|
|
|
|
* string. Supports the removal of unicode whitespace.
|
|
|
|
*
|
|
|
|
* Based on the http://markushedlund.com/dev/trim-unicodeutf-8-whitespace-in-php
|
|
|
|
*
|
|
|
|
* @param string $string string to remove whitespaces
|
|
|
|
* @return string trimmed $string
|
|
|
|
*/
|
2018-01-09 03:53:11 +05:30
|
|
|
public static function trim(?string $string): string {
|
|
|
|
if ($string === null) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
2018-01-07 21:38:40 +05:30
|
|
|
return preg_replace('/^[\pZ\pC]+|[\pZ\pC]+$/u', '', $string);
|
|
|
|
}
|
|
|
|
|
2016-05-11 01:55:04 +05:30
|
|
|
}
|