Skip to content

Commit 513b20e

Browse files
update
1 parent 015c3f9 commit 513b20e

File tree

4 files changed

+118
-44
lines changed

4 files changed

+118
-44
lines changed

Server.php

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -127,18 +127,23 @@ static public function createTemplateFile(?array $data = [], ?string $filename =
127127
static public function toArray($value)
128128
{
129129
// check value is a valid json data
130-
if(self::isValidJson($value)){
131-
return json_decode($value, true);
130+
if (is_string($value)) {
131+
if(self::isValidJson($value)){
132+
return json_decode($value, true);
133+
}
132134
}
133135

134-
// if not valid array and check if array is greater than one element
136+
// if not valid array, check if array is equal to one element
135137
if(!self::isNotValidArray($value) && count($value) === 1){
136138
if(!self::isNotValidArray($value[0] ?? $value)){
137139
return $value;
138140
}
139141
}
140142

141-
return json_decode(json_encode($value), true);
143+
return json_decode(
144+
json_encode($value),
145+
true
146+
);
142147
}
143148

144149
/**
@@ -163,10 +168,10 @@ static public function toObject($value)
163168
*/
164169
static public function toJson($value)
165170
{
166-
if(self::isValidJson($value)){
171+
if (self::isValidJson($value)) {
167172
return $value;
168173
}
169-
174+
170175
return json_encode($value);
171176
}
172177

@@ -178,16 +183,23 @@ static public function toJson($value)
178183
*/
179184
static private function isNotValidArray(mixed $data = null)
180185
{
186+
// Return true if $data is not an array
181187
if (!is_array($data)) {
182188
return true;
183189
}
184190

185-
// array filter
186-
$filteredArray = array_filter($data, 'is_array');
187-
188-
return count($filteredArray) === count($data);
191+
// Check if $data contains any non-array values
192+
foreach ($data as $value) {
193+
if (!is_array($value)) {
194+
return true; // Return true if a non-array value is found
195+
}
196+
}
197+
198+
// Return false if $data is a valid array
199+
return false;
189200
}
190201

202+
191203
/**
192204
* Check if data is valid JSON.
193205
*

Str.php

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,35 @@ static public function last($array = null)
4949
return end($array);
5050
}
5151

52+
/**
53+
* For sorting array
54+
*
55+
* @param array $data
56+
* @param string $type
57+
* - [rsort|asort|ksort|arsort|krsort|sort]
58+
*
59+
* @return void
60+
*/
61+
static public function sortArray(?array &$data = [], ?string $type = 'sort')
62+
{
63+
Tame::sortArray($data, $type);
64+
}
65+
66+
/**
67+
* For sorting muti-dimentional array
68+
*
69+
* @param array $data
70+
* @param string|null $key
71+
* @param string $type
72+
* - [asc|desc|snum]
73+
*
74+
* @return void
75+
*/
76+
static public function sortMultipleArray(?array &$data = [], $key = null, ?string $type = 'asc')
77+
{
78+
Tame::sortMultipleArray($data, $key, $type);
79+
}
80+
5281
/**
5382
* Change Keys of Array
5483
*
@@ -82,18 +111,22 @@ static public function changeKeysFromArray($data, $fromKey, $toKey)
82111
static public function removeKeysFromArray($data, ...$keys)
83112
{
84113
// always convert to an array
85-
$data = Server::toArray($data);
86-
87-
// If you don't want to modify the original array and create a new one without 'id' columns:
88-
return array_map(function ($data) use ($keys) {
89-
$keys = self::flattenValue($keys);
114+
$data = (array) $data;
115+
116+
// flattern keys
117+
$keys = self::flattenValue($keys);
118+
119+
// Iterate through each data item
120+
foreach ($data as &$item) {
121+
// Remove specified keys from the item
90122
foreach ($keys as $key) {
91-
if (isset($data[$key])) {
92-
unset($data[$key]);
123+
if (isset($item[$key])) {
124+
unset($item[$key]);
93125
}
94126
}
95-
return $data;
96-
}, $data);
127+
}
128+
129+
return $data;
97130
}
98131

99132
/**

Tame.php

Lines changed: 41 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -547,52 +547,74 @@ static public function isArraySame(?array $data = [])
547547
* @param string $type
548548
* - [rsort|asort|ksort|arsort|krsort|sort]
549549
*
550-
* @return array
550+
* @return void
551551
*/
552-
static public function sortArray(?array $data = [], ?string $type = 'sort')
552+
static public function sortArray(?array &$data = [], ?string $type = 'sort')
553553
{
554+
// Validate that $data is an array
555+
if (!is_array($data)) {
556+
return;
557+
}
558+
559+
// Perform sorting based on the specified type
554560
switch ($type) {
555561
case 'rsort':
556-
rsort($data); // sort arrays in descending order
562+
rsort($data); // Sort arrays in descending order
557563
break;
558-
564+
559565
case 'asort':
560-
asort($data); // sort associative arrays in ascending order, according to the value
566+
asort($data); // Sort associative arrays in ascending order, according to the value
561567
break;
562-
568+
563569
case 'ksort':
564-
ksort($data); // sort associative arrays in ascending order, according to the key
570+
ksort($data); // Sort associative arrays in ascending order, according to the key
565571
break;
566-
572+
567573
case 'arsort':
568-
arsort($data); // sort associative arrays in descending order, according to the value
574+
arsort($data); // Sort associative arrays in descending order, according to the value
569575
break;
570-
576+
571577
case 'krsort':
572-
krsort($data); // sort associative arrays in descending order, according to the value
578+
krsort($data); // Sort associative arrays in descending order, according to the value
573579
break;
574-
580+
575581
default:
576-
sort($data); // sort arrays in ascending order
582+
sort($data); // Sort arrays in ascending order
577583
break;
578584
}
579-
580-
return $data;
581585
}
582586

583587
/**
584588
* For sorting muti-dimentional array
585589
*
586-
* @param string|null $key
587590
* @param array $data
591+
* @param string|null $key
588592
* @param string $type
589593
* - [asc|desc|snum]
590594
*
591595
* @return void
592596
*/
593-
static public function sortMultipleArray($key = null, ?array &$data = [], ?string $type = 'asc')
597+
static public function sortMultipleArray(?array &$data = [], $key = null, ?string $type = 'asc')
594598
{
599+
// Check if $data is an array and not empty
600+
if (!is_array($data) || empty($data)) {
601+
return;
602+
}
603+
604+
// Check if $key is provided
605+
if ($key === null) {
606+
// If $key is not provided, return without sorting
607+
return;
608+
}
609+
610+
// Extract values of the specified key from each sub-array
595611
$id = array_column($data, $key);
612+
613+
// Ensure $id and $data have the same size before sorting
614+
if (count($id) !== count($data)) {
615+
return;
616+
}
617+
596618
switch ($type) {
597619
case 'desc':
598620
array_multisort($id, SORT_DESC, $data); //sort associative arrays in descending order
@@ -831,10 +853,8 @@ static public function convertJsonData($path, $format = true)
831853
*/
832854
static public function saveDataAsJsonObject(string $destination, mixed $data, ?bool $type = true)
833855
{
834-
$format = JSON_PRETTY_PRINT;
835-
if(!$type){
836-
$format = JSON_UNESCAPED_UNICODE;
837-
}
856+
// Choose the JSON encoding format
857+
$format = $type ? JSON_PRETTY_PRINT : JSON_UNESCAPED_UNICODE;
838858

839859
// check or convert data to an array
840860
if(!is_array(!$data)){

Tests/test.php

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

33
use Tamedevelopers\Support\Env;
44
use Tamedevelopers\Support\PDF;
5+
use Tamedevelopers\Support\Str;
56
use Tamedevelopers\Support\Tame;
67
use Tamedevelopers\Support\Slugify;
78

@@ -53,13 +54,21 @@
5354

5455
include $svg;
5556

56-
echo "<img src='$svg'>";
57+
58+
// Define an array to sort
59+
$data = [4, 2, 7, 1, 5];
60+
61+
Str::sortArray($data, 'krsort');
5762

5863
dd(
59-
Tame::platformIcon('windows'),
60-
64+
Tame::platformIcon('windows'),
65+
6166
to_object($Json),
62-
67+
68+
to_array($Json),
69+
70+
$data,
71+
6372
// server()->toArray($Json),
6473
);
6574

0 commit comments

Comments
 (0)