Skip to content

Commit 28c699c

Browse files
authored
fix: use FQCN when using an enum in callback constraint and remove ApiResource from the enum (#362)
1 parent 49d0177 commit 28c699c

File tree

11 files changed

+106
-19
lines changed

11 files changed

+106
-19
lines changed

bin/compile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ php schema.phar generate tmp/original tests/e2e/schema.yml -n -vv --ansi;
1111
diff tests/e2e/original/App/Entity/Person.php tmp/original/App/Entity/Person.php;
1212
diff tests/e2e/original/App/Entity/PostalAddress.php tmp/original/App/Entity/PostalAddress.php;
1313
diff tests/e2e/original/App/Entity/Thing.php tmp/original/App/Entity/Thing.php;
14+
diff tests/e2e/original/App/Enum/GenderType.php tmp/original/App/Enum/GenderType.php;
1415

1516
# Already generated files
1617

@@ -21,3 +22,4 @@ php schema.phar generate tmp/customized tests/e2e/schema.yml -n -vv --ansi;
2122
diff tests/e2e/customized/App/Entity/Person.php tmp/customized/App/Entity/Person.php;
2223
diff tests/e2e/customized/App/Entity/PostalAddress.php tmp/customized/App/Entity/PostalAddress.php;
2324
diff tests/e2e/customized/App/Entity/Thing.php tmp/customized/App/Entity/Thing.php;
25+
diff tests/e2e/customized/App/Enum/GenderType.php tmp/customized/App/Enum/GenderType.php;

src/AttributeGenerator/ApiPlatformCoreAttributeGenerator.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ final class ApiPlatformCoreAttributeGenerator extends AbstractAttributeGenerator
3535
*/
3636
public function generateClassAttributes(Class_ $class): array
3737
{
38-
if ($class->isAbstract) {
38+
if ($class->isAbstract || $class->isEnum()) {
3939
return [];
4040
}
4141

@@ -104,6 +104,6 @@ public function generatePropertyAttributes(Property $property, string $className
104104
*/
105105
public function generateUses(Class_ $class): array
106106
{
107-
return !$class->isEnum() ? [new Use_(ApiResource::class), new Use_(ApiProperty::class)] : [];
107+
return [new Use_(ApiResource::class), new Use_(ApiProperty::class)];
108108
}
109109
}

src/AttributeGenerator/ConstraintAttributeGenerator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public function generatePropertyAttributes(Property $property, string $className
7171
}
7272

7373
if ($property->isEnum && $property->range && $property->rangeName) {
74-
$args = ['callback' => [$property->rangeName, 'toArray']];
74+
$args = ['callback' => [new Literal(sprintf('%s::class', $property->rangeName)), 'toArray']];
7575

7676
if ($property->isArray) {
7777
$args['multiple'] = true;

src/Model/Attribute.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@ final class Attribute
2222

2323
private string $name;
2424

25-
/** @var (int|bool|null|string|string[]|string[][]|\Nette\PhpGenerator\Literal)[] */
25+
/** @var (int|bool|null|string|string[]|string[][]|\Nette\PhpGenerator\Literal|\Nette\PhpGenerator\Literal[])[] */
2626
private array $args;
2727

2828
/**
29-
* @param (int|bool|null|string|string[]|string[][]|\Nette\PhpGenerator\Literal)[] $args
29+
* @param (int|bool|null|string|string[]|string[][]|\Nette\PhpGenerator\Literal|\Nette\PhpGenerator\Literal[])[] $args
3030
*/
3131
public function __construct(string $name, array $args = [])
3232
{
@@ -40,7 +40,7 @@ public function name(): string
4040
}
4141

4242
/**
43-
* @return (int|bool|null|string|string[]|string[][]|\Nette\PhpGenerator\Literal)[]
43+
* @return (int|bool|null|string|string[]|string[][]|\Nette\PhpGenerator\Literal|\Nette\PhpGenerator\Literal[])[]
4444
*/
4545
public function args(): array
4646
{

tests/AttributeGenerator/ApiPlatformCoreAttributeGeneratorTest.php

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,9 @@ public function testGenerateClassAttributes(Class_ $class, array $attributes): v
5858

5959
public function provideGenerateClassAttributesCases(): \Generator
6060
{
61-
yield 'classical' => [new Class_('Res', new RdfResource('https://schema.org/Res')), [new Attribute('ApiResource', ['iri' => 'https://schema.org/Res'])]];
61+
yield 'classical' => [new Class_('Res', new RdfResource('https://schema.org/Res', new RdfGraph())), [new Attribute('ApiResource', ['iri' => 'https://schema.org/Res'])]];
6262

63-
$class = new Class_('WithOperations', new RdfResource('https://schema.org/WithOperations'));
63+
$class = new Class_('WithOperations', new RdfResource('https://schema.org/WithOperations', new RdfGraph()));
6464
$class->operations = [
6565
'item' => ['get' => ['route_name' => 'api_about_get']],
6666
'collection' => [],
@@ -71,9 +71,14 @@ public function provideGenerateClassAttributesCases(): \Generator
7171
$class->isAbstract = true;
7272
yield 'abstract' => [$class, []];
7373

74-
yield 'with short name' => [(new Class_('WithShortName', new RdfResource('https://schema.org/DifferentLocalName'))), [new Attribute('ApiResource', ['shortName' => 'DifferentLocalName', 'iri' => 'https://schema.org/DifferentLocalName'])]];
74+
$resource = new RdfResource('https://schema.org/MyEnum', new RdfGraph());
75+
$resource->add('rdfs:subClassOf', ['type' => 'uri', 'value' => TypesGenerator::SCHEMA_ORG_ENUMERATION]);
76+
$class = new Class_('Enum', $resource);
77+
yield 'enum' => [$class, []];
7578

76-
$class = new Class_('WithSecurity', new RdfResource('https://schema.org/WithSecurity'));
79+
yield 'with short name' => [(new Class_('WithShortName', new RdfResource('https://schema.org/DifferentLocalName', new RdfGraph()))), [new Attribute('ApiResource', ['shortName' => 'DifferentLocalName', 'iri' => 'https://schema.org/DifferentLocalName'])]];
80+
81+
$class = new Class_('WithSecurity', new RdfResource('https://schema.org/WithSecurity', new RdfGraph()));
7782
$class->security = "is_granted('ROLE_USER')";
7883
yield 'with security' => [$class, [new Attribute('ApiResource', ['iri' => 'https://schema.org/WithSecurity', 'security' => "is_granted('ROLE_USER')"])]];
7984
}
@@ -107,12 +112,4 @@ public function testGenerateUses(): void
107112
{
108113
$this->assertEquals([new Use_(ApiResource::class), new Use_(ApiProperty::class)], $this->generator->generateUses(new Class_('Res', new RdfResource('https://schema.org/Res', new RdfGraph()))));
109114
}
110-
111-
public function testGenerateNoUsesForEnum(): void
112-
{
113-
$graph = new RdfGraph();
114-
$myEnum = new RdfResource('https://schema.org/MyEnum', $graph);
115-
$myEnum->add('rdfs:subClassOf', ['type' => 'uri', 'value' => TypesGenerator::SCHEMA_ORG_ENUMERATION]);
116-
$this->assertSame([], $this->generator->generateUses(new Class_('MyEnum', $myEnum)));
117-
}
118115
}

tests/AttributeGenerator/ConstraintAttributeGeneratorTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
use Doctrine\Inflector\InflectorFactory;
2424
use EasyRdf\Graph as RdfGraph;
2525
use EasyRdf\Resource as RdfResource;
26+
use Nette\PhpGenerator\Literal;
2627
use PHPUnit\Framework\TestCase;
2728
use Psr\Log\NullLogger;
2829
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
@@ -105,7 +106,7 @@ public function provideGeneratePropertyAttributesCases(): \Generator
105106
$property->rangeName = 'Enum';
106107
$property->isEnum = true;
107108
$property->isArray = true;
108-
yield 'enum' => [$property, [new Attribute('Assert\Choice', ['callback' => ['Enum', 'toArray'], 'multiple' => true])]];
109+
yield 'enum' => [$property, [new Attribute('Assert\Choice', ['callback' => [new Literal('Enum::class'), 'toArray'], 'multiple' => true])]];
109110
}
110111

111112
public function testGenerateUses(): void

tests/e2e/customized/App/Entity/Person.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use ApiPlatform\Core\Annotation\ApiProperty;
88
use ApiPlatform\Core\Annotation\ApiResource;
99
use App\Attribute\MyCustomAttribute;
10+
use App\Enum\GenderType;
1011
use App\Model\MyCustomClass;
1112
use App\Model\MyCustomInterface;
1213
use Doctrine\ORM\Mapping as ORM;
@@ -68,6 +69,16 @@ class Person extends MyCustomClass implements MyCustomInterface
6869
#[Groups(['extra'])]
6970
private ?string $additionalName = null;
7071

72+
/**
73+
* Gender of something, typically a \[\[Person\]\], but possibly also fictional characters, animals, etc. While https://schema.org/Male and https://schema.org/Female may be used, text strings are also acceptable for people who do not identify as a binary gender. The \[\[gender\]\] property can also be used in an extended sense to cover e.g. the gender of sports teams. As with the gender of individuals, we do not try to enumerate all possibilities. A mixed-gender \[\[SportsTeam\]\] can be indicated with a text value of "Mixed".
74+
*
75+
* @see https://schema.org/gender
76+
*/
77+
#[ORM\Column(nullable: true)]
78+
#[ApiProperty(iri: 'https://schema.org/gender')]
79+
#[Assert\Choice(callback: [GenderType::class, 'toArray'])]
80+
private ?string $gender = null;
81+
7182
/**
7283
* Physical address of the item.
7384
*
@@ -160,6 +171,16 @@ public function getAdditionalName(): ?string
160171
return $this->additionalName;
161172
}
162173

174+
public function setGender(?string $gender): void
175+
{
176+
$this->gender = $gender;
177+
}
178+
179+
public function getGender(): ?string
180+
{
181+
return $this->gender;
182+
}
183+
163184
public function setAddress(?PostalAddress $address): void
164185
{
165186
$this->address = $address;
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Enum;
6+
7+
use MyCLabs\Enum\Enum;
8+
9+
/**
10+
* An enumeration of genders.
11+
*
12+
* @see https://schema.org/GenderType
13+
*/
14+
class GenderType extends Enum
15+
{
16+
/** @var string The female gender. */
17+
public const FEMALE = 'https://schema.org/Female';
18+
19+
/** @var string The male gender. */
20+
public const MALE = 'https://schema.org/Male';
21+
}

tests/e2e/original/App/Entity/Person.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use ApiPlatform\Core\Annotation\ApiProperty;
88
use ApiPlatform\Core\Annotation\ApiResource;
9+
use App\Enum\GenderType;
910
use Doctrine\ORM\Mapping as ORM;
1011
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
1112
use Symfony\Component\Serializer\Annotation\Groups;
@@ -59,6 +60,16 @@ class Person extends Thing
5960
#[Groups(['extra'])]
6061
private ?string $additionalName = null;
6162

63+
/**
64+
* Gender of something, typically a \[\[Person\]\], but possibly also fictional characters, animals, etc. While https://schema.org/Male and https://schema.org/Female may be used, text strings are also acceptable for people who do not identify as a binary gender. The \[\[gender\]\] property can also be used in an extended sense to cover e.g. the gender of sports teams. As with the gender of individuals, we do not try to enumerate all possibilities. A mixed-gender \[\[SportsTeam\]\] can be indicated with a text value of "Mixed".
65+
*
66+
* @see https://schema.org/gender
67+
*/
68+
#[ORM\Column(nullable: true)]
69+
#[ApiProperty(iri: 'https://schema.org/gender')]
70+
#[Assert\Choice(callback: [GenderType::class, 'toArray'])]
71+
private ?string $gender = null;
72+
6273
/**
6374
* Physical address of the item.
6475
*
@@ -148,6 +159,16 @@ public function getAdditionalName(): ?string
148159
return $this->additionalName;
149160
}
150161

162+
public function setGender(?string $gender): void
163+
{
164+
$this->gender = $gender;
165+
}
166+
167+
public function getGender(): ?string
168+
{
169+
return $this->gender;
170+
}
171+
151172
public function setAddress(?PostalAddress $address): void
152173
{
153174
$this->address = $address;
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Enum;
6+
7+
use MyCLabs\Enum\Enum;
8+
9+
/**
10+
* An enumeration of genders.
11+
*
12+
* @see https://schema.org/GenderType
13+
*/
14+
class GenderType extends Enum
15+
{
16+
/** @var string The female gender. */
17+
public const FEMALE = 'https://schema.org/Female';
18+
19+
/** @var string The male gender. */
20+
public const MALE = 'https://schema.org/Male';
21+
}

0 commit comments

Comments
 (0)