Skip to content

Commit 8b87cc1

Browse files
author
Matej Grgić
committed
feat: using php attributes
1 parent ba7ac55 commit 8b87cc1

File tree

6 files changed

+88
-5
lines changed

6 files changed

+88
-5
lines changed

rector.php

+19-5
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,26 @@
33
declare(strict_types=1);
44

55
use Rector\Config\RectorConfig;
6+
use Rector\Doctrine\Set\DoctrineSetList;
7+
use Rector\Php80\Rector\Class_\AnnotationToAttributeRector;
8+
use Rector\Php80\ValueObject\AnnotationToAttribute;
9+
use Rector\Symfony\Set\SensiolabsSetList;
10+
use Rector\Symfony\Set\SymfonySetList;
11+
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
612

7-
return RectorConfig::configure()
8-
->withPaths([
13+
return static function (RectorConfig $rectorConfig): void {
14+
// Paths for Rector to act upon
15+
$rectorConfig->paths([
916
__DIR__ . '/src',
1017
__DIR__ . '/tests',
11-
])
12-
->withPhpSets(true)
13-
->withRules([
1418
]);
19+
20+
$rectorConfig->sets([
21+
DoctrineSetList::ANNOTATIONS_TO_ATTRIBUTES,
22+
SymfonySetList::ANNOTATIONS_TO_ATTRIBUTES,
23+
SensiolabsSetList::ANNOTATIONS_TO_ATTRIBUTES,
24+
]);
25+
$rectorConfig->ruleWithConfiguration(AnnotationToAttributeRector::class, [
26+
new AnnotationToAttribute(UniqueEntity::class),
27+
]);
28+
};
+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Undabot\SymfonyJsonApi\Model\Resource\Attribute;
6+
7+
use Attribute as PhpAttribute;
8+
9+
#[PhpAttribute(PhpAttribute::TARGET_PROPERTY | PhpAttribute::IS_REPEATABLE)]
10+
class Attribute
11+
{
12+
public function __construct(
13+
public string $name,
14+
public ?string $description = null,
15+
public ?string $example = null,
16+
public ?string $format = null,
17+
public bool $nullable = false
18+
) {}
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Undabot\SymfonyJsonApi\Model\Resource\Attribute;
6+
7+
abstract class Relationship
8+
{
9+
public function __construct(
10+
public string $name,
11+
public ?string $type = null,
12+
public ?string $description = null,
13+
public bool $nullable = false
14+
) {}
15+
16+
abstract public function isToMany(): bool;
17+
}
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Undabot\SymfonyJsonApi\Model\Resource\Attribute;
6+
7+
use Attribute as PhpAttribute;
8+
9+
#[PhpAttribute(PhpAttribute::TARGET_PROPERTY | PhpAttribute::IS_REPEATABLE)]
10+
class ToMany extends Relationship
11+
{
12+
public function isToMany(): bool
13+
{
14+
return true;
15+
}
16+
}
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Undabot\SymfonyJsonApi\Model\Resource\Attribute;
6+
7+
use Attribute as PhpAttribute;
8+
9+
#[PhpAttribute(PhpAttribute::TARGET_PROPERTY | PhpAttribute::IS_REPEATABLE)]
10+
class ToOne extends Relationship
11+
{
12+
public function isToMany(): bool
13+
{
14+
return false;
15+
}
16+
}

tests/Bridge/OpenAPI/Model/Article.php

+1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ class Article implements ApiModel
4545
*
4646
* @Attribute(name="eventDate")
4747
*/
48+
#[\Undabot\SymfonyJsonApi\Model\Resource\Attribute\Attribute('ovoJeIme')]
4849
private $date;
4950

5051
/**

0 commit comments

Comments
 (0)