https://github.com/thenlabs/meta-parser
Extract metadata from attributes and/or annotations.
https://github.com/thenlabs/meta-parser
annotations attributes doctrine-annotations metadata-parser parser
Last synced: 3 months ago
JSON representation
Extract metadata from attributes and/or annotations.
- Host: GitHub
- URL: https://github.com/thenlabs/meta-parser
- Owner: thenlabs
- License: mit
- Created: 2022-07-23T14:42:42.000Z (almost 4 years ago)
- Default Branch: main
- Last Pushed: 2022-07-25T12:26:09.000Z (almost 4 years ago)
- Last Synced: 2026-01-11T11:44:01.669Z (4 months ago)
- Topics: annotations, attributes, doctrine-annotations, metadata-parser, parser
- Language: PHP
- Homepage:
- Size: 26.4 KB
- Stars: 0
- Watchers: 0
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# MetaParser
Extract metadata from attributes and/or annotations.
#### If you like this project gift us a ⭐.
## Installation.
composer require thenlabs/meta-parser dev-main
## Usage example.
```php
/**
* @MyMetadata(data1="value1")
*/
#[MyMetadata(data1: 'value2')]
class MyClass
{
}
$myClass = new ReflectionClass(MyClass::class);
/**
* Reading from annotations only.
*/
$annotationParser = new ThenLabs\MetaParser\AnnotationParser();
$annotationParserResult = $annotationParser->parse($myClass);
$annotationParserResult->get(MyMetadata::class)->get('data1') === 'value1'; // true
/**
* Reading from attributes only.
*/
$attributeParser = new ThenLabs\MetaParser\AttributeParser();
$attributeParserResult = $attributeParser->parse($myClass);
$attributeParserResult->get(MyMetadata::class)->get('data1') === 'value2'; // true
/**
* Reading both.
*/
$parser = new ThenLabs\MetaParser\Parser();
$parserResult = $parser->parse($myClass);
// this returns true becouse attributes override annotations.
$parserResult->get(MyMetadata::class)->get('data1') === 'value2';
```
### Highlights about `ThenLabs\MetaParser\Parser` class.
#### 1. For read annotations it's necessary to install [Doctrine Annotations](https://www.doctrine-project.org/projects/doctrine-annotations/en/1.13/index.html):
$ composer require doctrine/annotations
#### 2. The attribute parser require a PHP version grater than 8.0.
#### 3. The `parse()` methods accept an instance of `Reflector`, so that, for parse a method of a class(for example), you can use a `ReflectionMethod` instance.
## Development.
Clone this repository and install the Composer dependencies.
$ composer install
### Running the tests.
All the tests of this project was written with our testing framework [PyramidalTests][pyramidal-tests] wich is based on [PHPUnit][phpunit].
Run tests:
$ composer test
[phpunit]: https://phpunit.de
[pyramidal-tests]: https://github.com/thenlabs/pyramidal-tests