xicrow/php-icons

Icon management through PHP

1.0.0 2022-09-16 20:45 UTC

This package is not auto-updated.

Last update: 2024-04-28 03:46:38 UTC


README

Installation

The recommended way to install is through Composer.

composer require xicrow/php-icons

Usage

Depending on what icons you are using (or want to use), implement the icon class of your choice:

// FontAwesome 4
echo \Xicrow\PhpIcons\FontAwesome4::Icon(\Xicrow\PhpIcons\FontAwesome4::Icon_Thumbs_Up);
<i class="fa fa-thumbs-up"></i>
// FontAwesome 5
echo \Xicrow\PhpIcons\FontAwesome5::Icon(\Xicrow\PhpIcons\FontAwesome5::Icon_Thumbs_Up);
<i class="fas fa-thumbs-up"></i>

For now, we'll just use the BaseIcon class, to show usages.

Basic usage, show an icon:

echo BaseIcon::Icon(ICON_IDENTIFIER);
<span class="ICON_IDENTIFIER"></span>

Apply one or more modifiers (not all icon packs support modifiers):

echo BaseIcon::Icon(ICON_IDENTIFIER, ICON_MODIFIER, ICON_MODIFIER);
<span class="ICON_IDENTIFIER ICON_MODIFIER ICON_MODIFIER"></span>

Modifiers can also be chained from the icon:

echo BaseIcon::Icon(ICON_IDENTIFIER)
    ->modify(ICON_MODIFIER)
    ->modify(ICON_MODIFIER)
;
<span class="ICON_IDENTIFIER ICON_MODIFIER ICON_MODIFIER"></span>

Attributes for the icon can also be set by chaining:

echo BaseIcon::Icon(ICON_IDENTIFIER)
    ->modify(ICON_MODIFIER)
    ->modify(ICON_MODIFIER)
    ->attribute('title', 'Nifty help text')
    ->attribute('style', 'color: blue;')
;
<span class="ICON_IDENTIFIER ICON_MODIFIER ICON_MODIFIER" title="Nifty help text" style="color: blue;"></span>

Untill the icon is rendered (either through the BaseIcon::render() method or other to-string triggers like echo), it is possible to add modifiers and attributes to the icon.

// Made-up constants
$oStatusIcon = BaseIcon::Icon(BaseIcon::Icon_Bullet, BaseIcon::Modifier_Size_X2);
// Looping a resultset
foreach ($arrResults as $oResult) {
    // Clone the status icon, set color from result, and render
    echo (clone $oStatusIcon)->attribute('style', 'color: '.$oResult->strStatusColor.';');
}

Extending

Instead of using the classes in this repository directly, you are encouraged to implement your own extension like so:

class FA extends \Xicrow\PhpIcons\FontAwesome5 {}

echo FA::Icon(FA::Icon_Thumbs_Up, FA::Modifier_Lg);

This will ease your own implementation, since you determine what the class will be called and also makes it easier for you to change provider in the future.

It also makes it easy to implement shortcuts for commonly used icons:

class FA extends \Xicrow\PhpIcons\FontAwesome5 {
    public static function IconCreate(): self
    {
        return static::Icon(static::Icon_Plus_Circle)->attribute('style', 'color: green;');
    }

    public static function IconEdit(): self
    {
        return static::Icon(static::Icon_Pencil)->attribute('style', 'color: blue;');
    }

    public static function IconDelete(): self
    {
        return static::Icon(static::Icon_Trash)->attribute('style', 'color: red;');
    }
}

echo FA::IconCreate();

Supported icon packs

Currently, there are generators and classes for the following icon packs:

  • Bootstrap 3 Glyphicons
  • Devicons
  • FontAwesome 4
  • FontAwesome 5 (free)

Generators are used to create the constants for icons (and modifiers) for each icon pack, these generators will mostly be able to generate constants for every specific version of the icon pack, thereby supporting exactly the version you are using.

Classes are simply the implementation of the specific icon pack, that is using the generated icon (and modifier) constants, and implementing a render method specific for that icon pack.

TODO

License

Copyright © 2022 Jan Ebsen Licensed under the MIT license.