|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace tkanstantsin\fileupload\formatter\icon; |
|
5
|
|
|
|
|
6
|
|
|
/** |
|
7
|
|
|
* Class IconGenerator searches icon by file extension. |
|
8
|
|
|
*/ |
|
9
|
|
|
class IconGenerator |
|
10
|
|
|
{ |
|
11
|
|
|
public const ICON_DEFAULT = 1; |
|
12
|
|
|
public const ICON_TEXT = 2; |
|
13
|
|
|
public const ICON_DOC = 3; |
|
14
|
|
|
public const ICON_EXCEL = 4; |
|
15
|
|
|
public const ICON_POWER_POINT = 5; |
|
16
|
|
|
public const ICON_PDF = 6; |
|
17
|
|
|
public const ICON_IMAGE = 7; |
|
18
|
|
|
public const ICON_ARCHIVE = 8; |
|
19
|
|
|
public const ICON_AUDIO = 9; |
|
20
|
|
|
public const ICON_VIDEO = 10; |
|
21
|
|
|
|
|
22
|
|
|
private const TYPE_TO_REGEX = [ |
|
23
|
|
|
self::ICON_DEFAULT => null, |
|
24
|
|
|
// docs |
|
25
|
|
|
self::ICON_TEXT => 'txt', |
|
26
|
|
|
self::ICON_DOC => 'doc[x]?|odt', |
|
27
|
|
|
self::ICON_EXCEL => 'xls[xb]?|ods', |
|
28
|
|
|
self::ICON_POWER_POINT => 'ptt[x]?|odp', |
|
29
|
|
|
self::ICON_PDF => 'pdf', |
|
30
|
|
|
self::ICON_IMAGE => 'jpe?g|png|gif', |
|
31
|
|
|
// archives |
|
32
|
|
|
self::ICON_ARCHIVE => 'zip|rar|7zip', |
|
33
|
|
|
// multimedia |
|
34
|
|
|
self::ICON_AUDIO => 'mp3', |
|
35
|
|
|
self::ICON_VIDEO => 'mp4', |
|
36
|
|
|
]; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @var string |
|
40
|
|
|
*/ |
|
41
|
|
|
private $iconPrefix; |
|
42
|
|
|
/** |
|
43
|
|
|
* @var array |
|
44
|
|
|
*/ |
|
45
|
|
|
private $iconSet; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @param string $class |
|
49
|
|
|
* @return null|IconGenerator |
|
50
|
|
|
*/ |
|
51
|
1 |
|
public static function build($class): ?self |
|
52
|
|
|
{ |
|
53
|
|
|
/* @var ElusiveIcons|FontAwesome $iconSetClass */ |
|
54
|
1 |
|
$iconSetClass = $class ?? FontAwesome::class; |
|
55
|
|
|
|
|
56
|
1 |
|
return new self($iconSetClass::PREFIX, $iconSetClass::ICON_SET); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* IconGenerator constructor. |
|
61
|
|
|
* @param string $iconPrefix |
|
62
|
|
|
* @param array $iconSet |
|
63
|
|
|
*/ |
|
64
|
1 |
|
public function __construct(string $iconPrefix, array $iconSet) |
|
65
|
|
|
{ |
|
66
|
1 |
|
$this->iconPrefix = $iconPrefix; |
|
67
|
1 |
|
$this->iconSet = $iconSet; |
|
68
|
|
|
|
|
69
|
1 |
|
$this->init(); |
|
70
|
1 |
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Initialize object |
|
74
|
|
|
*/ |
|
75
|
1 |
|
public function init(): void |
|
76
|
|
|
{ |
|
77
|
|
|
// check if $iconSet implement all available icons from self::TYPE_TO_REGEX |
|
78
|
1 |
|
$diff1 = array_keys(\array_diff_assoc($this->iconSet, self::TYPE_TO_REGEX)); |
|
79
|
1 |
|
$diff2 = array_keys(\array_diff_assoc(self::TYPE_TO_REGEX, $this->iconSet)); |
|
80
|
1 |
|
if ($diff1 === $diff2) { |
|
81
|
|
|
\trigger_error('Icon set doesn\'t implement all range of icons.'); |
|
82
|
|
|
} |
|
83
|
1 |
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Returns suitable css class for icon by extension. |
|
87
|
|
|
* @param null|string $extension |
|
88
|
|
|
* @return string returns default icon if nothing found |
|
89
|
|
|
*/ |
|
90
|
|
|
public function getIcon(?string $extension): ?string |
|
91
|
|
|
{ |
|
92
|
|
|
$iconClass = $this->iconSet[$this->getIconType($extension)] ?? null; |
|
93
|
|
|
|
|
94
|
|
|
return $iconClass !== null |
|
95
|
|
|
? $this->iconPrefix . ' ' . $iconClass |
|
96
|
|
|
: null; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Searches icon type by it's extension |
|
101
|
|
|
* @param null|string $extension |
|
102
|
|
|
* @return string |
|
103
|
|
|
*/ |
|
104
|
|
|
public function getIconType(?string $extension): string |
|
105
|
|
|
{ |
|
106
|
|
|
if ($extension !== null && $extension !== '') { |
|
107
|
|
|
foreach (self::TYPE_TO_REGEX as $type => $regex) { |
|
108
|
|
|
if ($regex === null) { |
|
109
|
|
|
continue; |
|
110
|
|
|
} |
|
111
|
|
|
if (($regex === $extension && preg_match('/[a-z0-9]+/', $extension)) |
|
112
|
|
|
|| preg_match("/$regex/", $extension) |
|
113
|
|
|
) { |
|
114
|
|
|
return $type; |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
return self::ICON_DEFAULT; |
|
120
|
|
|
} |
|
121
|
|
|
} |