1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of ocubom/twig-svg-extension |
5
|
|
|
* |
6
|
|
|
* © Oscar Cubo Medina <https://ocubom.github.io> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Ocubom\Twig\Extension\Svg\Library\FontAwesome; |
13
|
|
|
|
14
|
|
|
use Ocubom\Twig\Extension\Svg\Exception\FileNotFoundException; |
15
|
|
|
use Ocubom\Twig\Extension\Svg\FinderInterface; |
16
|
|
|
use Ocubom\Twig\Extension\Svg\Library\FontAwesome; |
17
|
|
|
|
18
|
|
|
class Finder implements FinderInterface |
19
|
|
|
{ |
20
|
|
|
private FinderInterface $finder; |
21
|
|
|
|
22
|
2 |
|
public function __construct(FinderInterface $finder) |
23
|
|
|
{ |
24
|
2 |
|
$this->finder = $finder; |
25
|
|
|
} |
26
|
|
|
|
27
|
2 |
|
public function resolve(string $ident): \SplFileInfo |
28
|
|
|
{ |
29
|
2 |
|
$tokens = preg_split('@\s+@', mb_strtolower($ident)); |
30
|
2 |
|
$tokens[] = FontAwesome::DEFAULT_PREFIX; // Add default prefix as fallback |
31
|
|
|
|
32
|
2 |
|
foreach ($tokens as $prefix) { |
33
|
2 |
|
$style = FontAwesome::PREFIXES[$prefix] ?? null; |
34
|
2 |
|
if (null === $style) { |
35
|
1 |
|
continue; // Ignore tokens that are *not* a known prefix |
36
|
|
|
} |
37
|
|
|
|
38
|
2 |
|
foreach ($tokens as $name) { |
39
|
2 |
|
if (isset(FontAwesome::PREFIXES[$name])) { |
40
|
1 |
|
continue; // Ignore tokens that are a known prefix |
41
|
|
|
} |
42
|
|
|
|
43
|
2 |
|
$name = (0 === mb_strpos($name, 'fa-')) ? mb_substr($name, 3) : $name; |
44
|
2 |
|
if (empty($name)) { |
45
|
|
|
continue; // Ignore empty names |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
try { |
49
|
2 |
|
return $this->finder->resolve(sprintf('%s/%s.svg', $style, $name)); |
50
|
|
|
} catch (FileNotFoundException $err) { |
51
|
|
|
// Just ignore |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
// Could not resolve |
57
|
|
|
throw new FileNotFoundException(sprintf( |
58
|
|
|
'Could not found a FontAwesome icon for "%s" on "%s".', |
59
|
|
|
$ident, |
60
|
|
|
(string) $this, |
61
|
|
|
)); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function __toString(): string |
65
|
|
|
{ |
66
|
|
|
return (string) $this->finder; |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|