-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathServer.php
219 lines (185 loc) · 5.55 KB
/
Server.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
<?php
declare(strict_types=1);
namespace Tamedevelopers\Support;
use Tamedevelopers\Support\Traits\ServerTrait;
use Tamedevelopers\Support\Traits\ReusableTrait;
class Server{
use ServerTrait, ReusableTrait;
/**
* Get the value of a configuration option.
*
* @param mixed $key
* The configuration key in dot notation (e.g., 'database.connections.mysql')
*
* @param mixed $default
* [optional] The default value to return if the configuration option is not found
*
* @param string $base_folder
* [optional] Custom base folder after the base_path()
* - Default base for config() is 'config' folder.
*
* @return mixed
* The value of the configuration option, or null if it doesn't exist
*/
static public function config($key, $default = null, string $base_folder = 'config')
{
// Convert the key to an array
$parts = explode('.', $key);
// Get the file name
$filePath = base_path("{$base_folder}/{$parts[0]}.php");
// Check if the configuration file exists
if (file_exists($filePath)) {
// Load the configuration array from the file
$config = require($filePath);
}
// Remove the file name from the parts array
unset($parts[0]);
// Compile the configuration value
foreach ($parts as $part) {
if (isset($config[$part])) {
$config = $config[$part];
} else {
$config = null;
}
}
// if config not set
if(!isset($config)){
$config = null;
}
// try merging data if an array
if(is_array($config) && is_array($default)){
return array_merge($config, $default);
}
return $config ?? $default;
}
/**
* Create Template File
*
* @param array $data
* @param string|null $filename
* - [base path will be automatically added]
*
* @return void
*/
static public function createTemplateFile(?array $data = [], ?string $filename = null)
{
// Get the file name
$filePath = base_path($filename);
// Generate PHP code
$exported = var_export($data, true);
$string = explode("\n", $exported);
$string = array_map('trim', $string);
$string = implode("\n ", $string);
$string = ltrim($string, 'array (');
$string = rtrim($string, ')');
$string = trim($string);
// Generate PHP code with specific formatting
$phpCode = <<<PHP
<?php
return [
/*
|--------------------------------------------------------------------------
| Template File Lines
|--------------------------------------------------------------------------
|
| The following template lines are used during text formatting for various
| messages that we need to display to the user. You are free to modify
| these template lines according to your application's requirements.
|
*/
$string
];
PHP;
// to avoid warning error
// we check if path is a directory first before executing the code
if(is_dir(dirname($filePath))){
file_put_contents($filePath, $phpCode);
}
}
/**
* Convert Value to an Array
*
* @param mixed $value
* @return array
*/
static public function toArray($value)
{
// check value is a valid json data
if (is_string($value)) {
if(self::isValidJson($value)){
return json_decode($value, true);
}
}
// if not valid array, check if array is equal to one element
if(!self::isNotValidArray($value) && count($value) === 1){
if(!self::isNotValidArray($value[0] ?? $value)){
return $value;
}
}
return json_decode(
json_encode($value),
true
);
}
/**
* Convert Value to an Object
*
* @param mixed $value
* @return object
*/
static public function toObject($value)
{
return json_decode(
json_encode( self::toArray($value) ),
false
);
}
/**
* Convert Value to Json Data
*
* @param mixed $value
* @return string
*/
static public function toJson($value)
{
if (self::isValidJson($value)) {
return $value;
}
return json_encode($value);
}
/**
* Check if data is not a valid array
*
* @param mixed $data
* @return bool
*/
static private function isNotValidArray(mixed $data = null)
{
// Return true if $data is not an array
if (!is_array($data)) {
return true;
}
// Check if $data contains any non-array values
foreach ($data as $value) {
if (!is_array($value)) {
return true; // Return true if a non-array value is found
}
}
// Return false if $data is a valid array
return false;
}
/**
* Check if data is valid JSON.
*
* @param mixed $data
* @return bool
*/
static private function isValidJson(mixed $data = null)
{
if(is_string($data)){
json_decode($data);
return json_last_error() === JSON_ERROR_NONE;
}
return false;
}
}