Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement enums #1

Merged
merged 8 commits into from
May 23, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
/.github export-ignore
/.gitattributes export-ignore
/.gitignore export-ignore
/*.md export-ignore
/composer.json export-ignore
/composer.lock export-ignore
/rector.php export-ignore
Expand Down
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
],
"autoload": {
"psr-4": {
"Bugo\\FontAwesomeHelper\\": "src/FontAwesomeHelper/"
"Bugo\\FontAwesomeHelper\\": "src/"
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I probably would have just named that FAHelper. But I hate mile long namespaces :P

}
},
"autoload-dev": {
Expand All @@ -25,10 +25,10 @@
}
},
"require": {
"php": "^8.0"
"php": "^8.1",
"nette/utils": "^4.0"
},
"require-dev": {
"php": "^8.1",
"pestphp/pest": "^2.33.4",
"rector/rector": "^1.0"
},
Expand Down
95 changes: 90 additions & 5 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions src/Enums/BaseIcon.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php declare(strict_types=1);

namespace Bugo\FontAwesomeHelper\Enums;

enum BaseIcon: string
{
case V5 = 'fa%s fa-%s';
case V6 = 'fa-%s fa-%s';
}
64 changes: 64 additions & 0 deletions src/Enums/Icon.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php declare(strict_types=1);

namespace Bugo\FontAwesomeHelper\Enums;

use Bugo\FontAwesomeHelper\Interfaces\CasesAwareInterface;
use Bugo\FontAwesomeHelper\Interfaces\CollectionAwareInterface;
use Bugo\FontAwesomeHelper\Interfaces\RandomAwareInterface;
use Bugo\FontAwesomeHelper\Styles\BrandIcon;
use Bugo\FontAwesomeHelper\Styles\RegularIcon;
use Bugo\FontAwesomeHelper\Styles\SolidIcon;
use Bugo\FontAwesomeHelper\Traits\HasCases;
use Bugo\FontAwesomeHelper\Traits\HasCollection;
use Bugo\FontAwesomeHelper\Traits\HasRandom;

use function sprintf;
use function array_search;

enum Icon: string implements CasesAwareInterface, CollectionAwareInterface, RandomAwareInterface
{
use HasCases, HasCollection, HasRandom;

case V5 = '5';
case V6 = '6';

public function factory(Type $type, string $identifier): string
{
return $this->build($this->name, $type, $identifier);
}

protected function build($version, $type, $icon): string
{
$validIcon = $this->getIcon($type, $icon);
$baseIcon = constant(__NAMESPACE__ . "\BaseIcon::$version");

return $validIcon ? sprintf($baseIcon->value, $this->getSegment($type), $validIcon) : '';
}

protected function getSegment(Type $type): string
{
return match ([$this, $type]) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pretty creative. Actually solves an issue that I was facing in some code myself.

[self::V5, Type::Brand] => TypeId::BrandV5->value,
[self::V5, Type::Regular] => TypeId::RegularV5->value,
[self::V5, Type::Solid] => TypeId::SolidV5->value,
[self::V6, Type::Brand] => TypeId::BrandV6->value,
[self::V6, Type::Regular] => TypeId::RegularV6->value,
[self::V6, Type::Solid] => TypeId::SolidV6->value,
};
}

protected function getIcon(Type $type, string $icon): string
{
$set = match ($type) {
Type::Solid => (new SolidIcon())->getAll(),
Type::Brand => (new BrandIcon())->getAll(),
Type::Regular => (new RegularIcon())->getAll(),
};

if (($key = array_search($icon, $set, true)) !== false) {
return $set[$key];
}

return '';
}
}
10 changes: 10 additions & 0 deletions src/Enums/Type.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php declare(strict_types=1);

namespace Bugo\FontAwesomeHelper\Enums;

enum Type
{
case Brand;
case Regular;
case Solid;
}
13 changes: 13 additions & 0 deletions src/Enums/TypeId.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php declare(strict_types=1);

namespace Bugo\FontAwesomeHelper\Enums;

enum TypeId: string
{
case BrandV5 = 'b';
case BrandV6 = 'brands';
case RegularV5 = 'r';
case RegularV6 = 'regular';
case SolidV5 = 's';
case SolidV6 = 'solid';
}
45 changes: 0 additions & 45 deletions src/FontAwesomeHelper/Collections/Collection.php

This file was deleted.

35 changes: 0 additions & 35 deletions src/FontAwesomeHelper/Collections/OldCollection.php

This file was deleted.

26 changes: 0 additions & 26 deletions src/FontAwesomeHelper/Factories/BrandIconFactory.php

This file was deleted.

22 changes: 0 additions & 22 deletions src/FontAwesomeHelper/Factories/IconFactory.php

This file was deleted.

Loading