Skip to content

Commit b702cc3

Browse files
author
Fredrick Peter
committed
update
1 parent 2829604 commit b702cc3

File tree

5 files changed

+110
-215
lines changed

5 files changed

+110
-215
lines changed

Asset.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,16 @@ static public function config(?string $base_path = null, ?bool $cache = false)
8686
// - Trim forward slash from left and right
8787
$base_path = trim($base_path, '/');
8888

89+
// base for url path
90+
$baseForUrlPath = $base_path;
91+
92+
// check if accessed from default ip:address
93+
if(UrlHelper::isIpAccessedVia127Port()){
94+
$baseForUrlPath = '';
95+
}
96+
8997
// compile
90-
$urlFromhelper = "{$urlFromhelper}/{$base_path}";
98+
$urlFromhelper = "{$urlFromhelper}/{$baseForUrlPath}";
9199
}
92100

93101
define('ASSET_BASE_DIRECTORY', [

Str.php

Lines changed: 50 additions & 166 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
namespace Tamedevelopers\Support;
66

77
use Tamedevelopers\Support\Server;
8-
use Tamedevelopers\Support\Slugify;
98

109
class Str{
1110

@@ -57,25 +56,19 @@ static public function last($array = null)
5756
* @param string $fromKey
5857
* @param string $toKey
5958
* @return array
60-
* - Returns an associative array
6159
*/
6260
static public function changeKeysFromArray($data, $fromKey, $toKey)
6361
{
6462
// always convert to an array
6563
$data = Server::toArray($data);
66-
67-
// If $data is an associative array, convert it to a numeric array
68-
if (self::isAssoc($data)) {
69-
$data = [$data];
70-
}
7164

7265
// If you don't want to modify the original array and create a new one without 'id' columns:
73-
return array_map(function($item) use($fromKey, $toKey) {
74-
if (isset($item[$fromKey])) {
75-
$item[$toKey] = $item[$fromKey];
76-
unset($item[$fromKey]);
66+
return array_map(function($data) use($fromKey, $toKey) {
67+
if (isset($data[$fromKey])) {
68+
$data[$toKey] = $data[$fromKey];
69+
unset($data[$fromKey]);
7770
}
78-
return $item;
71+
return $data;
7972
}, $data);
8073
}
8174

@@ -85,27 +78,21 @@ static public function changeKeysFromArray($data, $fromKey, $toKey)
8578
* @param array $data
8679
* @param mixed $keys
8780
* @return array
88-
* - Returns an associative array
8981
*/
9082
static public function removeKeysFromArray($data, ...$keys)
9183
{
9284
// always convert to an array
9385
$data = Server::toArray($data);
94-
95-
// If $data is an associative array, convert it to a numeric array
96-
if (self::isAssoc($data)) {
97-
$data = [$data];
98-
}
9986

10087
// If you don't want to modify the original array and create a new one without 'id' columns:
101-
return array_map(function($items) use($keys) {
88+
return array_map(function($data) use($keys) {
10289
$keys = self::flattenValue($keys);
10390
foreach($keys as $key){
104-
if(isset($items[$key])){
105-
unset($items[$key]);
91+
if(isset($data[$key])){
92+
unset($data[$key]);
10693
}
10794
}
108-
return $items;
95+
return $data;
10996
}, $data);
11097
}
11198

@@ -336,33 +323,6 @@ static public function endsWith(string $haystack, string $needle)
336323
return substr($haystack, -strlen($needle)) === $needle;
337324
}
338325

339-
/**
340-
* Generate a string with a specified number of random words.
341-
*
342-
* @param int $wordCount
343-
* @param int $minLength
344-
* @param int $maxLength
345-
* @return string
346-
*/
347-
static public function randomWords(int $wordCount, int $minLength = 3, int $maxLength = 10)
348-
{
349-
$words = [];
350-
$characters = 'abcdefghijklmnopqrstuvwxyz';
351-
352-
for ($i = 0; $i < $wordCount; $i++) {
353-
$length = rand($minLength, $maxLength);
354-
$word = '';
355-
356-
for ($j = 0; $j < $length; $j++) {
357-
$word .= $characters[rand(0, strlen($characters) - 1)];
358-
}
359-
360-
$words[] = $word;
361-
}
362-
363-
return implode(' ', $words);
364-
}
365-
366326
/**
367327
* Generate a random string of a given length.
368328
*
@@ -496,118 +456,26 @@ static public function title(string $value)
496456
* Convert a string to a URL-friendly slug.
497457
*
498458
* @param string $value
499-
* @param string $language
500459
* @param string $separator
501-
* @param bool $case
502-
* @return string
503-
*/
504-
static public function slugify(string $value, $language = null, string $separator = '-', $case = true)
505-
{
506-
return Slugify::slug(
507-
$value, $language, $separator, $case
508-
);
509-
}
510-
511-
/**
512-
* Masks characters in a string.
513-
*
514-
* @param string|null $str
515-
* - The string to be masked.
516-
*
517-
* @param int $length
518-
* - The desired length of the masked string. Default is 4.
519-
*
520-
* @param string|null $position
521-
* - The position to apply the mask: 'left', 'middle' or 'center', 'right'. Default is 'right'.
522-
*
523-
* @param string $mask
524-
* - The character used for masking. Default is '*'.
525-
*
526-
* @return string
527-
* - The masked string.
528-
*/
529-
static public function mask($str = null, ?int $length = 4, ?string $position = null, ?string $mask = '*')
530-
{
531-
return Tame::mask(
532-
$str, $length, $position, $mask
533-
);
534-
}
535-
536-
/**
537-
* Shorten String to Given Limit
538-
*
539-
* @param mixed $string
540-
*
541-
* @param mixed $limit
542-
* [optional] Default 50
543-
*
544-
* @param mixed $replacer
545-
* [optional] Default ...
546-
*
547-
* @return string
548-
*/
549-
static public function shorten($string = null, $limit = 50, $replacer = '...')
550-
{
551-
return Tame::shortenString(
552-
$string, $limit, $replacer
553-
);
554-
}
555-
556-
/**
557-
* Decode entity html strings
558-
*
559-
* @param string|null $string
560460
* @return string
561461
*/
562-
static public function html($string = null)
462+
static public function slugify(string $value, string $separator = '-')
563463
{
564-
return Tame::html($string);
565-
}
464+
// Try to transliterate using intl extension
465+
if (function_exists('transliterator_transliterate')) {
466+
$value = transliterator_transliterate('Any-Latin; Latin-ASCII; Lower()', $value);
467+
} else {
468+
// Fallback to iconv for transliteration if intl is not available
469+
$value = iconv('UTF-8', 'ASCII//TRANSLIT//IGNORE', $value);
470+
}
566471

567-
/**
568-
* Convert string to clean text without html tags
569-
*
570-
* @param string|null $string
571-
*
572-
* @return string
573-
* - strip all tags from string content
574-
*/
575-
static public function text($string = null)
576-
{
577-
return Tame::text($string);
578-
}
472+
// Replace non-alphanumeric characters with the separator
473+
$value = preg_replace('/[^a-z0-9-]+/', $separator, $value);
579474

580-
/**
581-
* Hash String
582-
*
583-
* @param string|null $string
584-
* @param int $length
585-
* @param string $type
586-
* @param int $interation
587-
* @return void
588-
*/
589-
static public function hash($string = null, $length = 100, $type = 'sha256', $interation = 100)
590-
{
591-
return Tame::stringHash(
592-
$string, $length, $type, $interation
593-
);
594-
}
475+
// Remove leading and trailing separators
476+
$value = trim($value, $separator);
595477

596-
/**
597-
* Clean phone string
598-
*
599-
* @param string|null $phone
600-
*
601-
* @param bool $allow --- Default is true
602-
* - [optional] to allow int format `+` (before number)
603-
*
604-
* @return string
605-
*/
606-
static public function phone($phone = null, ?bool $allow = true)
607-
{
608-
return Tame::cleanPhoneNumber(
609-
$phone, $allow
610-
);
478+
return $value;
611479
}
612480

613481
/**
@@ -711,13 +579,40 @@ static public function removeWhitespace(string $value)
711579
return preg_replace('/\s+/', '', $value);
712580
}
713581

582+
/**
583+
* Generate a string with a specified number of random words.
584+
*
585+
* @param int $wordCount
586+
* @param int $minLength
587+
* @param int $maxLength
588+
* @return string
589+
*/
590+
static public function generateRandomWords(int $wordCount, int $minLength = 3, int $maxLength = 10)
591+
{
592+
$words = [];
593+
$characters = 'abcdefghijklmnopqrstuvwxyz';
594+
595+
for ($i = 0; $i < $wordCount; $i++) {
596+
$length = rand($minLength, $maxLength);
597+
$word = '';
598+
599+
for ($j = 0; $j < $length; $j++) {
600+
$word .= $characters[rand(0, strlen($characters) - 1)];
601+
}
602+
603+
$words[] = $word;
604+
}
605+
606+
return implode(' ', $words);
607+
}
608+
714609
/**
715610
* Get the file extension from a filename or path.
716611
*
717612
* @param string $filename
718613
* @return string|null
719614
*/
720-
static public function fileExtension(string $filename)
615+
static public function getFileExtension(string $filename)
721616
{
722617
$extension = pathinfo($filename, PATHINFO_EXTENSION);
723618

@@ -810,15 +705,4 @@ static public function padString(string $value, int $length, string $padChar = '
810705
return str_pad($value, $length, $padChar, $padType);
811706
}
812707

813-
/**
814-
* Check if an array is associative.
815-
*
816-
* @param array $array
817-
* @return bool
818-
*/
819-
static private function isAssoc(array $array)
820-
{
821-
return array_keys($array) !== range(0, count($array) - 1);
822-
}
823-
824708
}

Tame.php

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -619,17 +619,16 @@ static public function cleanPhoneNumber($phone = null, ?bool $allow = true)
619619
{
620620
$phone = trim((string) $phone);
621621
$phone = str_replace([' ', '-'], '', $phone);
622-
$plus = '';
622+
$phone = str_replace(['(', ')'], '', $phone);
623623

624-
// if plus sign is found in string
625-
if(Str::contains('+', $phone) && $allow){
626-
$plus = "+";
624+
if(Str::contains('+', $phone)){
625+
$phone = str_replace('+', '', $phone);
626+
if($allow){
627+
$phone = "+{$phone}";
628+
}
627629
}
628630

629-
// clean any tags
630-
$phone = self::removeSpecialChars($phone);
631-
632-
return "{$plus}{$phone}";
631+
return $phone;
633632
}
634633

635634
/**
@@ -941,7 +940,7 @@ static public function imageToBase64($path = null)
941940
* @param int $length
942941
* - The desired length of the masked string. Default is 4.
943942
*
944-
* @param string|null $position
943+
* @param string $position
945944
* - The position to apply the mask: 'left', 'middle' or 'center', 'right'. Default is 'right'.
946945
*
947946
* @param string $mask
@@ -950,7 +949,7 @@ static public function imageToBase64($path = null)
950949
* @return string
951950
* - The masked string.
952951
*/
953-
static public function mask($str = null, ?int $length = 4, ?string $position = null, ?string $mask = '*')
952+
static public function mask($str = null, ?int $length = 4, ?string $position = 'right', ?string $mask = '*')
954953
{
955954
// Check if the mbstring extension is available
956955
if (!extension_loaded('mbstring')) {
@@ -968,19 +967,11 @@ static public function mask($str = null, ?int $length = 4, ?string $position = n
968967

969968
// Check if the length parameter is greater than the actual length of the string to avoid errors
970969
if ($isEmail && $atPosition !== false) {
971-
if(empty($position)){
972-
$position = 'left';
973-
}
974970
$length = $length >= mb_strlen(mb_substr($str, 0, $atPosition, 'UTF-8'), 'UTF-8') ? 4 : $length;
975971
} else {
976972
$length = $length >= $strLength ? 4 : $length;
977973
}
978974

979-
// position
980-
if(empty($position)){
981-
$position = 'right';
982-
}
983-
984975
// Calculate string length
985976
$strMinusLength = $strLength - $length;
986977
if ($strMinusLength < 0) {
@@ -1029,7 +1020,7 @@ static public function emailValidator($email = null, ?bool $use_internet = true,
10291020
{
10301021
$filteredEmail = filter_var($email, FILTER_VALIDATE_EMAIL);
10311022

1032-
// if internet usage is set to false
1023+
// if internet usage if set to false
10331024
if(!$use_internet){
10341025
return $filteredEmail !== false;
10351026
}
@@ -1040,7 +1031,7 @@ static public function emailValidator($email = null, ?bool $use_internet = true,
10401031
}
10411032

10421033
// Extract the domain from the email address
1043-
$domain = Str::contains('@', $email) ? explode('@', $email)[1] : '';
1034+
$domain = explode('@', $email)[1];
10441035
$mxRecords = [];
10451036

10461037
// Check DNS records corresponding to a given Internet host name or IP address

0 commit comments

Comments
 (0)