Skip to content

Commit ef78033

Browse files
author
Fredrick Peter
committed
convertArrayKey() method aaded and Time Error fixed
1 parent fd28d4d commit ef78033

File tree

2 files changed

+67
-65
lines changed

2 files changed

+67
-65
lines changed

Str.php

Lines changed: 45 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ static public function wrap($value)
2424
/**
2525
* Get the first element of an array.
2626
*
27-
* @param array $array
27+
* @param array|null $array
2828
* @return mixed|null
2929
*/
3030
static public function head($array = null)
@@ -35,7 +35,7 @@ static public function head($array = null)
3535
/**
3636
* Get the last element of an array.
3737
*
38-
* @param array $array
38+
* @param array|null $array
3939
* @return mixed|null
4040
*/
4141
static public function last($array = null)
@@ -46,6 +46,37 @@ static public function last($array = null)
4646

4747
return end($array);
4848
}
49+
50+
/**
51+
* Convert array keys to specified key if available, else return the original array.
52+
*
53+
* @param array $data The input data array.
54+
* @param string $key The key to use for conversion.
55+
* @param int $case The case sensitivity option for key comparison (upper, lower).
56+
*
57+
* @return array
58+
* - The converted array with specified key as keys if available, else the original array
59+
*/
60+
static public function convertArrayKey(array $data, string $key, $case = null)
61+
{
62+
// Extract the specified key values from the sub-arrays
63+
$values = array_column($data, $key);
64+
65+
// Check if specified key values are available
66+
if ($values) {
67+
// Apply case transformation based on the specified option
68+
$matchValue = match (self::lower($case)) {
69+
'upper' => array_map('strtoupper', $values),
70+
'lower' => array_map('strtolower', $values),
71+
default => $values,
72+
};
73+
74+
// Combine specified key values as keys with the original sub-arrays as values
75+
return array_combine($matchValue, $data);
76+
}
77+
78+
return $data;
79+
}
4980

5081
/**
5182
* Merge the binding arrays into a single array.
@@ -104,7 +135,7 @@ static public function flattenValue(array $array)
104135
* @param mixed $keys The key(s) to exclude
105136
* @return array The filtered array
106137
*/
107-
static public function exceptArray($array, $keys)
138+
static public function exceptArray(array $array, $keys)
108139
{
109140
// Convert single key to an array
110141
if (!is_array($keys)) {
@@ -123,9 +154,10 @@ static public function exceptArray($array, $keys)
123154
* @param string $search The substring to search for.
124155
* @param string $replace The replacement substring.
125156
* @param string $subject The original string.
126-
* @return string The modified string.
157+
* @return string
158+
* - The modified string.
127159
*/
128-
static public function replaceFirst($search, $replace, $subject)
160+
static public function replaceFirst(string $search, string $replace, string $subject)
129161
{
130162
// Find the position of the first occurrence of the search string
131163
$pos = strpos($subject, $search);
@@ -145,9 +177,10 @@ static public function replaceFirst($search, $replace, $subject)
145177
* @param string $search The substring to search for.
146178
* @param string $replace The replacement substring.
147179
* @param string $subject The original string.
148-
* @return string The modified string.
180+
* @return string
181+
* - The modified string.
149182
*/
150-
static public function replaceLast($search, $replace, $subject)
183+
static public function replaceLast(string $search, string $replace, string $subject)
151184
{
152185
// Find the position of the first occurrence of the search string
153186
$pos = strrpos($subject, $search);
@@ -164,10 +197,10 @@ static public function replaceLast($search, $replace, $subject)
164197
/**
165198
* Get the plural form of an English word.
166199
*
167-
* @param string $value
200+
* @param string|null $value
168201
* @return string
169202
*/
170-
static public function pluralize(?string $value = null)
203+
static public function pluralize($value = null)
171204
{
172205
$value = (string) $value;
173206
if (strlen($value) === 1) {
@@ -381,7 +414,7 @@ static public function slugify(string $value, string $separator = '-')
381414
*
382415
* @return string
383416
*/
384-
static public function lower(?string $value = null)
417+
static public function lower($value = null)
385418
{
386419
return trim(strtolower((string) $value));
387420
}
@@ -392,7 +425,7 @@ static public function lower(?string $value = null)
392425
*
393426
* @return string
394427
*/
395-
static public function upper(?string $value = null)
428+
static public function upper($value = null)
396429
{
397430
return trim(strtoupper((string) $value));
398431
}
@@ -404,7 +437,7 @@ static public function upper(?string $value = null)
404437
* @param string $needle
405438
* @return bool
406439
*/
407-
static public function contains($haystack, string $needle)
440+
static public function contains(string|array $haystack, string $needle)
408441
{
409442
if (is_array($haystack)) {
410443
// Check if any word in the array contains the substring

Time.php

Lines changed: 22 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class Time {
3232
* @param int|string|null $date
3333
* @param string|null $timezone
3434
*/
35-
public function __construct($date = 'now', ?string $timezone = 'UTC')
35+
public function __construct($date = 'now', $timezone = 'UTC')
3636
{
3737
if(empty(self::$date)){
3838
self::setDate($date);
@@ -69,11 +69,11 @@ static public function __callStatic($name, $args)
6969

7070
/**
7171
* Set the timezone.
72-
* @param string $timezone
72+
* @param string|null $timezone
7373
*
7474
* @return $this
7575
*/
76-
static public function setTimezone(?string $timezone = null)
76+
static public function setTimezone($timezone = null)
7777
{
7878
if(in_array($timezone, Country::timeZone())){
7979
self::$timezone = $timezone;
@@ -83,7 +83,7 @@ static public function setTimezone(?string $timezone = null)
8383
}
8484

8585
// set timezone
86-
date_default_timezone_set($timezone);
86+
date_default_timezone_set(self::$timezone);
8787

8888
return new self(self::$date, self::$timezone);
8989
}
@@ -172,7 +172,7 @@ static public function format(int|string $date)
172172
*
173173
* @return string
174174
*/
175-
static public function timestamp($date, ?string $format = "Y-m-d H:i:s")
175+
static public function timestamp($date, $format = "Y-m-d H:i:s")
176176
{
177177
if(is_string($date)){
178178
$date = strtotime($date);
@@ -299,7 +299,7 @@ static public function getYear()
299299
*
300300
* @return mixed
301301
*/
302-
static public function timeDifference(?string $mode = null)
302+
static public function timeDifference($mode = null)
303303
{
304304
$now = new DateTime('now', new DateTimeZone(self::getTimezone()));
305305
$date = new DateTime();
@@ -348,7 +348,7 @@ static public function greeting()
348348
*
349349
* @return string
350350
*/
351-
static public function timeAgo(?string $mode = null)
351+
static public function timeAgo($mode = null)
352352
{
353353
$minutes = self::getMin();
354354
$seconds = self::getSecond();
@@ -419,7 +419,7 @@ static public function timeAgo(?string $mode = null)
419419
*
420420
* @return mixed
421421
*/
422-
static private function getText(?string $mode = null)
422+
static private function getText($mode = null)
423423
{
424424
if(!defined('TIME_TEXT')){
425425
self::config();
@@ -450,51 +450,20 @@ static private function nonExistMethod($method = null, $args = null)
450450
// convert to lowercase
451451
$name = Str::lower($method);
452452

453-
switch ($name) {
454-
case in_array($name, ['tojs', 'jstimer']):
455-
$method = 'toJsTimer';
456-
break;
457-
458-
case in_array($name, ['time', 'gettimes', 'gettime']):
459-
$method = 'getDate';
460-
break;
461-
462-
case in_array($name, ['hours', 'hr', 'hrs', 'gethr', 'gethours']):
463-
$method = 'getHour';
464-
break;
465-
466-
case in_array($name, ['getseconds', 'getsec', 'sec', 's']):
467-
$method = 'getSecond';
468-
break;
469-
470-
case in_array($name, ['min', 'mins', 'getminute', 'getminutes', 'getmins']):
471-
$method = 'getMin';
472-
break;
473-
474-
case in_array($name, ['getday', 'getdays', 'getd', 'day', 'days']):
475-
$method = 'getDay';
476-
break;
477-
478-
case in_array($name, ['getweek', 'getweeks', 'getw']):
479-
$method = 'getWeek';
480-
break;
481-
482-
case in_array($name, ['getmonths', 'getmonth', 'getm']):
483-
$method = 'getMonth';
484-
break;
485-
486-
case in_array($name, ['getyr', 'getyears', 'getyear', 'year', 'years', 'yr', 'yrs', 'y']):
487-
$method = 'getYear';
488-
break;
489-
490-
case $name === 'greetings':
491-
$method = 'greeting';
492-
break;
493-
494-
default:
495-
$method = 'timeAgo';
496-
break;
497-
}
453+
// create correct method name
454+
$method = match ($name) {
455+
'greetings' => 'greeting',
456+
'tojs', 'jstimer' => 'toJsTimer',
457+
'time', 'gettimes', 'gettime' => 'getDate',
458+
'hours', 'hr', 'hrs', 'gethr', 'gethours' => 'getHour',
459+
'getseconds', 'getsec', 'sec', 's' => 'getSecond',
460+
'min', 'mins', 'getminute', 'getminutes', 'getmins' => 'getMin',
461+
'getday', 'getdays', 'getd', 'day', 'days' => 'getDay',
462+
'getweek', 'getweeks', 'getw' => 'getWeek',
463+
'getmonths', 'getmonth', 'getm' => 'getMonth',
464+
'getyr', 'getyears', 'getyear', 'year', 'years', 'yr', 'yrs', 'y' => 'getYear',
465+
default => 'timeAgo'
466+
};
498467

499468
// create instance of new static self
500469
$instance = new static(self::$date, self::$timezone);

0 commit comments

Comments
 (0)