Skip to content

Commit e634e2f

Browse files
authored
fix: manage lang string directly in range (#376)
1 parent cb37c41 commit e634e2f

File tree

4 files changed

+39
-3
lines changed

4 files changed

+39
-3
lines changed

src/PhpTypeConverter.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121

2222
final class PhpTypeConverter implements PhpTypeConverterInterface
2323
{
24+
private const RDF_LANG_STRING = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#langString';
25+
2426
/**
2527
* Is this type a datatype?
2628
*/
@@ -107,16 +109,17 @@ private function getNonArrayType(SchemaProperty $property, array $classes): ?str
107109
}
108110

109111
/**
110-
* This is a hack to detect internationalized strings in ActivityStreams.
112+
* This is a hack to detect internationalized strings.
111113
*
112114
* @todo find something smarter to detect this kind of strings
113115
*/
114116
private function isLangString(RdfResource $range): bool
115117
{
116-
return $range->isBNode() &&
118+
return self::RDF_LANG_STRING === $this->getUri($range)
119+
|| ($range->isBNode() &&
117120
null !== ($unionOf = $range->get('owl:unionOf')) &&
118121
null !== ($rdfFirst = $unionOf->get('rdf:first')) &&
119-
'http://www.w3.org/1999/02/22-rdf-syntax-ns#langString' === $rdfFirst->getUri();
122+
self::RDF_LANG_STRING === $rdfFirst->getUri());
120123
}
121124

122125
private function getUri(RdfResource $range): string

tests/Command/GenerateCommandTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,30 @@ public function testSupersededProperties(): void
424424
$this->assertStringNotContainsString('protected', $creativeWork);
425425
}
426426

427+
public function testActivityStreams(): void
428+
{
429+
$outputDir = __DIR__.'/../../build/activity-streams';
430+
$config = __DIR__.'/../config/activity-streams.yaml';
431+
432+
$this->fs->mkdir($outputDir);
433+
434+
$commandTester = new CommandTester(new GenerateCommand());
435+
$this->assertEquals(0, $commandTester->execute(['output' => $outputDir, 'config' => $config]));
436+
437+
$object = file_get_contents("$outputDir/App/Entity/Object_.php");
438+
439+
$this->assertStringContainsString(<<<'PHP'
440+
/**
441+
* The content of the object.
442+
*
443+
* @see http://www.w3.org/ns/activitystreams#content
444+
*/
445+
#[ApiProperty(iri: 'http://www.w3.org/ns/activitystreams#content')]
446+
private ?string $content = null;
447+
PHP
448+
, $object);
449+
}
450+
427451
public function testGenerationWithoutConfigFileQuestion(): void
428452
{
429453
// No config file is given.

tests/TypesGeneratorTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,14 @@ public function testGenerateAllResolveTypes(): void
170170

171171
$finder = new Finder();
172172
self::assertSame(2, $finder->files()->in($this->outputDir)->count());
173+
174+
$competencyWorldEntity = file_get_contents("$this->outputDir/App/Entity/CompetencyWorldEntity.php");
175+
$this->assertStringContainsString('class CompetencyWorldEntity extends Thing', $competencyWorldEntity);
176+
$this->assertStringContainsString('private ?int $id = null;', $competencyWorldEntity);
177+
$this->assertStringContainsString('private string $hasAppellation;', $competencyWorldEntity);
178+
$this->assertStringContainsString('public function getId(): ?int', $competencyWorldEntity);
179+
$this->assertStringContainsString('public function setHasAppellation(string $hasAppellation): void', $competencyWorldEntity);
180+
$this->assertStringContainsString('public function getHasAppellation(): string', $competencyWorldEntity);
173181
}
174182

175183
public function testGenerateVocabAllTypes(): void
@@ -228,6 +236,7 @@ private function getGraphs(): array
228236

229237
$nodeGraph->addResource('https://gitlab.com/mmorg/nodefr-2/hasAppellation', 'rdf:type', 'rdf:Property');
230238
$nodeGraph->addResource('https://gitlab.com/mmorg/nodefr-2/hasAppellation', 'schema:domainIncludes', 'https://gitlab.com/mmorg/nodefr-2/CompetencyWorldEntity');
239+
$nodeGraph->addResource('https://gitlab.com/mmorg/nodefr-2/hasAppellation', 'schema:rangeIncludes', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#langString');
231240
$nodeGraph->addResource('https://gitlab.com/mmorg/nodefr-2/hasAppellation', 'schema:rangeIncludes', 'https://schema.org/Text');
232241

233242
$graph = new RdfGraph(SchemaGeneratorConfiguration::SCHEMA_ORG_URI);

0 commit comments

Comments
 (0)