PHPackages                             tobento/service-icon - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. tobento/service-icon

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

tobento/service-icon
====================

Managing icons for PHP applications.

2.0(7mo ago)053↓66.7%3MITPHPPHP &gt;=8.4

Since Nov 11Pushed 7mo ago1 watchersCompare

[ Source](https://github.com/tobento-ch/service-icon)[ Packagist](https://packagist.org/packages/tobento/service-icon)[ Docs](https://www.tobento.ch)[ RSS](/packages/tobento-service-icon/feed)WikiDiscussions 2.x Synced 1mo ago

READMEChangelog (3)Dependencies (7)Versions (5)Used By (3)

Icon Service
============

[](#icon-service)

Managing icons for PHP applications.

Table of Contents
-----------------

[](#table-of-contents)

- [Getting started](#getting-started)
    - [Requirements](#requirements)
    - [Highlights](#highlights)
- [Documentation](#documentation)
    - [Icon Interface](#icon-interface)
        - [Icon](#icon)
    - [Icon Factory Interface](#icon-factory-interface)
        - [Icon Factory](#icon-factory)
        - [Icon Factory Translator](#icon-factory-translator)
    - [Icons Interface](#icons-interface)
        - [Svg File Icons](#svg-file-icons)
        - [Svg File Icons To Json File](#svg-file-icons-to-json-file)
        - [Svg File Icons To Json Files](#svg-file-icons-to-json-files)
        - [In Memory Html Icons](#in-memory-html-icons)
        - [Icons](#icons)
        - [Stack Icons](#stack-icons)
    - [Example](#example)
        - [Font Awesome](#font-awesome)
    - [Accessibility](#accessibility)
- [Credits](#credits)

---

Getting started
===============

[](#getting-started)

Add the latest version of the icon service project running this command.

```
composer require tobento/service-icon

```

Requirements
------------

[](#requirements)

- PHP 8.4 or greater

Highlights
----------

[](#highlights)

- Framework-agnostic, will work with any project
- Decoupled design
- Customizable with factories to fit your needs

Documentation
=============

[](#documentation)

Icon Interface
--------------

[](#icon-interface)

```
use Tobento\Service\Icon\IconInterface;
use Tobento\Service\Icon\IconFactory;

$icon = new IconFactory()->createIconFromHtml('download', '');

var_dump($icon instanceof IconInterface);
// bool(true)
```

**name**

Returns the icon name.

```
var_dump($icon->name());
// string(8) "download"
```

**render**

Returns the icon.

```

// or just

```

Both icons from above will produce the following output:

```

```

**size**

Returns a new instance with the specified size.

```
$icon = $icon->size('xs');
```

**attr**

Returns a new instance with the specified attribute.

```
$icon = $icon->attr(name: 'id', 'some-id');
```

Adds a class to existing classes:

```
$icon = $icon->attr(name: 'class', 'foo');
```

Overwrites existing classes:

```
$icon = $icon->attr(name: 'class', ['foo']);
```

**label**

Returns a new instance with the specified label and position.

```
$icon = $icon->label(text: 'Download');

$icon = $icon->label(text: 'Download', position: 'left');
```

**labelSize**

Returns a new instance with the specified label size.

```
$icon = $icon->labelSize('xl');
```

**labelAttr**

Returns a new instance with the specified label size.

```
$icon = $icon->labelAttr(name: 'id', 'some-id');
```

Adds a class to existing classes:

```
$icon = $icon->labelAttr(name: 'class', 'foo');
```

Overwrites existing classes:

```
$icon = $icon->labelAttr(name: 'class', ['foo']);
```

**tag**

Returns a new instance of the icon tag which may be used if you only want to render the SVG.

```
use Tobento\Service\Tag\TagInterface;

var_dump($icon->tag() instanceof TagInterface);
// bool(true)

$svg = (string)$icon->tag();
//
```

Check out [Tag Interface](https://github.com/tobento-ch/service-tag#tag-interface) to learn more about the interface.

### Icon

[](#icon)

```
use Tobento\Service\Icon\Icon;
use Tobento\Service\Icon\IconInterface;
use Tobento\Service\Tag\Tag;
use Tobento\Service\Tag\TagInterface;
use Tobento\Service\Tag\Attributes;

$icon = new Icon(
    name: 'download',
    tag: new Tag(
        name: 'svg',
        html: '',
        attributes: new Attributes([
            'xmlns' => 'http://www.w3.org/2000/svg',
            'width' => '20',
            'height'=> '20',
            'viewBox' => '0 0 100 100',
        ]),
    ),
    labelTag: null, // null|TagInterface
    parentTag: null, // null|TagInterface
);

var_dump($icon instanceof IconInterface);
// bool(true)
```

You may check out the [Tag Service](https://github.com/tobento-ch/service-tag) to learn more about it.

Icon Factory Interface
----------------------

[](#icon-factory-interface)

Easily create icons with the provided icon factory:

```
use Tobento\Service\Icon\IconFactoryInterface;
use Tobento\Service\Icon\IconFactory;

$iconFactory = new IconFactory();

var_dump($iconFactory instanceof IconFactoryInterface);
// bool(true)
```

**createIcon**

```
use Tobento\Service\Icon\IconInterface;
use Tobento\Service\Icon\CreateIconException;
use Tobento\Service\Tag\Tag;
use Tobento\Service\Tag\TagInterface;
use Tobento\Service\Tag\Attributes;

try {
    $icon = $iconFactory->createIcon(
        name: 'download',
        tag: new Tag(
            name: 'svg',
            html: '',
            attributes: new Attributes([
                'xmlns' => 'http://www.w3.org/2000/svg',
                'width' => '20',
                'height'=> '20',
                'viewBox' => '0 0 100 100',
            ]),
        ),
        labelTag: null, // null|TagInterface
        parentTag: null, // null|TagInterface
    );

    var_dump($icon instanceof IconInterface);
    // bool(true)
} catch (CreateIconException $e) {
    // do something
}
```

**createIconFromHtml**

```
use Tobento\Service\Icon\IconInterface;
use Tobento\Service\Icon\CreateIconException;

try {
    $icon = $iconFactory->createIconFromHtml(
        name: 'download',
        html: ''
    );

    var_dump($icon instanceof IconInterface);
    // bool(true)
} catch (CreateIconException $e) {
    // do something
}
```

**createIconFromFile**

```
use Tobento\Service\Icon\IconInterface;
use Tobento\Service\Icon\CreateIconException;
use Tobento\Service\Filesystem\File;

try {
    $icon = $iconFactory->createIconFromFile(
        name: 'download',
        file: 'path/download.svg' // string|File
    );

    var_dump($icon instanceof IconInterface);
    // bool(true)
} catch (CreateIconException $e) {
    // do something
}
```

### Icon Factory

[](#icon-factory)

```
use Tobento\Service\Icon\IconFactoryInterface;
use Tobento\Service\Icon\IconFactory;
use Tobento\Service\Tag\TagFactoryInterface;

$iconFactory = new IconFactory(
    tagFactory: null // null|TagFactoryInterface
);

var_dump($iconFactory instanceof IconFactoryInterface);
// bool(true)
```

The default icon factory will produce the following icon output:

```
$icon = $iconFactory->createIconFromHtml(
    name: 'download',
    html: ''
);
