Skip to content

Commit 79f8413

Browse files
Merge pull request #9 from MaximVanhove/feature/new-style-names
Feature/new style names
2 parents 8165e4a + af73baf commit 79f8413

File tree

7 files changed

+131
-43
lines changed

7 files changed

+131
-43
lines changed

readme.md

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Font Awesome SVG - PHP
22

3-
A PHP class that can be used to add [Font Awesome 5+](https://fontawesome.com/)'s SVG icons inline without Javascript.
3+
A PHP class that can be used to add [Font Awesome 6+](https://fontawesome.com/)'s SVG icons inline without Javascript.
44

55
## Installation
66

@@ -26,37 +26,37 @@ Or you can download the `FontAwesomeSVG.php` file and include it manually.
2626
// $dir = directory where SVG files are
2727
$FA = new FontAwesomeSVG($dir);
2828

29-
echo $FA->get_svg('fas fa-file');
29+
echo $FA->get_svg('fa-solid fa-file');
3030
```
3131

3232
Add custom classes:
3333

3434
```php
35-
echo $FA->get_svg('fas fa-file', ['class' => 'my-custom-class another-class']);
35+
echo $FA->get_svg('fa-solid fa-file', ['class' => 'my-custom-class another-class']);
3636
```
3737

3838
Remove default class `.svg-inline--fa`:
3939

4040
```php
41-
echo $FA->get_svg('fas fa-file', ['default_class' => false]);
41+
echo $FA->get_svg('fa-solid fa-file', ['default_class' => false]);
4242
```
4343

4444
Change `<path>` fill (default is `currentColor`):
4545

4646
```php
47-
echo $FA->get_svg('fas fa-file', ['fill' => '#f44336']);
47+
echo $FA->get_svg('fa-solid fa-file', ['fill' => '#f44336']);
4848
```
4949

5050
Add `<title></title>`:
5151

5252
```php
53-
echo $FA->get_svg('fas fa-file', ['title' => 'My accessible icon']);
53+
echo $FA->get_svg('fa-solid fa-file', ['title' => 'My accessible icon']);
5454
```
5555

5656
Multiple options at once:
5757

5858
```php
59-
echo $FA->get_svg('fas fa-file', [
59+
echo $FA->get_svg('fa-solid fa-file', [
6060
'class' => 'my-custom-class another-class',
6161
'default_class' => false,
6262
'title' => 'My title',
@@ -68,7 +68,7 @@ echo $FA->get_svg('fas fa-file', [
6868
Customise duotone icons:
6969

7070
```php
71-
echo $FA->get_svg('fad fa-laugh-wink', [
71+
echo $FA->get_svg('fa-duotone fa-laugh-wink', [
7272
'primary' => [
7373
'fill' => '#e64980',
7474
],
@@ -95,6 +95,16 @@ echo $FA->get_svg('fad fa-laugh-wink', [
9595

9696
> Requires **v5.10.0** or greater, and a FontAwesome Pro license
9797
98+
## Sharp
99+
100+
> Requires **v6.4.0** or greater, and a FontAwesome Pro license
101+
102+
```php
103+
echo $FA->get_svg('fa-sharp fa-light fa-file');
104+
echo $FA->get_svg('fa-sharp fa-regular fa-file');
105+
echo $FA->get_svg('fa-sharp fa-solid fa-file');
106+
```
107+
98108
### options
99109

100110
If `inline_style` is enabled, the value of `fill` and `opacity` are also used in the inline style on `<svg>` tag.
@@ -149,6 +159,24 @@ echo $FA->get_svg('fad fa-laugh-wink', [
149159
]);
150160
```
151161

162+
## Aliases
163+
164+
The short aliases from version 5 are still supported
165+
166+
```php
167+
echo $FA->get_svg('fab fa-twitter');
168+
echo $FA->get_svg('fad fa-file');
169+
echo $FA->get_svg('fal fa-file');
170+
echo $FA->get_svg('far fa-file');
171+
echo $FA->get_svg('fas fa-file');
172+
173+
// And the new shorthands for thin and sharp
174+
echo $FA->get_svg('fat fa-file'); // thin
175+
echo $FA->get_svg('fasl fa-file'); // sharp-light
176+
echo $FA->get_svg('fasr fa-file'); // sharp-regular
177+
echo $FA->get_svg('fass fa-file'); // sharp-solid
178+
```
179+
152180
## Accessibility
153181

154182
The below is implemented based on:
@@ -169,7 +197,7 @@ The below is implemented based on:
169197
You can set a `<title>`, an `id` for the `<title>` and the `aria-labelledby` attribute will be added automatically:
170198

171199
```php
172-
echo $FA->get_svg('fas fa-file', [
200+
echo $FA->get_svg('fa-solid fa-file', [
173201
'title' => 'File',
174202
'title_id' => 'file-id',
175203
]);
@@ -186,7 +214,7 @@ echo $FA->get_svg('fas fa-file', [
186214
You can add any aria-\* attribute to the SVG tag:
187215

188216
```php
189-
echo $FA->get_svg('fas fa-file', [
217+
echo $FA->get_svg('fa-solid fa-file', [
190218
'aria-label' => 'File',
191219
]);
192220
```
@@ -200,7 +228,7 @@ echo $FA->get_svg('fas fa-file', [
200228
`aria-hidden="true"` is added to the SVG tag by default unless `<title id="">` (and `aria-labelledby`) or `aria-label` is set.
201229

202230
```php
203-
echo $FA->get_svg('fas fa-file');
231+
echo $FA->get_svg('fa-solid fa-file');
204232
```
205233

206234
```html

src/FontAwesomeSVG.php

Lines changed: 57 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -174,13 +174,14 @@ public function get_svg($id, $opts=[]) {
174174
public function get_icon_details($id) {
175175
$icon = array();
176176

177-
$id = explode(' ', $id);
178-
$dir = $this->get_icon_dir($id[0]);
179-
$filename = $this->get_icon_filename($id[1]);
177+
$classes = explode(' ', $id);
178+
$dir = $this->get_icon_dir($classes);
179+
$filename = $this->get_icon_filename($classes, $dir);
180+
$filepath = $this->get_icon_filepath($dir, $filename);
180181

181182
$icon['dir'] = $dir;
182183
$icon['filename'] = $filename;
183-
$icon['filepath'] = str_replace('/', DIRECTORY_SEPARATOR, "$this->svg_dir/$dir/$filename.svg");
184+
$icon['filepath'] = $filepath;
184185

185186
if(!is_file($icon['filepath'])) {
186187
throw new Exception('File ' . $icon['filepath'] . ' does not exist.');
@@ -198,30 +199,33 @@ public function get_icon_details($id) {
198199
* @param string $style
199200
* @return string
200201
*/
201-
public function get_icon_dir($style) {
202-
switch($style) {
203-
case 'far':
204-
$dir = 'regular';
205-
break;
202+
public function get_icon_dir($classes) {
203+
if (in_array('fa-sharp', $classes)) {
204+
if (in_array('fa-regular', $classes)) return 'sharp-regular';
205+
if (in_array('fa-light', $classes)) return 'sharp-light';
206+
if (in_array('fa-solid', $classes)) return 'sharp-solid';
207+
}
206208

207-
case 'fal':
208-
$dir = 'light';
209-
break;
209+
if (in_array('fasr', $classes)) return 'sharp-regular';
210+
if (in_array('fasl', $classes)) return 'sharp-light';
211+
if (in_array('fass', $classes)) return 'sharp-solid';
210212

211-
case 'fab':
212-
$dir = 'brands';
213-
break;
213+
if (in_array('far', $classes)) return 'regular';
214+
if (in_array('fa-regular', $classes)) return 'regular';
214215

215-
case 'fad':
216-
$dir = 'duotone';
217-
break;
216+
if (in_array('fal', $classes)) return 'light';
217+
if (in_array('fa-light', $classes)) return 'light';
218218

219-
case 'fas':
220-
default:
221-
$dir = 'solid';
222-
}
219+
if (in_array('fab', $classes)) return 'brands';
220+
if (in_array('fa-brands', $classes)) return 'brands';
223221

224-
return $dir;
222+
if (in_array('fad', $classes)) return 'duotone';
223+
if (in_array('fa-duotone', $classes)) return 'duotone';
224+
225+
if (in_array('fat', $classes)) return 'thin';
226+
if (in_array('fa-thin', $classes)) return 'thin';
227+
228+
return 'solid';
225229
}
226230

227231

@@ -230,10 +234,36 @@ public function get_icon_dir($style) {
230234
/**
231235
* Get the icon's SVG file name
232236
*
233-
* @param string $icon_name
237+
* @param array $classes
238+
* @param string $dir
239+
* @return string
240+
*/
241+
public function get_icon_filename($classes, $dir) {
242+
foreach ($classes as $class) {
243+
$filename = str_replace('fa-', '', $class);
244+
$path = $this->get_icon_filepath($dir, $filename);
245+
246+
if (is_file($path)) {
247+
return $filename;
248+
}
249+
}
250+
251+
$id = join(' ', $classes);
252+
253+
throw new Exception("No icon found for '$id'");
254+
}
255+
256+
257+
258+
259+
/**
260+
* Get the icon's SVG file path
261+
*
262+
* @param string $dir
263+
* @param string $filename
234264
* @return string
235265
*/
236-
public function get_icon_filename($icon_name) {
237-
return str_replace('fa-', '', $icon_name);
266+
public function get_icon_filepath($dir, $filename) {
267+
return str_replace('/', DIRECTORY_SEPARATOR, "$this->svg_dir/$dir/$filename.svg");
238268
}
239-
}
269+
}
Lines changed: 1 addition & 0 deletions
Loading
Lines changed: 1 addition & 0 deletions
Loading
Lines changed: 1 addition & 0 deletions
Loading

tests/Fixtures/icons/thin/test.svg

Lines changed: 1 addition & 0 deletions
Loading

tests/Unit/FontAwesomeSVGTest.php

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,24 @@ final class FontAwesomeSVGTest extends TestCase
1111
static public function icons(): array
1212
{
1313
return [
14-
['fab fa-test'],
15-
['fad fa-test'],
16-
['fal fa-test'],
17-
['far fa-test'],
18-
['fas fa-test'],
14+
['fab fa-test', 'brands', 'test'],
15+
['fad fa-test', 'duotone', 'test'],
16+
['fal fa-test', 'light', 'test'],
17+
['far fa-test', 'regular', 'test'],
18+
['fas fa-test', 'solid', 'test'],
19+
['fat fa-test', 'thin', 'test'],
20+
['fa-brands fa-test', 'brands', 'test'],
21+
['fa-duotone fa-test', 'duotone', 'test'],
22+
['fa-light fa-test', 'light', 'test'],
23+
['fa-regular fa-test', 'regular', 'test'],
24+
['fa-solid fa-test', 'solid', 'test'],
25+
['fa-thin fa-test', 'thin', 'test'],
26+
['fa-sharp fa-light fa-test', 'sharp-light', 'test'],
27+
['fa-sharp fa-regular fa-test', 'sharp-regular', 'test'],
28+
['fa-sharp fa-solid fa-test', 'sharp-solid', 'test'],
29+
['fasl fa-test', 'sharp-light', 'test'],
30+
['fasr fa-test', 'sharp-regular', 'test'],
31+
['fass fa-test', 'sharp-solid', 'test'],
1932
];
2033
}
2134

@@ -173,4 +186,17 @@ public function test_it_can_remove_duotone_inline_styles(): void
173186
$this->assertStringNotContainsString('--fa-secondary-color', $svg);
174187
$this->assertStringNotContainsString('--fa-secondary-opacity', $svg);
175188
}
189+
190+
/**
191+
* @dataProvider icons
192+
*/
193+
public function test_it_can_get_the_icons($icon, $dir, $filename): void
194+
{
195+
$fa = $this->createInstance();
196+
197+
$details = $fa->get_icon_details($icon);
198+
199+
$this->assertStringContainsString($dir, $details['dir']);
200+
$this->assertStringContainsString($filename, $details['filename']);
201+
}
176202
}

0 commit comments

Comments
 (0)