Skip to content

Commit 95427a4

Browse files
authored
Merge pull request #326 from ixarlie/issue-325-php8-attributes
Issue 325 php8 attributes
2 parents 5d7953f + c247512 commit 95427a4

File tree

11 files changed

+328
-121
lines changed

11 files changed

+328
-121
lines changed

src/Configuration/Annotation/Embedded.php

+13
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,17 @@
44

55
namespace Hateoas\Configuration\Annotation;
66

7+
use JMS\Serializer\Annotation\AnnotationUtilsTrait;
8+
79
/**
810
* @Annotation
911
* @Target("ANNOTATION")
1012
*/
13+
#[\Attribute]
1114
class Embedded
1215
{
16+
use AnnotationUtilsTrait;
17+
1318
/**
1419
* @Required
1520
* @var mixed
@@ -32,4 +37,12 @@ class Embedded
3237
* phpcs:enable
3338
*/
3439
public $exclusion = null;
40+
41+
/**
42+
* @param string|array $content
43+
*/
44+
public function __construct(array $values = [], $content = null, ?string $type = null, ?string $xmlElementName = null, ?Exclusion $exclusion = null)
45+
{
46+
$this->loadAnnotationParameters(get_defined_vars());
47+
}
3548
}

src/Configuration/Annotation/Exclusion.php

+10
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,17 @@
44

55
namespace Hateoas\Configuration\Annotation;
66

7+
use JMS\Serializer\Annotation\AnnotationUtilsTrait;
8+
79
/**
810
* @Annotation
911
* @Target("ANNOTATION")
1012
*/
13+
#[\Attribute]
1114
final class Exclusion
1215
{
16+
use AnnotationUtilsTrait;
17+
1318
/**
1419
* @var array
1520
*/
@@ -38,4 +43,9 @@ final class Exclusion
3843
* @var string
3944
*/
4045
public $excludeIf = null;
46+
47+
public function __construct(array $values = [], ?array $groups = null, ?string $sinceVersion = null, ?string $untilVersion = null, ?int $maxDepth = null, ?string $excludeIf = null)
48+
{
49+
$this->loadAnnotationParameters(get_defined_vars());
50+
}
4151
}

src/Configuration/Annotation/Relation.php

+14
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,17 @@
44

55
namespace Hateoas\Configuration\Annotation;
66

7+
use JMS\Serializer\Annotation\AnnotationUtilsTrait;
8+
79
/**
810
* @Annotation
911
* @Target("CLASS")
1012
*/
13+
#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::IS_REPEATABLE)]
1114
final class Relation
1215
{
16+
use AnnotationUtilsTrait;
17+
1318
/**
1419
* @Required
1520
* @var string
@@ -37,4 +42,13 @@ final class Relation
3742
* phpcs:enable
3843
*/
3944
public $exclusion = null;
45+
46+
/**
47+
* @param string|Route $href
48+
* @param string|Embedded $embedded
49+
*/
50+
public function __construct(array $values = [], ?string $name = null, $href = null, $embedded = null, array $attributes = [], ?Exclusion $exclusion = null)
51+
{
52+
$this->loadAnnotationParameters(get_defined_vars());
53+
}
4054
}

src/Configuration/Annotation/RelationProvider.php

+10
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,24 @@
44

55
namespace Hateoas\Configuration\Annotation;
66

7+
use JMS\Serializer\Annotation\AnnotationUtilsTrait;
8+
79
/**
810
* @Annotation
911
* @Target("CLASS")
1012
*/
13+
#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::IS_REPEATABLE)]
1114
class RelationProvider
1215
{
16+
use AnnotationUtilsTrait;
17+
1318
/**
1419
* @var string
1520
*/
1621
public $name;
22+
23+
public function __construct(array $values = [], ?string $name = null)
24+
{
25+
$this->loadAnnotationParameters(get_defined_vars());
26+
}
1727
}

src/Configuration/Annotation/Route.php

+14
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,17 @@
44

55
namespace Hateoas\Configuration\Annotation;
66

7+
use JMS\Serializer\Annotation\AnnotationUtilsTrait;
8+
79
/**
810
* @Annotation
911
* @Target("ANNOTATION")
1012
*/
13+
#[\Attribute]
1114
class Route
1215
{
16+
use AnnotationUtilsTrait;
17+
1318
/**
1419
* @Required
1520
* @var string
@@ -31,4 +36,13 @@ class Route
3136
* @var string
3237
*/
3338
public $generator = null;
39+
40+
/**
41+
* @param array|string $parameters
42+
* @param bool|string $absolute
43+
*/
44+
public function __construct(array $values = [], ?string $name = null, $parameters = null, $absolute = false, ?string $generator = null)
45+
{
46+
$this->loadAnnotationParameters(get_defined_vars());
47+
}
3448
}

src/Configuration/Metadata/Driver/AnnotationDriver.php

+6-120
Original file line numberDiff line numberDiff line change
@@ -5,147 +5,33 @@
55
namespace Hateoas\Configuration\Metadata\Driver;
66

77
use Doctrine\Common\Annotations\Reader as AnnotationsReader;
8-
use Hateoas\Configuration\Annotation;
9-
use Hateoas\Configuration\Embedded;
10-
use Hateoas\Configuration\Exclusion;
11-
use Hateoas\Configuration\Metadata\ClassMetadata;
128
use Hateoas\Configuration\Provider\RelationProviderInterface;
13-
use Hateoas\Configuration\Relation;
14-
use Hateoas\Configuration\RelationProvider;
15-
use Hateoas\Configuration\Route;
169
use JMS\Serializer\Expression\CompilableExpressionEvaluatorInterface;
17-
use JMS\Serializer\Expression\Expression;
1810
use JMS\Serializer\Type\ParserInterface;
19-
use Metadata\ClassMetadata as JMSClassMetadata;
20-
use Metadata\Driver\DriverInterface;
2111

22-
class AnnotationDriver implements DriverInterface
12+
class AnnotationDriver extends AnnotationOrAttributeDriver
2313
{
24-
use CheckExpressionTrait;
25-
2614
/**
2715
* @var AnnotationsReader
2816
*/
2917
private $reader;
3018

31-
/**
32-
* @var RelationProviderInterface
33-
*/
34-
private $relationProvider;
35-
36-
/**
37-
* @var ParserInterface
38-
*/
39-
private $typeParser;
40-
4119
public function __construct(
4220
AnnotationsReader $reader,
4321
CompilableExpressionEvaluatorInterface $expressionLanguage,
4422
RelationProviderInterface $relationProvider,
4523
ParserInterface $typeParser
4624
) {
47-
$this->reader = $reader;
48-
$this->relationProvider = $relationProvider;
49-
$this->expressionLanguage = $expressionLanguage;
50-
$this->typeParser = $typeParser;
51-
}
52-
53-
public function loadMetadataForClass(\ReflectionClass $class): ?JMSClassMetadata
54-
{
55-
$annotations = $this->reader->getClassAnnotations($class);
56-
57-
if (0 === count($annotations)) {
58-
return null;
59-
}
60-
61-
$classMetadata = new ClassMetadata($class->getName());
62-
$classMetadata->fileResources[] = $class->getFilename();
63-
64-
foreach ($annotations as $annotation) {
65-
if ($annotation instanceof Annotation\Relation) {
66-
$classMetadata->addRelation(new Relation(
67-
$annotation->name,
68-
$this->createHref($annotation->href),
69-
$this->createEmbedded($annotation->embedded),
70-
$this->checkExpressionArray($annotation->attributes) ?: [],
71-
$this->createExclusion($annotation->exclusion)
72-
));
73-
} elseif ($annotation instanceof Annotation\RelationProvider) {
74-
$relations = $this->relationProvider->getRelations(new RelationProvider($annotation->name), $class->getName());
75-
foreach ($relations as $relation) {
76-
$classMetadata->addRelation($relation);
77-
}
78-
}
79-
}
80-
81-
if (0 === count($classMetadata->getRelations())) {
82-
return null;
83-
}
84-
85-
return $classMetadata;
86-
}
87-
88-
private function parseExclusion(Annotation\Exclusion $exclusion): Exclusion
89-
{
90-
return new Exclusion(
91-
$exclusion->groups,
92-
null !== $exclusion->sinceVersion ? (string) $exclusion->sinceVersion : null,
93-
null !== $exclusion->untilVersion ? (string) $exclusion->untilVersion : null,
94-
null !== $exclusion->maxDepth ? (int) $exclusion->maxDepth : null,
95-
$this->checkExpression($exclusion->excludeIf)
96-
);
97-
}
98-
99-
/**
100-
* @param mixed $href
101-
*
102-
* @return Expression|mixed
103-
*/
104-
private function createHref($href)
105-
{
106-
if ($href instanceof Annotation\Route) {
107-
return new Route(
108-
$this->checkExpression($href->name),
109-
is_array($href->parameters) ? $this->checkExpressionArray($href->parameters) : $this->checkExpression($href->parameters),
110-
$this->checkExpression($href->absolute),
111-
$href->generator
112-
);
113-
}
25+
parent::__construct($expressionLanguage, $relationProvider, $typeParser);
11426

115-
return $this->checkExpression($href);
27+
$this->reader = $reader;
11628
}
11729

11830
/**
119-
* @param Annotation\Embedded|mixed $embedded
120-
*
121-
* @return Expression|mixed
31+
* {@inheritdoc}
12232
*/
123-
private function createEmbedded($embedded)
124-
{
125-
if ($embedded instanceof Annotation\Embedded) {
126-
$embeddedExclusion = $embedded->exclusion;
127-
128-
if (null !== $embeddedExclusion) {
129-
$embeddedExclusion = $this->parseExclusion($embeddedExclusion);
130-
}
131-
132-
return new Embedded(
133-
$this->checkExpression($embedded->content),
134-
$this->checkExpression($embedded->xmlElementName),
135-
$embeddedExclusion,
136-
null !== $embedded->type ? $this->typeParser->parse($embedded->type) : null
137-
);
138-
}
139-
140-
return $this->checkExpression($embedded);
141-
}
142-
143-
private function createExclusion(?Annotation\Exclusion $exclusion = null): ?Exclusion
33+
protected function getClassAnnotations(\ReflectionClass $class): array
14434
{
145-
if (null !== $exclusion) {
146-
$exclusion = $this->parseExclusion($exclusion);
147-
}
148-
149-
return $exclusion;
35+
return $this->reader->getClassAnnotations($class);
15036
}
15137
}

0 commit comments

Comments
 (0)