Skip to content

Commit 73738cb

Browse files
committed
- Added icons to allowed mime types
1 parent 8c09fd9 commit 73738cb

File tree

3 files changed

+286
-17
lines changed

3 files changed

+286
-17
lines changed

docs/modules/allowed-mimetypes.md

Lines changed: 80 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,9 @@ class ProductsRequest extends FormRequest
4646

4747
The `AllowedMimeTypes` class has the following available static methods.
4848

49-
### getAllowedMimeTypes(string|array $type): array
49+
### getAllowedMimeTypes(string|array $type = ''): array
5050

51-
Given a file type, or an array of types, returns an array of allowed mime types.
51+
Given a file type, or an array of types, returns an array of allowed mime types. If no type is provided, returns all allowed mimetypes.
5252

5353
```php
5454
$mimetypes = AllowedMimeTypes::getAllowedMimeTypes('image');
@@ -74,9 +74,79 @@ use Javaabu\Helpers\AllowedMimeTypes;
7474
AllowedMimeTypes::registerMimeTypes('word', ['application/vnd.ms-word', 'text/plain']);
7575
```
7676

77-
### getAllowedMimeTypesString(string|array $type, string $separator = ','): string
77+
### getDefaultIconPack(): string
7878

79-
Given a file type, or an array of types, returns a string of allowed mime types separated by the given delimiter.
79+
Returns the default icon pack.
80+
81+
```php
82+
$icon_pack = AllowedMimeTypes::getDefaultIconPack();
83+
// fontawesome
84+
```
85+
86+
To change the default icon pack, you can call the `AllowedMimeTypes::setDefaultIconPack` method in the `boot` method of your`App\Providers\AppServiceProvider` class.
87+
Out of the box, the package supports `fontawesome` and `material` icon packs.
88+
89+
```php
90+
use Javaabu\Helpers\AllowedMimeTypes;
91+
92+
AllowedMimeTypes::setDefaultIconPack('material');
93+
```
94+
95+
### getIconPrefix(string $icon_pack = ''): string
96+
97+
Returns the icon prefix for the given icon pack. If no icon pack is given, returns the prefix for the default icon pack.
98+
99+
```php
100+
$prefix = AllowedMimeTypes::getIconPrefix('material');
101+
// zmdi zmdi-
102+
```
103+
104+
To change the icon prefix for a given icon pack, you can call the `AllowedMimeTypes::registerIconPrefix` method in the `boot` method of your`App\Providers\AppServiceProvider` class.
105+
106+
```php
107+
use Javaabu\Helpers\AllowedMimeTypes;
108+
109+
AllowedMimeTypes::registerIconPrefix('fontawesome', 'fa-light fa');
110+
```
111+
112+
### getIcon(string $mime_type, string $icon_pack = '', bool $with_prefix = false): string
113+
114+
Returns the icon for the given mimetype. If no icon pack is given, uses the default icon pack. If an icon is not defined directly for the given mime type, then it will fallback to the file type of the mime type. If no icon is defined for the file type as well, then will fallback to the `default` icon of the icon pack.
115+
116+
```php
117+
AllowedMimeTypes::getIcon('word'); // file-word
118+
AllowedMimeTypes::getIcon('application/msword'); // file-word
119+
AllowedMimeTypes::getIcon('application/msword', 'material'); // file-text
120+
AllowedMimeTypes::getIcon('application/msword', with_prefix: true)); // fa-regular fa-file-word
121+
AllowedMimeTypes::getIcon('missing-mimetype')); // file
122+
```
123+
124+
To register your own icons for a given icon pack, you can call the `AllowedMimeTypes::registerIcons` method in the `boot` method of your`App\Providers\AppServiceProvider` class. This will merge any existing with your new icons. If you want to fully override the icons for a given icon pack, then you can set the `merge` option to false.
125+
126+
```php
127+
use Javaabu\Helpers\AllowedMimeTypes;
128+
129+
// add to icons
130+
AllowedMimeTypes::registerIcons('fontawesome', [
131+
'text/javascript' => 'file-code'
132+
]);
133+
134+
// fully override icons
135+
AllowedMimeTypes::registerIcons('material', [
136+
'default' => 'file',
137+
'text/javascript' => 'code'
138+
]);
139+
140+
// register your own icon pack
141+
AllowedMimeTypes::registerIcons('feather', [
142+
'default' => 'file',
143+
'text/javascript' => 'code'
144+
]);
145+
```
146+
147+
### getAllowedMimeTypesString(string|array $type = '', string $separator = ','): string
148+
149+
Given a file type, or an array of types, returns a string of allowed mime types separated by the given delimiter. If no type is given, returns all mimetypes separated by given delimiter.
80150

81151
```php
82152
$mimetypes = AllowedMimeTypes::getAllowedMimeTypesString('image');
@@ -85,17 +155,17 @@ $mimetypes = AllowedMimeTypes::getAllowedMimeTypesString('image');
85155
*/
86156
```
87157

88-
### isAllowedMimeType(string $mime_type, array|string $type): bool
158+
### isAllowedMimeType(string $mime_type, array|string $type = ''): bool
89159

90-
Checks whether a given mimetype is allowed for the given file type(s).
160+
Checks whether a given mimetype is allowed for the given file type(s). If no type is given, checks if the given mimetype is allowed.
91161

92162
```php
93163
AllowedMimeTypes::isAllowedMimeType('audio/mp3', 'image'); // returns false
94164
```
95165

96-
### getMaxFileSize(string|array $types): int
166+
### getMaxFileSize(string|array $types = ''): int
97167

98-
Returns the max allowed file size in KB for the given file type. If an array of file types is given, it will return the maximum size allowed from all the given file types. By default, for each given file type, the method will look if a `'max_<type>_file_size''` setting is available. Otherwise, it will fallback to the `'max_upload_file_size'` setting.
168+
Returns the max allowed file size in KB for the given file type. If an array of file types is given or no type is given, it will return the maximum size allowed from all the file types. By default, for each given file type, the method will look if a `'max_<type>_file_size''` setting is available. Otherwise, it will fallback to the `'max_upload_file_size'` setting.
99169

100170
```php
101171
AllowedMimeTypes::getMaxFileSize('image');
@@ -114,9 +184,9 @@ AllowedMimeTypes::registerFileSizeSettings('max_audio_visual_file_size', ['video
114184

115185
In the above example, both `video` and `audio` files will use the `'max_audio_visual_file_size'` setting.
116186

117-
### getValidationRule(string|array $type, bool $as_array = false, ?int $max_size = null): array|string
187+
### getValidationRule(string|array $type = '', bool $as_array = false, ?int $max_size = null): array|string
118188

119-
Generates the validation rule for the given file type. Optionally pass a custom file size.
189+
Generates the validation rule for the given file type. If no type is given, will allow all allowed mime types. Optionally pass a custom file size.
120190

121191
### getType(string $mime_type): ?string
122192

src/Media/AllowedMimeTypes.php

Lines changed: 151 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,43 @@
1010

1111
abstract class AllowedMimeTypes
1212
{
13+
protected static string $default_icon_pack = 'fontawesome';
14+
15+
protected static array $icon_prefixes = [
16+
'fontawesome' => 'fa-regular fa-',
17+
'material' => 'zmdi zmdi-',
18+
];
19+
20+
protected static array $icons = [
21+
'fontawesome' => [
22+
'default' => 'file',
23+
'excel' => 'file-excel',
24+
'word' => 'file-word',
25+
'powerpoint' => 'file-powerpoint',
26+
'audio' => 'file-audio',
27+
'video' => 'file-video',
28+
'image' => 'file-image',
29+
'pdf' => 'file-pdf',
30+
'text' => 'file-lines',
31+
'json' => 'file-code',
32+
'zip' => 'file-zip',
33+
],
34+
35+
'material' => [
36+
'default' => 'file',
37+
'excel' => 'border-all',
38+
'word' => 'file-text',
39+
'powerpoint' => 'slideshow',
40+
'audio' => 'file-audio',
41+
'video' => 'collection-video',
42+
'image' => 'collection-image',
43+
'pdf' => 'collection-pdf',
44+
'text' => 'file-text',
45+
'json' => 'code',
46+
'zip' => 'archive',
47+
],
48+
];
49+
1350
/**
1451
* @var array
1552
*/
@@ -46,12 +83,6 @@ abstract class AllowedMimeTypes
4683
'image/vnd.microsoft.icon'
4784
],
4885

49-
'document' => [
50-
'application/pdf',
51-
'image/jpeg',
52-
'image/png',
53-
],
54-
5586
'video' => [
5687
'video/webm',
5788
'video/ogg',
@@ -72,7 +103,53 @@ abstract class AllowedMimeTypes
72103
'application/vnd.ms-excel.addin.macroEnabled.12',
73104
'application/vnd.ms-excel.sheet.binary.macroEnabled.12',
74105
'text/csv',
75-
]
106+
],
107+
108+
'word' => [
109+
'application/msword',
110+
'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
111+
'application/vnd.openxmlformats-officedocument.wordprocessingml.template',
112+
'application/vnd.ms-word.document.macroEnabled.12',
113+
'application/vnd.ms-word.template.macroEnabled.12',
114+
],
115+
116+
'powerpoint' => [
117+
'application/vnd.ms-powerpoint',
118+
'application/vnd.openxmlformats-officedocument.presentationml.presentation',
119+
'application/vnd.openxmlformats-officedocument.presentationml.template',
120+
'application/vnd.openxmlformats-officedocument.presentationml.slideshow',
121+
'application/vnd.ms-powerpoint.addin.macroEnabled.12',
122+
'application/vnd.ms-powerpoint.presentation.macroEnabled.12',
123+
'application/vnd.ms-powerpoint.template.macroEnabled.12',
124+
'application/vnd.ms-powerpoint.slideshow.macroEnabled.12'
125+
],
126+
127+
'pdf' => [
128+
'application/pdf',
129+
],
130+
131+
'text' => [
132+
'text/plain'
133+
],
134+
135+
'json' => [
136+
'application/json'
137+
],
138+
139+
'document' => [
140+
'application/pdf',
141+
'image/jpeg',
142+
'image/png',
143+
],
144+
145+
'zip' => [
146+
'application/zip',
147+
'application/octet-stream',
148+
'application/x-zip-compressed',
149+
'multipart/x-zip',
150+
],
151+
152+
76153
];
77154

78155
/**
@@ -266,6 +343,73 @@ abstract class AllowedMimeTypes
266343
'text/x-scriptzsh' => 'zsh',
267344
];
268345

346+
/**
347+
* Set the default icon pack
348+
*/
349+
public static function setDefaultIconPack(string $icon_pack): void
350+
{
351+
self::$default_icon_pack = $icon_pack;
352+
}
353+
354+
/**
355+
* Get the default icon pack
356+
*/
357+
public static function getDefaultIconPack(): string
358+
{
359+
return self::$default_icon_pack;
360+
}
361+
362+
/**
363+
* Get the icon prefix
364+
*/
365+
public static function getIconPrefix(string $icon_pack = ''): string
366+
{
367+
if (! $icon_pack) {
368+
$icon_pack = self::getDefaultIconPack();
369+
}
370+
371+
return self::$icon_prefixes[$icon_pack] ?? '';
372+
}
373+
374+
/**
375+
* Get the icon based on mime type
376+
*/
377+
public static function getIcon(string $mime_type, string $icon_pack = '', bool $with_prefix = false): string
378+
{
379+
if (! $icon_pack) {
380+
$icon_pack = self::getDefaultIconPack();
381+
}
382+
383+
// get icon pack
384+
$icons = self::$icons[$icon_pack] ?? [];
385+
386+
// try using mime type
387+
$icon = $icons[$mime_type] ?? '';
388+
389+
// try using type
390+
if (! $icon) {
391+
$icon = $icons[self::getType($mime_type)] ?? '';
392+
}
393+
394+
$icon = $icon ?: ($icons['default'] ?? '');
395+
396+
return $with_prefix && $icon ? self::getIconPrefix($icon_pack) . $icon : $icon;
397+
}
398+
399+
public static function registerIcons(string $icon_pack, array $icons, bool $merge = true): void
400+
{
401+
$pack_icons = self::$icons[$icon_pack] ?? [];
402+
403+
$pack_icons = $merge ? array_merge($pack_icons, $icons) : $icons;
404+
405+
self::$icons[$icon_pack] = $pack_icons;
406+
}
407+
408+
public static function registerIconPrefix(string $icon_pack, string $prefix): void
409+
{
410+
self::$icon_prefixes[$icon_pack] = $prefix;
411+
}
412+
269413
/**
270414
* Get all allowed types
271415
*

tests/Feature/AllowedMimeTypesTest.php

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,61 @@ protected function setUp(): void
1616
$this->app['config']->set('defaults.max_image_file_size', 1024 * 2);
1717
}
1818

19+
#[Test]
20+
public function it_can_get_default_icon_pack(): void
21+
{
22+
$this->assertEquals('fontawesome', AllowedMimeTypes::getDefaultIconPack());
23+
}
24+
25+
#[Test]
26+
public function it_can_change_the_default_icon_pack(): void
27+
{
28+
AllowedMimeTypes::setDefaultIconPack('material');
29+
30+
$this->assertEquals('material', AllowedMimeTypes::getDefaultIconPack());
31+
}
32+
33+
#[Test]
34+
public function it_can_get_the_icon_prefix(): void
35+
{
36+
$this->assertEquals('fa-regular fa-', AllowedMimeTypes::getIconPrefix());
37+
$this->assertEquals('zmdi zmdi-', AllowedMimeTypes::getIconPrefix('material'));
38+
}
39+
40+
#[Test]
41+
public function it_can_register_the_icon_prefix(): void
42+
{
43+
AllowedMimeTypes::registerIconPrefix('fontawesome', 'fa-light fa-');
44+
45+
$this->assertEquals('fa-light fa-', AllowedMimeTypes::getIconPrefix());
46+
}
47+
48+
#[Test]
49+
public function it_can_get_the_icon(): void
50+
{
51+
$this->assertEquals('file-word', AllowedMimeTypes::getIcon('word'));
52+
$this->assertEquals('file-word', AllowedMimeTypes::getIcon('application/msword'));
53+
$this->assertEquals('file-text', AllowedMimeTypes::getIcon('application/msword', 'material'));
54+
$this->assertEquals('fa-regular fa-file-word', AllowedMimeTypes::getIcon('application/msword', with_prefix: true));
55+
$this->assertEquals('file', AllowedMimeTypes::getIcon('test'));
56+
}
57+
58+
#[Test]
59+
public function it_can_register_icons(): void
60+
{
61+
AllowedMimeTypes::registerIcons('fontawesome', [
62+
'test' => 'file-test'
63+
]);
64+
65+
$this->assertEquals('file-test', AllowedMimeTypes::getIcon('test'));
66+
67+
AllowedMimeTypes::registerIcons('fontawesome', [
68+
'default' => 'file'
69+
], false);
70+
71+
$this->assertEquals('file', AllowedMimeTypes::getIcon('word'));
72+
}
73+
1974
#[Test]
2075
public function it_can_get_the_max_file_size_based_on_multiple_file_types(): void
2176
{

0 commit comments

Comments
 (0)