codamos / tagger
A simple tagging library inspired by Go's struct tags
Requires
- php: ^8.1
- ext-mbstring: *
- ramsey/collection: ^2.1
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.80
- phpstan/phpstan: ^2.1
- phpunit/phpunit: ^10.5
README
A simple tagging library inspired by Go's struct tags
About
codamos/tagger
is a simple tagging library inspired by Go's struct tags.
It allows you to fetch free-form tags from class properties via a Tag
attribute that can later be used by third-party
libraries or your own code.
The library also includes a tag parser and a class tags scanner. These should allow you to use tagger at scale for your entire application at build time, or at runtime.
Installation
Install this package as a dependency using Composer.
composer require codamos/tagger
Usage
You may use the Tag
attribute to annotate your class' properties with free-form tags, split by space characters.
use Codamos\Tagger\Attributes\Tag; use Codamos\Tagger\Parser\TagParser; use Codamos\Tagger\Scanner\TagScanner; class UserDTO { #[Tag('json:"id" orm:"id,primaryKey,autoIncrement"')]} public int $id; #[Tag('json:"name" orm:"name,notNull,size:255"')] public string $name; #[Tag('json:"email" orm:"email,notNull,size:255,unique,index"')] public string $email; $parser = new TagParser(); $scanner = new TagScanner($scanner); $map = $scanner->scan(UserDTO::class); $emailTags = $map->get('email'); $emailTags->get('json'); // string(json:"email") $emailTags->get('orm'); // string(orm:"email,notNull,size:255,unique,index")
Copyright and License
The codamos/tagger library is copyright © Codamos and licensed for use under the terms of the MIT License (MIT). Please see LICENSE for more information.