Skip to content

Commit 3b836ea

Browse files
committed
updating the README.md
1 parent 4c70263 commit 3b836ea

File tree

4 files changed

+96
-0
lines changed

4 files changed

+96
-0
lines changed

README.md

+65
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11

22
# MetaParser
33

4+
Extract metadata from attributes and/or annotations.
5+
46
#### If you like this project gift us a ⭐.
57

68
<hr />
@@ -9,3 +11,66 @@
911

1012
composer require thenlabs/meta-parser dev-main
1113

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
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace ThenLabs\MetaParser\Exception;
5+
6+
/**
7+
* @author Andy Daniel Navarro Taño <[email protected]>
8+
*/
9+
class NoParserFoundException extends \Exception
10+
{
11+
}

src/Parser.php

+10
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
use Doctrine\Common\Annotations\AnnotationReader;
77
use Reflector;
8+
use ThenLabs\MetaParser\Exception\NoParserFoundException;
89

910
/**
1011
* @author Andy Daniel Navarro Taño <[email protected]>
@@ -37,8 +38,17 @@ public function setParsers(array $parsers): void
3738
$this->parsers = $parsers;
3839
}
3940

41+
/**
42+
* @param Reflector $reflector
43+
* @return Metadata
44+
* @throws NoParserFoundException
45+
*/
4046
public function parse(Reflector $reflector): Metadata
4147
{
48+
if (empty($this->parsers)) {
49+
throw new NoParserFoundException();
50+
}
51+
4252
$result = [];
4353

4454
foreach ($this->parsers as $parser) {

tests/test-Parser.php

+10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?php
22

3+
use ThenLabs\MetaParser\Exception\NoParserFoundException;
34
use ThenLabs\MetaParser\Parser;
45
use ThenLabs\MetaParser\Tests\MyAnnotation;
56
use ThenLabs\MetaParser\Tests\MyClass;
@@ -13,3 +14,12 @@
1314
$this->assertEquals($data1Expected, $result->get(MyAnnotation::class)->get('data1'));
1415
$this->assertNull($result->get(MyAnnotation::class)->get('data2'));
1516
});
17+
18+
test(function () {
19+
$this->expectException(NoParserFoundException::class);
20+
21+
$parser = new Parser();
22+
$parser->setParsers([]);
23+
24+
$parser->parse(new ReflectionClass('stdClass'));
25+
});

0 commit comments

Comments
 (0)