1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace InnoFlash\LaraStart; |
4
|
|
|
|
5
|
|
|
use Carbon\Carbon; |
6
|
|
|
use Illuminate\Http\Request; |
7
|
|
|
|
8
|
|
|
class Helper |
9
|
|
|
{ |
10
|
|
|
public static $strRlc = [ |
11
|
|
|
'/', |
12
|
|
|
'//', |
13
|
|
|
'\\', |
14
|
|
|
'\\\\', |
15
|
|
|
]; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Gets the limit set in the request or returns a default set in the config. |
19
|
|
|
* |
20
|
|
|
* @param Request $request |
21
|
|
|
* @return \Illuminate\Config\Repository|mixed |
22
|
|
|
*/ |
23
|
|
|
public static function getLimit(Request $request) |
24
|
|
|
{ |
25
|
|
|
if ($request->has('limit')) { |
26
|
|
|
return $request->limit; |
27
|
|
|
} else { |
28
|
|
|
return config('larastart.limit'); |
29
|
|
|
} |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Formats the dates to readable formats. |
34
|
|
|
* |
35
|
|
|
* @param $date |
36
|
|
|
* @return array |
37
|
|
|
*/ |
38
|
|
|
public static function getDates($date) |
39
|
|
|
{ |
40
|
|
|
if (! $date instanceof Carbon) { |
41
|
|
|
$date = Carbon::parse($date); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
return [ |
45
|
|
|
'approx' => $date->diffForHumans(), |
46
|
|
|
'formatted' => $date->format('D d M Y'), |
47
|
|
|
'exact' => $date, |
48
|
|
|
'time' => $date->format('H:i'), |
49
|
|
|
]; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Gets the filename. |
54
|
|
|
* |
55
|
|
|
* @param string $fullname |
56
|
|
|
* @return string |
57
|
|
|
*/ |
58
|
|
|
public static function getFileName(string $fullname): string |
59
|
|
|
{ |
60
|
|
|
$newName = \str_replace(self::$strRlc, '/', $fullname); |
61
|
|
|
$pieces = \explode('/', $newName); |
62
|
|
|
|
63
|
|
|
return $pieces[count($pieces) - 1]; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Get the directory name. |
68
|
|
|
* |
69
|
|
|
* @param string $fullname |
70
|
|
|
* @param bool $includeSlash |
71
|
|
|
* @return string |
72
|
|
|
*/ |
73
|
|
|
public static function getDirName(string $fullname, bool $includeSlash = false): string |
74
|
|
|
{ |
75
|
|
|
$newName = \str_replace(self::$strRlc, '/', $fullname); |
76
|
|
|
$pieces = \explode('/', $newName); |
77
|
|
|
array_pop($pieces); |
78
|
|
|
if (count($pieces)) { |
79
|
|
|
if ($includeSlash) { |
80
|
|
|
return '\\'.implode('\\', $pieces); |
81
|
|
|
} else { |
82
|
|
|
return implode('\\', $pieces); |
83
|
|
|
} |
84
|
|
|
} else { |
85
|
|
|
return ''; |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public static function getModelNamespace(string $modelName): string |
90
|
|
|
{ |
91
|
|
|
return 'App\\'.\str_replace(self::$strRlc, '\\', $modelName); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|