-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
347 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# Extractor | ||
Sometimes you have a piece of text that contains an enum value that you | ||
want to use. With this you can simply pass a multiline string and extract all | ||
enums that are mentioned by value. | ||
|
||
Note: This also works with `Mappers`. | ||
|
||
## Usage | ||
```php | ||
use Henzeb\Enumhancer\Concerns\Extractor; | ||
|
||
enum YourEnum: string { | ||
use Extractor; | ||
|
||
case ENUM = 'enum'; | ||
case ENUM2 = 'another enum'; | ||
} | ||
``` | ||
|
||
### Examples | ||
```php | ||
YourEnum::extract('you can find another enum here'); // returns [YourEnum::ENUM2] | ||
YourEnum::extract('A lot of text with (enum)'); // returns [YourEnum::ENUM] | ||
YourEnum::extract('another enum (enum)'); // returns [YourEnum::ENUM2, YourEnum::ENUM] | ||
YourEnum::extract('extact case sensitive another ENUM') // returns [YourEnum::ENUM2]; | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
namespace Henzeb\Enumhancer\Concerns; | ||
|
||
use Henzeb\Enumhancer\Helpers\EnumExtractor; | ||
|
||
trait Extractor | ||
{ | ||
public static function extract(string $text): array | ||
{ | ||
return EnumExtractor::extract(self::class, $text); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
|
||
namespace Henzeb\Enumhancer\Helpers; | ||
|
||
use UnitEnum; | ||
use Henzeb\Enumhancer\Contracts\Mapper; | ||
use Henzeb\Enumhancer\Concerns\Mappers; | ||
|
||
class EnumExtractor | ||
{ | ||
use Mappers; | ||
|
||
public static function extract(string $class, string $text, Mapper ...$mappers): array | ||
{ | ||
EnumCheck::check($class); | ||
|
||
/** | ||
* @var $class UnitEnum|string | ||
*/ | ||
|
||
$match = array_map( | ||
function ($enum) { | ||
|
||
if (property_exists($enum, 'value')) { | ||
return $enum->value; | ||
} | ||
|
||
return $enum->name; | ||
}, $class::cases() | ||
); | ||
|
||
$match = implode( | ||
'|', | ||
array_merge( | ||
$match, | ||
...array_map(fn(Mapper $map) => $map->keys($class), $mappers) | ||
)); | ||
|
||
preg_match_all(sprintf('/%s/i', $match), $text, $matches); | ||
|
||
$matches = array_map(fn($value) => EnumMapper::map($value, ...$mappers), $matches[0] ?? []); | ||
|
||
return EnumMakers::makeArray($class, $matches); | ||
} | ||
} |
Oops, something went wrong.