-
Notifications
You must be signed in to change notification settings - Fork 1
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
Changes from 1 commit
833b2d9
d37c3a1
257d9c9
da68214
95c081c
4e0e1cc
26115f5
f34c6fc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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'; | ||
} |
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]) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 ''; | ||
} | ||
} |
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; | ||
} |
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'; | ||
} |
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
There was a problem hiding this comment.
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