Skip to content

Commit b2dfaf4

Browse files
update
1 parent 57ae0bc commit b2dfaf4

File tree

11 files changed

+1157
-9
lines changed

11 files changed

+1157
-9
lines changed

Asset.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Tamedevelopers\Support;
66

7+
use Tamedevelopers\Support\Str;
78
use Tamedevelopers\Support\Traits\ServerTrait;
89

910
class Asset{
@@ -38,7 +39,7 @@ static public function asset(?string $asset = null, $cache = null)
3839
}
3940

4041
// trim
41-
$asset = trim((string) $asset, '/');
42+
$asset = Str::trim($asset, '/');
4243

4344
$file_domain = "{$assetPath['domain']}/{$asset}";
4445

@@ -84,7 +85,7 @@ static public function config(?string $base_path = null, ?bool $cache = false)
8485
if(!empty($base_path)){
8586

8687
// - Trim forward slash from left and right
87-
$base_path = trim($base_path, '/');
88+
$base_path = Str::trim($base_path, '/');
8889

8990
// base for url path
9091
$baseForUrlPath = $base_path;

Capsule/Manager.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ static public function setHeaders($status = 404, $closure = null)
175175
*/
176176
static public function replaceWhiteSpace(?string $string = null)
177177
{
178-
return trim(preg_replace(
178+
return Str::trim(preg_replace(
179179
self::$regex_whitespace,
180180
" ",
181181
$string

Env.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use Exception;
88
use Dotenv\Dotenv;
9+
use Tamedevelopers\Support\Str;
910
use Tamedevelopers\Support\Tame;
1011
use Tamedevelopers\Support\Constant;
1112
use Tamedevelopers\Support\Capsule\Manager;
@@ -250,7 +251,7 @@ static public function env($key = null, $value = null)
250251
$envData = array_change_key_case($_ENV, CASE_UPPER);
251252

252253
// convert to upper-case
253-
$key = strtoupper(trim((string) $key));
254+
$key = Str::upper(Str::trim($key));
254255

255256
return $envData[$key] ?? $value;
256257
}

NumberToWords.php

Lines changed: 203 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,203 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tamedevelopers\Support;
6+
7+
use Tamedevelopers\Support\Str;
8+
use Tamedevelopers\Support\Traits\NumberToWordsTraits;
9+
10+
class NumberToWords {
11+
12+
use NumberToWordsTraits;
13+
14+
/**
15+
* Allow cents text to be added
16+
*
17+
* @var boolean
18+
*/
19+
static private $allowCents = false;
20+
21+
/**
22+
* Currency data
23+
*
24+
* @var mixed
25+
*/
26+
static private $currencyData;
27+
28+
/**
29+
* Convert a number to its text representation.
30+
* - Can be able to convert numbers unto quintillion
31+
*
32+
* @param string|int|float $number
33+
*
34+
* @param bool $cents
35+
* - [optional] Default is false
36+
*
37+
* @param string|null $code
38+
* - [optional] Currency code
39+
*
40+
* @return string
41+
*/
42+
static public function text($number, $code = null, $cents = false)
43+
{
44+
self::$allowCents = $cents;
45+
46+
// trim to convert to string
47+
$number = Str::trim($number);
48+
49+
// get currency code
50+
self::$currencyData = self::getCurrencyText($code);
51+
52+
// if cents is allowed
53+
if(self::$allowCents){
54+
55+
// get name of currency
56+
$currencyText = self::$currencyData['name'] ?? null;
57+
58+
// allow if not empty
59+
$currencyText = !empty($currencyText) ? " {$currencyText}" : null;
60+
} else{
61+
$currencyText = null;
62+
}
63+
64+
// convert number to text
65+
$numberText = self::convertNumberToText($number);
66+
67+
// remove line break from text
68+
$numberText = Str::trim(
69+
Str::replace(["\n", "\r"], '', $numberText)
70+
);
71+
72+
return ucfirst($numberText) . $currencyText . self::toCents($number);
73+
}
74+
75+
/**
76+
* Convert to cents
77+
*
78+
* @param string $number
79+
* @return string
80+
*/
81+
static private function toCents($number)
82+
{
83+
// if number contain (.) dot
84+
// we treat as cents
85+
if(Str::contains('.', $number)){
86+
$cents = explode('.', $number);
87+
$decimalNumber = isset($cents[1]) ? $cents[1] : null;
88+
89+
if(!empty($decimalNumber) && self::$allowCents){
90+
91+
$centsCurrency = Str::trim(
92+
self::convertNumberToText($decimalNumber)
93+
);
94+
95+
// cents text
96+
$centsText = self::$currencyData['cents'] ?? null;
97+
98+
// allow if not empty
99+
$centsText = !empty($centsText) ? " {$centsText}" : '';
100+
101+
return ", {$centsCurrency}{$centsText}";
102+
}
103+
}
104+
}
105+
106+
/**
107+
* Convert the integer part of a number to its text representation.
108+
*
109+
* @param string $number
110+
* @return string
111+
*/
112+
static private function convertNumberToText($number)
113+
{
114+
if (intval($number) == 0) {
115+
return 'zero';
116+
}
117+
118+
return self::convertIntegerToText($number);
119+
}
120+
121+
/**
122+
* Convert an integer to its text representation.
123+
*
124+
* @param string $number
125+
* @return string
126+
*/
127+
static private function convertIntegerToText($number)
128+
{
129+
$result = '';
130+
$i = 0;
131+
132+
while (intval($number) > 0) {
133+
$chunk = intval($number) % 1000;
134+
$number = intval($number) / 1000;
135+
136+
if (intval($chunk) > 0) {
137+
$result = self::convertChunkToText($chunk) . ' ' . self::$units[$i] . ' ' . $result;
138+
}
139+
140+
$i++;
141+
}
142+
143+
return $result;
144+
}
145+
146+
/**
147+
* Convert a chunk of numbers (up to 999) to its text representation.
148+
*
149+
* @param string $number
150+
* @return string
151+
*/
152+
static private function convertChunkToText($number)
153+
{
154+
$result = '';
155+
156+
if (intval($number) >= 100) {
157+
$result .= self::$words[(int)intval($number) / 100] . ' hundred';
158+
$number = intval($number) % 100;
159+
if (intval($number) > 0) {
160+
$result .= ' and ';
161+
}
162+
}
163+
164+
if (intval($number) > 0) {
165+
if (intval($number) < 20) {
166+
$result .= self::$words[intval($number)];
167+
} else {
168+
$result .= self::$tens[(int)intval($number) / 10];
169+
if (intval($number) % 10 > 0) {
170+
$result .= '-' . self::$words[intval($number) % 10];
171+
}
172+
}
173+
}
174+
175+
return $result;
176+
177+
178+
}
179+
180+
/**
181+
* Get the text representation of a currency code.
182+
*
183+
* @param string|null $code
184+
* - [NGA, USD, EUR]
185+
*
186+
* @return array|null
187+
*/
188+
static public function getCurrencyText($code = null)
189+
{
190+
// convert code to upper
191+
$code = Str::upper($code);
192+
193+
// get data
194+
$data = self::currencyNames()[$code] ?? null;
195+
196+
if(is_null($data)){
197+
return;
198+
}
199+
200+
return Str::convertArrayCase($data, 'lower', 'lower');
201+
}
202+
203+
}

Str.php

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,8 @@ static public function convertArrayKey(array $data, string $key, $case = null)
115115
if ($values) {
116116
// Apply case transformation based on the specified option
117117
$matchValue = match (self::lower($case)) {
118-
'upper' => array_map('strtoupper', $values),
119-
'lower' => array_map('strtolower', $values),
118+
'upper', 'uppercase', 'upper_case' => array_map('strtoupper', $values),
119+
'lower', 'lowercase', 'lower_case' => array_map('strtolower', $values),
120120
default => $values,
121121
};
122122

@@ -127,6 +127,35 @@ static public function convertArrayKey(array $data, string $key, $case = null)
127127
return $data;
128128
}
129129

130+
/**
131+
* Change the case of keys and/or values in a multi-dimensional array.
132+
*
133+
* @param array $data The input array
134+
* @param string $key The case to convert for keys: 'lower', 'upper', or 'unchanged'
135+
* @param string $value The case to convert for values: 'lower', 'upper', or 'unchanged'
136+
*
137+
* @return array The array with converted case
138+
*/
139+
public static function convertArrayCase(array $data, string $key = 'lower', string $value = 'unchanged'): array
140+
{
141+
$result = [];
142+
143+
foreach ($data as $currentKey => $currentValue) {
144+
145+
// convert the key at first
146+
$convertedKey = self::convertCase($currentKey, $key);
147+
148+
if (is_array($currentValue)) {
149+
$result[$convertedKey] = self::convertArrayCase($currentValue, $key, $value);
150+
} else {
151+
$convertedValue = self::convertCase($currentValue, $value);
152+
$result[$convertedKey] = $convertedValue;
153+
}
154+
}
155+
156+
return $result;
157+
}
158+
130159
/**
131160
* Merge the binding arrays into a single array.
132161
*
@@ -840,4 +869,22 @@ static public function padString(string $value, int $length, string $padChar = '
840869
{
841870
return str_pad($value, $length, $padChar, $padType);
842871
}
872+
873+
/**
874+
* Convert the case of a string based on the specified type.
875+
*
876+
* @param string|int $string The input string
877+
* @param string $type The case to convert: 'lower', 'upper', or 'unchanged'
878+
*
879+
* @return string The string with converted case
880+
*/
881+
private static function convertCase(string|int $string, string $type)
882+
{
883+
return match (self::lower($type)) {
884+
'upper', 'uppercase', 'upper_case' => self::upper($string),
885+
'lower', 'lowercase', 'lower_case' => self::lower($string),
886+
default => $string,
887+
};
888+
}
889+
843890
}

Tame.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -956,6 +956,8 @@ static public function mask($str = null, ?int $length = 4, ?string $position = '
956956
return $str;
957957
}
958958

959+
$str = Str::trim($str);
960+
959961
// Get the length of the string
960962
$strLength = mb_strlen($str, 'UTF-8');
961963

Tests/currency.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
use Tamedevelopers\Support\NumberToWords;
4+
5+
require_once __DIR__ . '/../vendor/autoload.php';
6+
7+
8+
dd(
9+
10+
NumberToWords::text(1205435349345443534),
11+
12+
NumberToWords::text(455987.09, 'nga', true),
13+
14+
NumberToWords::text(34590323, 'FRA', true),
15+
16+
NumberToWords::text('120.95', 'tUr', true),
17+
18+
NumberToWords::text(1999),
19+
20+
// NumberToWords::CurrencyNames()
21+
22+
);

Tests/str.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,16 @@
3232
['is_active', 'id']
3333
);
3434

35+
3536
dd(
3637
$changeArrayKeys,
3738
$removeArrayKeys,
3839

40+
Str::convertArrayCase(
41+
data: $arrayCollection,
42+
value: 'lower'
43+
),
44+
3945
Str::snake('Peterso More'),
4046
Str::camel('peterson more'),
4147
Str::studly('peterson more'),

0 commit comments

Comments
 (0)