Skip to content

Commit 05abd8c

Browse files
author
Fredrick Peter
committed
same usage, critical update
1 parent aa9641f commit 05abd8c

File tree

8 files changed

+202
-76
lines changed

8 files changed

+202
-76
lines changed

Asset.php

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,9 @@ static public function asset(?string $asset = null, $cache = null)
5050

5151
// cache allow from self method
5252
if($cache){
53-
$cacheTimeAppend = self::getFiletime($file_server) ?? null;
53+
if(!empty($asset)){
54+
$cacheTimeAppend = self::getFiletime($file_server) ?? null;
55+
}
5456
}
5557

5658
return "{$file_domain}{$cacheTimeAppend}";
@@ -73,30 +75,26 @@ static public function asset(?string $asset = null, $cache = null)
7375
*/
7476
static public function config(?string $base_path = null, ?bool $cache = false)
7577
{
76-
// severs
77-
$server = self::getServers();
78-
79-
// set default directory
80-
if(empty($base_path)){
81-
$base_path = "public";
82-
}
83-
8478
// if not defined
8579
if(!defined('ASSET_BASE_DIRECTORY')){
86-
// - Trim forward slash from left and right
87-
$base_path = trim($base_path, '/');
88-
89-
// old url getter helper
90-
$urlFromServer = self::cleanServerPath("{$server['domain']}{$base_path}");
91-
9280
// url helper class
9381
$urlFromhelper = UrlHelper::url();
9482

83+
// if base path is set
84+
if(!empty($base_path)){
85+
86+
// - Trim forward slash from left and right
87+
$base_path = trim($base_path, '/');
88+
89+
// compile
90+
$urlFromhelper = "{$urlFromhelper}/{$base_path}";
91+
}
92+
9593
define('ASSET_BASE_DIRECTORY', [
9694
'cache' => $cache,
9795
'server' => self::formatWithBaseDirectory($base_path),
9896
'domain' => rtrim(
99-
self::cleanServerPath("{$urlFromhelper}{$base_path}"),
97+
self::cleanServerPath($urlFromhelper),
10098
'/'
10199
),
102100
]);

Cookie.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class Cookie{
4545
*/
4646
static protected function init()
4747
{
48-
self::$name = strtolower(str_replace([' '], '', env('APP_NAME')));
48+
self::$name = strtolower(str_replace([' '], '', env('APP_NAME', '')));
4949
self::$timeName = "__time_" . self::$name;
5050
self::$expireName = "__expire_" . self::$name;
5151
self::$timeFormat = Time::timestamp('next year', 'Y-m-d');

Server.php

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ static public function config($key, $default = null, string $base_folder = 'conf
7272
*
7373
* @param array $data
7474
* @param string|null $filename
75+
* - [base path will be automatically added]
76+
*
7577
* @return void
7678
*/
7779
static public function createTemplateFile(?array $data = [], ?string $filename = null)
@@ -124,17 +126,19 @@ static public function createTemplateFile(?array $data = [], ?string $filename =
124126
*/
125127
static public function toArray($value)
126128
{
127-
if(self::isNotValidArray($value)){
129+
// check value is a valid json data
130+
if(self::isValidJson($value)){
131+
return json_decode($value, true);
132+
}
128133

129-
// check value is a valid json data
130-
if(self::isValidJson($value)){
131-
return json_decode($value, true);
134+
// if not valid array and check if array is greater than one element
135+
if(!self::isNotValidArray($value) && count($value) === 1){
136+
if(!self::isNotValidArray($value[0])){
137+
return $value;
132138
}
133-
134-
return json_decode(json_encode($value), true);
135139
}
136140

137-
return $value;
141+
return json_decode(json_encode($value), true);
138142
}
139143

140144
/**

Str.php

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

55
namespace Tamedevelopers\Support;
66

7+
use Tamedevelopers\Support\Server;
8+
79
class Str{
810

911
/**
@@ -46,6 +48,53 @@ static public function last($array = null)
4648

4749
return end($array);
4850
}
51+
52+
/**
53+
* Change Keys of Array
54+
*
55+
* @param array $data
56+
* @param string $fromKey
57+
* @param string $toKey
58+
* @return array
59+
*/
60+
static public function changeKeysFromArray($data = [], $fromKey, $toKey)
61+
{
62+
// always convert to an array
63+
$data = Server::toArray($data);
64+
65+
// If you don't want to modify the original array and create a new one without 'id' columns:
66+
return array_map(function($data) use($fromKey, $toKey) {
67+
if (isset($data[$fromKey])) {
68+
$data[$toKey] = $data[$fromKey];
69+
unset($data[$fromKey]);
70+
}
71+
return $data;
72+
}, $data);
73+
}
74+
75+
/**
76+
* Remove Keys From Array
77+
*
78+
* @param array $data
79+
* @param mixed $keys
80+
* @return array
81+
*/
82+
static public function removeKeysFromArray($data = [], ...$keys)
83+
{
84+
// 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);
90+
foreach($keys as $key){
91+
if(isset($data[$key])){
92+
unset($data[$key]);
93+
}
94+
}
95+
return $data;
96+
}, $data);
97+
}
4998

5099
/**
51100
* Convert array keys to specified key if available, else return the original array.
@@ -433,11 +482,11 @@ static public function upper($value = null)
433482
/**
434483
* Check if a string or an array of words contains a given substring.
435484
*
436-
* @param string|array $haystack
437485
* @param string $needle
486+
* @param string|array $haystack
438487
* @return bool
439488
*/
440-
static public function contains(string|array $haystack, string $needle)
489+
static public function contains(string $needle, string|array $haystack)
441490
{
442491
if (is_array($haystack)) {
443492
// Check if any word in the array contains the substring

Tame.php

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -617,10 +617,11 @@ static public function sortMultipleArray($key = null, ?array &$data = [], ?strin
617617
*/
618618
static public function cleanPhoneNumber($phone = null, ?bool $allow = true)
619619
{
620-
$phone = str_replace(' ', '', str_replace('-', '', $phone));
621-
$phone = str_replace('(', '', str_replace(')', '', $phone));
622-
$phone = str_replace(' ', '', $phone);
623-
if(Str::contains($phone, '+')){
620+
$phone = trim((string) $phone);
621+
$phone = str_replace([' ', '-'], '', $phone);
622+
$phone = str_replace(['(', ')'], '', $phone);
623+
624+
if(Str::contains('+', $phone)){
624625
$phone = str_replace('+', '', $phone);
625626
if($allow){
626627
$phone = "+{$phone}";
@@ -689,7 +690,7 @@ static public function shortenString($string = null, $limit = 50, $replacer = '.
689690
$string = trim(str_replace(PHP_EOL, ' ', $string));
690691

691692
if(strlen($string) > $limit) {
692-
return mb_strcut($string, 0, $limit) . $replacer;
693+
return mb_strcut($string, 0, (int) $limit) . $replacer;
693694
}
694695

695696
return $string;
@@ -799,7 +800,7 @@ static public function unlink(string $pathToFile, $fileName = null)
799800
* Convert json data to array|object
800801
*
801802
* @param string $path
802-
* - [base path will be automatically added]
803+
* - [full path to destination]
803804
*
804805
* @param bool $format
805806
* - [optional] `true` will convert to an array
@@ -808,10 +809,8 @@ static public function unlink(string $pathToFile, $fileName = null)
808809
*/
809810
static public function convertJsonData($path, $format = true)
810811
{
811-
$fullPath = self::getBasePath($path);
812-
813-
if(self::exists($fullPath)){
814-
return json_decode(file_get_contents($fullPath), $format);
812+
if(self::exists($path)){
813+
return json_decode(file_get_contents($path), $format);
815814
}
816815
}
817816

Traits/ServerTrait.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
namespace Tamedevelopers\Support\Traits;
66

77
use ReflectionClass;
8-
use Tamedevelopers\Support\Env;
9-
use Tamedevelopers\Support\Str;
108
use Tamedevelopers\Support\UrlHelper;
119

1210

@@ -100,12 +98,12 @@ static public function formatWithBaseDirectory($path = null)
10098
*/
10199
static public function formatWithDomainURI($path = null)
102100
{
103-
$server = rtrim(
101+
$domain = rtrim(
104102
self::getServers('domain'),
105103
'/'
106104
);
107105
return self::pathReplacer(
108-
"{$server}/{$path}"
106+
"{$domain}/{$path}"
109107
);
110108
}
111109

0 commit comments

Comments
 (0)