Skip to content

Commit

Permalink
optimize code
Browse files Browse the repository at this point in the history
  • Loading branch information
Leonid74 committed Feb 12, 2024
1 parent 0659650 commit 71c832f
Show file tree
Hide file tree
Showing 5 changed files with 209 additions and 119 deletions.
10 changes: 5 additions & 5 deletions src/EmailHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,23 @@ class EmailHelper
*
* Проверка валидности email адреса.
*
* @param string $sString
* @param string|null $string
*
* @return bool
*/
public static function isEmailValid(?string $sString = ''): bool
public static function isEmailValid(?string $string = ''): bool
{
if ('' === $sString || \is_null($sString)) {
if (null === $string || '' === $string) {
return false;
}

// According to the FQDN add a dot to the end of the string and check if the domain has an mx record
if (\function_exists('filter_var') && \filter_var($sString, FILTER_VALIDATE_EMAIL) && \checkdnsrr(\ltrim(\stristr($sString, '@'), '@') . '.', 'MX')) {
if (\function_exists('filter_var') && \filter_var($string, FILTER_VALIDATE_EMAIL) && \checkdnsrr(\ltrim(\stristr($string, '@'), '@') . '.', 'MX')) {
return true;
}

// Сheck using a regular expression if the filter_var() function does not exist
if (\preg_match('/^[a-zA-Z0-9.!#$%&\'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/sD', $sString) && \preg_match('/@.+\./', $sString) && \checkdnsrr(\ltrim(\stristr($sString, '@'), '@') . '.', 'MX')) {
if (\preg_match('/^[a-zA-Z0-9.!#$%&\'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/sD', $string) && \preg_match('/@.+\./', $string) && \checkdnsrr(\ltrim(\stristr($string, '@'), '@') . '.', 'MX')) {
return true;
}

Expand Down
36 changes: 32 additions & 4 deletions src/FileHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ class FileHelper
* Gets name of the filename only (without dot and extension).
* If the file name contains several dots (file.name.ext) - return all parts before the last dot.
*
* @param string $sFilename
* @param string|null $sFilename
* @param string $default
*
* @return string
*/
public static function getFileNameOnly(?string $sFilename = '', string $default = ''): string
{
if ('' === $sFilename || \is_null($sFilename)) {
if (null === $sFilename || '' === $sFilename) {
return $default;
}

Expand All @@ -41,19 +41,47 @@ public static function getFileNameOnly(?string $sFilename = '', string $default
/**
* Gets filename extension only (without dot and name)
*
* @param string $sFilename
* @param string|null $sFilename
* @param string $default
*
* @return string
*/
public static function getFileExtOnly(?string $sFilename = '', string $default = ''): string
{
if ('' === $sFilename || \is_null($sFilename)) {
if (null === $sFilename || '' === $sFilename) {
return $default;
}

$sParsed = \pathinfo(\trim($sFilename), PATHINFO_EXTENSION);

return $sParsed ? $sParsed : $default;
}

/**
* Converts file size in bytes into human readable file size.
*
* Конвертация размера файла в удобный для чтения формат.
*
* @param mixed|null $bytes
* @param int $decimals
*
* @return string human readable file size (2.87 MB)
*
* @author evgenij at kostanay dot kz
*
* @edit Leonid Sheikman <[email protected]>
*/
public static function getHumanFileSize(?mixed $bytes, int $decimals = 2): string
{
if (null === $bytes) {
return '0';
}

$factor = \floor((\strlen((string) $bytes) - 1) / 3);
if ($factor > 0) {
$sz = 'KMGT';
}

return \sprintf("%.{$decimals}f", (float) $bytes / \pow(1024, $factor)) . ' ' . @$sz[$factor - 1] . 'B';
}
}
26 changes: 13 additions & 13 deletions src/PhoneHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,54 +57,54 @@ public static function formatRussianMobilePhoneNumber(?string $phoneNumber): ?st
* Функция принимает телефонный номер в произвольном формате и возвращает телефонный
* номер в формате +7xxxxxxxxxx или false, если входной номер телефона не проходит проверку валидности.
*
* @param string $sString
* @param string|null $string
*
* @return bool|string
*
* @author kirkizh.ru
*
* @edit Leonid Sheikman (leonid74)
*/
public static function getValidRusPhone(?string $sString = '')
public static function getValidRusPhone(?string $string = '')
{
if ('' === $sString || \is_null($sString)) {
if (null === $string || '' === $string) {
return false;
}

$sString = \preg_replace('#[^0-9+]+#uis', '', \trim((string) $sString));
$string = \preg_replace('#[^0-9+]+#uis', '', \trim((string) $string));

if (!\preg_match('#^(?:\\+?7|8|)(.*?)$#uis', $sString, $aTmp)) {
if (!\preg_match('#^(?:\\+?7|8|)(.*?)$#uis', $string, $aTmp)) {
return false;
}
$sString = '+7' . \preg_replace('#[^0-9]+#uis', '', $aTmp[1]);
$string = '+7' . \preg_replace('#[^0-9]+#uis', '', $aTmp[1]);

if (!\preg_match('#^\\+7[0-9]{10}$#uis', $sString, $aTmp)) {
if (!\preg_match('#^\\+7[0-9]{10}$#uis', $string, $aTmp)) {
return false;
}
return $sString;
return $string;
}
/**
* Checking the phone number for existence through the sms.ru
*
* Проверка номера телефона на существование через sms.ru
*
* @param string $sString
* @param string $string
* @param string $apiToken
* @param string $apiSmsId API Key to SMS.RU
*
* @return stdClass()
*/
public static function isPhoneExist(string $sString, string $apiToken)
public static function isPhoneExist(string $string, string $apiToken)
{
$oResult = new \stdClass();
$oResult->phone = $sString;
$oResult->phone = $string;
$oResult->phone_exists = 'unknown';
$oResult->is_error = false;

if (empty($sString)) {
if (empty($string)) {
$oResult->phone_exists = false;

return $oResult;
Expand All @@ -115,7 +115,7 @@ public static function isPhoneExist(string $sString, string $apiToken)
$i = 1;
do {
$json = \json_decode(
\file_get_contents("https://sms.ru/sms/cost?api_id={$apiToken}&to={$sString}&msg=" . \urlencode('Test' . \microtime(true)) . '&json=1')
\file_get_contents("https://sms.ru/sms/cost?api_id={$apiToken}&to={$string}&msg=" . \urlencode('Test' . \microtime(true)) . '&json=1')
);

if ((\json_last_error() === JSON_ERROR_NONE) && $json) {
Expand Down
Loading

0 comments on commit 71c832f

Please sign in to comment.