Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Metadata/Extractor/ResourceExtractorTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ private function phpize(\SimpleXMLElement|array|null $resource, string $key, str
return (bool) XmlUtils::phpize($resource[$key]);
}

return \in_array($resource[$key], ['1', 'true', 1, true], false);
return \is_bool($resource[$key]) ? $resource[$key] : filter_var($resource[$key], \FILTER_VALIDATE_BOOLEAN);
}

throw new InvalidArgumentException(\sprintf('The property "%s" must be a "%s", "%s" given.', $key, $type, \gettype($resource[$key])));
Expand Down
2 changes: 1 addition & 1 deletion src/Metadata/Extractor/YamlPropertyExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ private function phpize(?array $resource, string $key, string $type, mixed $defa
'bool|string' => \in_array($resource[$key], ['1', '0', 1, 0, 'true', 'false', true, false], true) ? $this->phpize($resource, $key, 'bool') : $this->phpize($resource, $key, 'string'),
'string' => (string) $resource[$key],
'integer' => (int) $resource[$key],
'bool' => \in_array($resource[$key], ['1', 'true', 1, true], false),
'bool' => \is_bool($resource[$key]) ? $resource[$key] : filter_var($resource[$key], \FILTER_VALIDATE_BOOLEAN),
default => throw new InvalidArgumentException(\sprintf('The property "%s" must be a "%s", "%s" given.', $key, $type, \gettype($resource[$key]))),
};
}
Expand Down
16 changes: 14 additions & 2 deletions src/Metadata/Extractor/YamlResourceExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,8 @@ private function buildBase(array $resource): array
'securityPostDenormalizeMessage' => $this->phpize($resource, 'securityPostDenormalizeMessage', 'string'),
'securityPostValidation' => $this->resolveExpressionPlaceholder($this->phpize($resource, 'securityPostValidation', 'string', resolve: false)),
'securityPostValidationMessage' => $this->phpize($resource, 'securityPostValidationMessage', 'string'),
'input' => $this->phpize($resource, 'input', 'bool|string'),
'output' => $this->phpize($resource, 'output', 'bool|string'),
'input' => $this->buildInputOutput($resource, 'input'),
'output' => $this->buildInputOutput($resource, 'output'),
'normalizationContext' => $this->buildArrayValue($resource, 'normalizationContext'),
'denormalizationContext' => $this->buildArrayValue($resource, 'denormalizationContext'),
'collectDenormalizationErrors' => $this->phpize($resource, 'collectDenormalizationErrors', 'bool'),
Expand Down Expand Up @@ -306,6 +306,18 @@ private function buildMercure(array $resource): array|bool|string|null
return $resource['mercure'];
}

/**
* @return array{class?: string, name?: string}|bool|string|null
*/
private function buildInputOutput(array $resource, string $key): array|bool|string|null
{
if (\is_array($resource[$key] ?? null)) {
return $resource[$key];
}

return $this->phpize($resource, $key, 'bool|string');
}

private function buildMessenger(array $resource): bool|array|string|null
{
if (!\array_key_exists('messenger', $resource)) {
Expand Down
8 changes: 8 additions & 0 deletions src/Metadata/Tests/Extractor/YamlExtractorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -581,6 +581,14 @@ public function testInputAndOutputAreStrings(): void
$this->assertSame(Program::class.'Output', $resources[Program::class][0]['operations'][0]['output']);
}

public function testScalarTypes(): void
{
$resource = (new YamlResourceExtractor([__DIR__.'/yaml/scalar-types.yaml']))->getResources()[Program::class][0];

$this->assertFalse($resource['paginationEnabled']);
$this->assertSame(['class' => Program::class, 'name' => 'ProgramInput'], $resource['input']);
}

public function testExtendingYamlResourcesByMultipleYamlFiles(): void
{
$extractor = new YamlResourceExtractor([
Expand Down
29 changes: 29 additions & 0 deletions src/Metadata/Tests/Extractor/YamlPropertyExtractorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace ApiPlatform\Metadata\Tests\Extractor;

use ApiPlatform\Metadata\Extractor\YamlPropertyExtractor;
use ApiPlatform\Metadata\Tests\Fixtures\ApiResource\FileConfigDummy;
use PHPUnit\Framework\TestCase;

final class YamlPropertyExtractorTest extends TestCase
{
public function testBooleanStrings(): void
{
$properties = (new YamlPropertyExtractor([__DIR__.'/../Fixtures/FileConfigurations/properties_boolean_strings.yml']))->getProperties();

$this->assertFalse($properties[FileConfigDummy::class]['foo']['readable']);
$this->assertTrue($properties[FileConfigDummy::class]['foo']['writable']);
}
}
6 changes: 6 additions & 0 deletions src/Metadata/Tests/Extractor/yaml/scalar-types.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
resources:
ApiPlatform\Metadata\Tests\Fixtures\ApiResource\Program:
- paginationEnabled: 'false'
input:
class: ApiPlatform\Metadata\Tests\Fixtures\ApiResource\Program
name: ProgramInput
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
properties:
'ApiPlatform\Metadata\Tests\Fixtures\ApiResource\FileConfigDummy':
'foo':
readable: 'false'
writable: 'yes'
Loading