ta-tikoma / bog-jug
Convert regex group to objects
Installs: 2 221
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 1
Forks: 0
Open Issues: 0
Requires
- php: ^8.1.0
Requires (Dev)
- phpstan/phpstan: ^1.10
- phpunit/phpunit: ^10.2
- squizlabs/php_codesniffer: 3.*
- symfony/var-dumper: ^6.3
This package is auto-updated.
Last update: 2024-11-18 11:26:16 UTC
README
A tool that makes it easier to work with regex groups. Mapped regex group to php-classes, because array is sucks.
Install
composer require ta-tikoma/bog-jug
Use
- Create a class for regex descriptions.
- Use property attributes for defined regex group:
#[Group('...')]
for group body;#[After]
and#[Before]
for defined outside symbols. - If you need to indicate the count of a group, use attributes like
#[ZeroOrOne]
,#[ZeroOrMore]
and others from the namespaceBogJug\Attributes\Count
. - Finally, you can add regex flags via the class attributes; for example:
#[SingleLine]
. - Now create instance of class BogJug and use one of two base methods:
- Method
->one($regex, $text)
to find the first value equal to regex; analogue:preg_match
. - Method
->many($regex, $text)
to get all values equal regex; analog:preg_match_all
.
- Method
Sample example
1. Define class of descriptions.
<?php declare(strict_types=1); namespace tests\Data; use BogJug\Attributes\Flags\SingleLine; use BogJug\Attributes\Regex\After; use BogJug\Attributes\Regex\Group; #[SingleLine] final class TinWoodman { public function __construct( #[Group('head'), After('.*')] public readonly string $noggin, #[Group('arms'), After('.*')] public readonly string $upperLimbs, #[Group('legs'), After('.*')] public readonly string $lowerLimbs, #[Group('body'), After('.*')] public readonly string $torso, #[Group('heart'), After('.*')] public readonly string|null $coeur, ) { } }
2. Call method of BogJug.
$bj = new BogJug(); $tw = $bj->one(TinWoodman::class, <<<OZ One of the big trees had been partly chopped through, and standing beside it, with an uplifted axe in his hands, was a man made entirely of tin. His head and arms and legs were jointed upon his body, but he stood perfectly motionless, as if he could not stir at all. OZ); dump($tw);
3. Take result.
^ tests\Data\TinWoodman^ {#427 +noggin: "head" +upperLimbs: "arms" +lowerLimbs: "legs" +torso: "body" +coeur: null }