Extract metadata from attributes and/or annotations.
composer require thenlabs/meta-parser dev-main
/**
* @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';
1. For read annotations it's necessary to install Doctrine Annotations:
$ composer require doctrine/annotations
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.
Clone this repository and install the Composer dependencies.
$ composer install
All the tests of this project was written with our testing framework PyramidalTests wich is based on PHPUnit.
Run tests:
$ composer test