Skip to content
This repository was archived by the owner on Jul 7, 2025. It is now read-only.

Commit eace6ac

Browse files
Merge branch 'modern-language-changes' into v2.0
2 parents e8778b3 + f1a2e90 commit eace6ac

File tree

4 files changed

+46
-46
lines changed

4 files changed

+46
-46
lines changed

README.md

Lines changed: 14 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -30,19 +30,17 @@ require 'vendor/autoload.php';
3030

3131
### Get an icon
3232

33-
Icons echo by default.
34-
3533
```php
3634
<?php
3735
require 'vendor/autoload.php';
38-
$icons = new Feather\Icons;
36+
$icons = new Feather\Icons();
3937
?>
4038

4139
<!-- Display the 'anchor' icon !-->
42-
<?php $icons->get('feather'); ?>
40+
<?php echo $icons->get('feather'); ?>
4341

4442
<!-- Get creative! !-->
45-
<button class="icon-button">Learn More <?php $icons->get('arrow-right'); ?></button>
43+
<button class="icon-button">Learn More <?php echo $icons->get('arrow-right'); ?></button>
4644
```
4745

4846
### Get an icon with modified properties
@@ -67,45 +65,32 @@ $icons->get('mail');
6765
// <svg ... color="red" stroke-width="3" ... >...</svg>
6866
```
6967

70-
### Get an icon as a string
71-
72-
Set the third argument to `false` to get a string instead of echoing the icon.
73-
74-
```php
75-
$icon_string = $icons->get('activity', array(), false);
76-
// Then do whatever with $icon_string
77-
```
78-
7968
## API
8069

8170
### `Feather\Icons`
8271

8372
Usage:
8473

8574
```php
86-
$icons = new Feather\Icons;
75+
$icons = new Feather\Icons();
8776
```
8877

8978
<br>
9079

91-
### `Feather\Icons->get($name, $attributes = array(), $echo = true)`
80+
### `Feather\Icons->get($name, $attributes = array())`
9281

93-
Gets an icon as svg. Can be echoed (default) or returned as a string. Attributes passed will be merged over the class defaults.
82+
Gets an icon as a string. Attributes passed will be merged over the class defaults.
9483

9584
```php
96-
$icons = new Feather\Icons;
85+
$icons = new Feather\Icons();
9786

98-
// Echo an icon
99-
$icons->get('anchor');
87+
// Get an icon
88+
echo $icons->get('anchor');
10089
// <svg ... >...</svg>
10190

102-
// Echo an icon with modified properties
103-
$icons->get('battery', array('class' => 'fooclass', 'stroke-width' => 1, 'aria-label' => 'Battery icon'));
91+
// Get an icon with modified properties
92+
echo $icons->get('battery', array('class' => 'fooclass', 'stroke-width' => 1, 'aria-label' => 'Battery icon'));
10493
// <svg ... class="feather feather-battery fooclass", stroke-width="1", aria-label="Battery icon" ... >...</svg>
105-
106-
// Get an icon as a string without echoing it
107-
$cloud_icon = $icons->get('cloud', array(), false);
108-
doStuffWith($cloud_icon);
10994
```
11095

11196
#### Arguments
@@ -114,7 +99,6 @@ doStuffWith($cloud_icon);
11499
|------------|-------|---------------------------------------------------------------------------------|
115100
|$name |string |The name of the icon. A full list can be found [here](https://feathericons.com/).|
116101
|$attributes?|array |Attributes to modify/add (see 'Usage' above for example) |
117-
|$echo? |boolean|Whether to echo the icon svg (true) or not (false) |
118102

119103
<br>
120104

@@ -124,7 +108,7 @@ Sets default attributes of the class. These are used as default attributes for t
124108
disable this by setting the `$merge` argument to false, but only do it if you know what you are doing.
125109

126110
```php
127-
$icons = new Feather\Icons;
111+
$icons = new Feather\Icons();
128112

129113
// Set some default attributes (this will be merged with the current defaults in the class)
130114
$icons->setAttributes(array('color' => 'red', 'stroke-width' => 3));
@@ -148,7 +132,7 @@ $icons->get('delete');
148132
Get the current attributes for the class. To set it, use the `setAttributes()` method;
149133

150134
```php
151-
$icons = new Feather\Icons;
135+
$icons = new Feather\Icons();
152136

153137
$attrs = $icons->getAttributes();
154138
```
@@ -160,7 +144,7 @@ $attrs = $icons->getAttributes();
160144
Constant array of default attributes. They are applied to every new `Icons` class. You can use this to reset the attributes of an `Icons` class.
161145

162146
```php
163-
$icons = new Feather\Icons;
147+
$icons = new Feather\Icons();
164148

165149
// Add/modify some default attributes
166150
$icons->setAttributes( ... );
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace Feather\Exception;
4+
5+
class IconNotFoundException extends \Exception
6+
{
7+
8+
}

src/Icons.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Feather;
44

5+
use Feather\Exception\IconNotFoundException;
6+
57
require __DIR__ . '/defaultAttributes.php';
68

79
class Icons
@@ -15,7 +17,7 @@ public function __construct()
1517
$this->icons = require implode(DIRECTORY_SEPARATOR, [dirname(__FILE__), '..', 'resources', 'icons.php']);
1618
}
1719

18-
public function get($name, $attributes = [], $echo = true)
20+
public function get(string $name, array $attributes = []): string
1921
{
2022
if (isset($this->icons[$name])) {
2123
$contents = $this->icons[$name];
@@ -47,26 +49,24 @@ function ($final, $current) use ($attributes) {
4749

4850
$icon = '<svg ' . $dom_attributes . '>' . $contents . '</svg>';
4951

50-
if ($echo) {
51-
echo $icon;
52-
} else {
53-
return $icon;
54-
}
52+
return $icon;
5553
}
5654

57-
return false;
55+
throw new IconNotFoundException(\sprintf('Icon `%s` not found', $name));
5856
}
5957

60-
public function setAttributes($attributes, $merge = true)
58+
public function setAttributes(array $attributes, bool $merge = true): self
6159
{
6260
if ($merge) {
6361
$this->attributes = array_merge($this->attributes, $attributes);
6462
} else {
6563
$this->attributes = $attributes;
6664
}
65+
66+
return $this;
6767
}
6868

69-
public function getAttributes()
69+
public function getAttributes(): array
7070
{
7171
return $this->attributes;
7272
}

tests/FeatherTest.php

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?php
22

3+
use Feather\Exception\IconNotFoundException;
34
use PHPUnit\Framework\TestCase;
45

56
class FeatherTest extends TestCase
@@ -36,27 +37,27 @@ protected function setUp(): void
3637
);
3738
}
3839

39-
public function testIconsHasDefaultAttributes()
40+
public function testIconsHasDefaultAttributes(): void
4041
{
4142
$this->assertEquals(Feather\DEFAULT_ATTRIBUTES, $this->icons->getAttributes());
4243
}
4344

44-
public function testIconDefaultXML()
45+
public function testIconDefaultXML(): void
4546
{
4647
foreach ($this->XMLTestData as $test_data) {
4748
$this->assertXMLStringEqualsXMLString(
4849
$test_data['xml'],
49-
$this->icons->get($test_data['name'], [], false),
50+
$this->icons->get($test_data['name'], []),
5051
'Icon fail: ' . $test_data['name']
5152
);
5253
}
5354
}
5455

55-
public function testIconXMLWithAttributes()
56+
public function testIconXMLWithAttributes(): void
5657
{
5758
$test_data = $this->AttributeTestData;
5859

59-
$icon = $this->icons->get($test_data['name'], $test_data['attributes'], false);
60+
$icon = $this->icons->get($test_data['name'], $test_data['attributes']);
6061

6162
$this->assertXMLStringEqualsXMLString(
6263
$test_data['xml'],
@@ -65,12 +66,12 @@ public function testIconXMLWithAttributes()
6566
);
6667
}
6768

68-
public function testIconXMLWithAttributesFromClass()
69+
public function testIconXMLWithAttributesFromClass(): void
6970
{
7071
$test_data = $this->AttributeTestData;
7172

7273
$this->icons->setattributes($test_data['attributes']);
73-
$icon = $this->icons->get($test_data['name'], [], false);
74+
$icon = $this->icons->get($test_data['name'], []);
7475

7576
$this->assertXMLStringEqualsXMLString(
7677
$test_data['xml'],
@@ -80,4 +81,11 @@ public function testIconXMLWithAttributesFromClass()
8081

8182
$this->icons->setAttributes(Feather\DEFAULT_ATTRIBUTES);
8283
}
84+
85+
public function testIconNotFound(): void
86+
{
87+
$this->expectException(IconNotFoundException::class);
88+
89+
$this->icons->get('icon-that-should-not-be-found');
90+
}
8391
}

0 commit comments

Comments
 (0)