Skip to content

Commit 410ca75

Browse files
authored
Merge pull request #31 from undabot/feat/attributes
Feat/attributes
2 parents 71dcf8b + 4165e68 commit 410ca75

File tree

9 files changed

+118
-86
lines changed

9 files changed

+118
-86
lines changed

phpunit.xml.dist

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<!-- https://phpunit.de/manual/current/en/appendixes.configuration.html -->
3-
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/11.1/phpunit.xsd" backupGlobals="false" colors="true" bootstrap="vendor/autoload.php" cacheDirectory=".phpunit.cache">
3+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/11.1/phpunit.xsd" backupGlobals="false" colors="true" bootstrap="vendor/autoload.php" cacheDirectory=".phpunit.cache"
4+
displayDetailsOnTestsThatTriggerDeprecations="true"
5+
displayDetailsOnTestsThatTriggerWarnings="true"
6+
>
47
<coverage>
58
<report>
69
<clover outputFile="tests/_reports/logs/clover.xml"/>

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

+26-74
Original file line numberDiff line numberDiff line change
@@ -6,92 +6,44 @@
66

77
use Symfony\Component\Validator\Constraints as Assert;
88
use Undabot\SymfonyJsonApi\Model\ApiModel;
9-
use Undabot\SymfonyJsonApi\Model\Resource\Annotation\Attribute;
10-
use Undabot\SymfonyJsonApi\Model\Resource\Annotation\ToMany;
11-
use Undabot\SymfonyJsonApi\Model\Resource\Annotation\ToOne;
9+
use Undabot\SymfonyJsonApi\Model\Resource\Attribute\Attribute;
10+
use Undabot\SymfonyJsonApi\Model\Resource\Attribute\ToMany;
11+
use Undabot\SymfonyJsonApi\Model\Resource\Attribute\ToOne;
1212
use Undabot\SymfonyJsonApi\Service\Resource\Validation\Constraint\ResourceType;
1313

14-
/** @ResourceType(type="article") */
14+
#[ResourceType('article')]
1515
class Article implements ApiModel
1616
{
17-
/**
18-
* @var string
19-
*/
20-
private $id;
17+
private string $id;
2118

22-
/**
23-
* @var string
24-
*
25-
* @Attribute(nullable=false)
26-
*/
27-
private $slug;
19+
#[Attribute('slug')]
20+
private string $slug;
2821

29-
/**
30-
* @var string
31-
*
32-
* @Attribute
33-
*/
34-
private $title;
22+
#[Attribute('titlw')]
23+
private string $title;
3524

36-
/**
37-
* @var string
38-
*
39-
* @Attribute(name="eventAddress")
40-
*/
41-
private $address;
25+
#[Attribute('eventAddress')]
26+
private string $address;
4227

43-
/**
44-
* @var string
45-
*
46-
* @Attribute(name="eventDate")
47-
*/
48-
private $date;
28+
#[Attribute('eventDate')]
29+
private string $date;
4930

50-
/**
51-
* @var bool
52-
*
53-
* @Attribute
54-
*/
55-
private $enabled;
31+
#[Attribute('enabled')]
32+
private bool $enabled;
5633

57-
/**
58-
* @var null|string
59-
*
60-
* @Attribute
61-
*/
62-
private $description;
34+
#[Attribute('description', null, null, null, true)]
35+
private ?string $description;
6336

64-
/**
65-
* @var string
66-
*
67-
* @ToOne(name="category", type="category", nullable=true)
68-
*
69-
* @Assert\Type(type="string")
70-
*/
71-
private $categoryId;
37+
#[ToOne('category', 'category', null, true)]
38+
private string $categoryId;
7239

73-
/**
74-
* @var string[]
75-
*
76-
* @ToMany(name="tags", type="tag", nullable=false)
77-
*
78-
* @Assert\Type(type="array")
79-
*/
80-
private $tagIds;
40+
#[ToMany('tags', 'tag')]
41+
#[Assert\Type('array')]
42+
private array $tagIds;
8143

82-
/**
83-
* @var string
84-
*
85-
* @Attribute(format="datetime", example="2001")
86-
*
87-
* @Assert\NotBlank
88-
*/
89-
private $createdAt;
44+
#[Attribute('createdAt', null, null, 'datetime')]
45+
private string $createdAt;
9046

91-
/**
92-
* @var null|string
93-
*
94-
* @Attribute
95-
*/
96-
private $updatedAt;
47+
#[Attribute('updatedAt')]
48+
private ?string $updatedAt;
9749
}

tests/Unit/Http/Service/Factory/RequestFactoryTest.php

-5
Original file line numberDiff line numberDiff line change
@@ -282,11 +282,6 @@ public static function invalidRequestPrimaryDataProvider(): iterable
282282
'Request data must be valid JSON',
283283
];
284284

285-
yield 'Null data given' => [
286-
null,
287-
'Request data must be valid JSON',
288-
];
289-
290285
yield 'Not array given' => [
291286
'2',
292287
'Request data must be parsable to a valid array',

tests/Unit/Http/Service/Factory/ResourceCollectionResponseTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ public static function provideFromArrayWillThrowExceptionGivenInvalidArgumentsPr
180180

181181
yield 'Resource array not valid type' => [
182182
[$objectCollection, ResourceInterface::class],
183-
null,
183+
[],
184184
null,
185185
null,
186186
'Class "' . \get_class($objectCollection) . '" was expected to be instanceof of "Undabot\JsonApi\Definition\Model\Resource\ResourceInterface" but is not.',

0 commit comments

Comments
 (0)