|
1 | 1 |
|
2 | 2 | # MetaParser
|
3 | 3 |
|
| 4 | +Extract metadata from attributes and/or annotations. |
| 5 | + |
4 | 6 | #### If you like this project gift us a ⭐.
|
5 | 7 |
|
6 | 8 | <hr />
|
|
9 | 11 |
|
10 | 12 | composer require thenlabs/meta-parser dev-main
|
11 | 13 |
|
| 14 | +## Usage example. |
| 15 | + |
| 16 | +```php |
| 17 | +/** |
| 18 | + * @MyMetadata(data1="value1") |
| 19 | + */ |
| 20 | +#[MyMetadata(data1: 'value2')] |
| 21 | +class MyClass |
| 22 | +{ |
| 23 | +} |
| 24 | + |
| 25 | +$myClass = new ReflectionClass(MyClass::class); |
| 26 | + |
| 27 | +/** |
| 28 | + * Reading from annotations only. |
| 29 | + */ |
| 30 | +$annotationParser = new ThenLabs\MetaParser\AnnotationParser(); |
| 31 | +$annotationParserResult = $annotationParser->parse($myClass); |
| 32 | + |
| 33 | +$annotationParserResult->get(MyMetadata::class)->get('data1') === 'value1'; // true |
| 34 | + |
| 35 | +/** |
| 36 | + * Reading from attributes only. |
| 37 | + */ |
| 38 | +$attributeParser = new ThenLabs\MetaParser\AttributeParser(); |
| 39 | +$attributeParserResult = $attributeParser->parse($myClass); |
| 40 | + |
| 41 | +$attributeParserResult->get(MyMetadata::class)->get('data1') === 'value2'; // true |
| 42 | + |
| 43 | +/** |
| 44 | + * Reading both. |
| 45 | + */ |
| 46 | +$parser = new ThenLabs\MetaParser\Parser(); |
| 47 | +$parserResult = $parser->parse($myClass); |
| 48 | + |
| 49 | +// this returns true becouse attributes override annotations. |
| 50 | +$parserResult->get(MyMetadata::class)->get('data1') === 'value2'; |
| 51 | +``` |
| 52 | + |
| 53 | +### Highlights about `ThenLabs\MetaParser\Parser` class. |
| 54 | + |
| 55 | +#### 1. For read annotations it's necessary to install [Doctrine Annotations](https://www.doctrine-project.org/projects/doctrine-annotations/en/1.13/index.html): |
| 56 | + |
| 57 | + $ composer require doctrine/annotations |
| 58 | + |
| 59 | +#### 2. The attribute parser require a PHP version grater than 8.0. |
| 60 | + |
| 61 | +## Development. |
| 62 | + |
| 63 | +Clone this repository and install the Composer dependencies. |
| 64 | + |
| 65 | + $ composer install |
| 66 | + |
| 67 | +### Running the tests. |
| 68 | + |
| 69 | +All the tests of this project was written with our testing framework [PyramidalTests][pyramidal-tests] wich is based on [PHPUnit][phpunit]. |
| 70 | + |
| 71 | +Run tests: |
| 72 | + |
| 73 | + $ composer test |
| 74 | + |
| 75 | +[phpunit]: https://phpunit.de |
| 76 | +[pyramidal-tests]: https://github.com/thenlabs/pyramidal-tests |
0 commit comments