Skip to content
Merged
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
6 changes: 4 additions & 2 deletions src/Metadata/ApiResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -969,8 +969,9 @@ public function __construct(
array|Parameters|null $parameters = null,
protected ?bool $strictQueryParameterValidation = null,
protected ?bool $hideHydraOperation = null,
?bool $jsonStream = null,
protected ?bool $jsonStream = null,
protected array $extraProperties = [],
?bool $map = null,
) {
parent::__construct(
shortName: $shortName,
Expand Down Expand Up @@ -1016,7 +1017,8 @@ class: $class,
strictQueryParameterValidation: $strictQueryParameterValidation,
hideHydraOperation: $hideHydraOperation,
jsonStream: $jsonStream,
extraProperties: $extraProperties
extraProperties: $extraProperties,
map: $map
);

/* @var Operations<HttpOperation> $operations> */
Expand Down
2 changes: 2 additions & 0 deletions src/Metadata/Delete.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ public function __construct(
protected ?bool $hideHydraOperation = null,
?bool $jsonStream = null,
array $extraProperties = [],
?bool $map = null,
) {
parent::__construct(
method: 'DELETE',
Expand Down Expand Up @@ -183,6 +184,7 @@ class: $class,
strictQueryParameterValidation: $strictQueryParameterValidation,
hideHydraOperation: $hideHydraOperation,
stateOptions: $stateOptions,
map: $map
);
}
}
2 changes: 2 additions & 0 deletions src/Metadata/Error.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ public function __construct(
?OptionsInterface $stateOptions = null,
?bool $hideHydraOperation = null,
?bool $jsonStream = null,
?bool $map = null,
array $extraProperties = [],
) {
parent::__construct(
Expand Down Expand Up @@ -171,6 +172,7 @@ class: $class,
stateOptions: $stateOptions,
hideHydraOperation: $hideHydraOperation,
jsonStream: $jsonStream,
map: $map,
extraProperties: $extraProperties,
);
}
Expand Down
1 change: 1 addition & 0 deletions src/Metadata/Extractor/XmlResourceExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ private function buildBase(\SimpleXMLElement $resource): array
'read' => $this->phpize($resource, 'read', 'bool'),
'write' => $this->phpize($resource, 'write', 'bool'),
'jsonStream' => $this->phpize($resource, 'jsonStream', 'bool'),
'map' => $this->phpize($resource, 'map', 'bool'),
];
}

Expand Down
1 change: 1 addition & 0 deletions src/Metadata/Extractor/YamlResourceExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ private function buildBase(array $resource): array
'read' => $this->phpize($resource, 'read', 'bool'),
'write' => $this->phpize($resource, 'write', 'bool'),
'jsonStream' => $this->phpize($resource, 'jsonStream', 'bool'),
'map' => $this->phpize($resource, 'map', 'bool'),
];
}

Expand Down
1 change: 1 addition & 0 deletions src/Metadata/Extractor/schema/resources.xsd
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,7 @@
<xsd:attribute type="xsd:string" name="securityPostValidationMessage"/>
<xsd:attribute type="xsd:boolean" name="collectDenormalizationErrors"/>
<xsd:attribute type="xsd:boolean" name="jsonStream"/>
<xsd:attribute type="xsd:boolean" name="map"/>
</xsd:attributeGroup>

<xsd:attributeGroup name="extendedBase">
Expand Down
2 changes: 2 additions & 0 deletions src/Metadata/Get.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ public function __construct(
protected ?bool $hideHydraOperation = null,
?bool $jsonStream = null,
array $extraProperties = [],
?bool $map = null,
) {
parent::__construct(
uriTemplate: $uriTemplate,
Expand Down Expand Up @@ -182,6 +183,7 @@ class: $class,
hideHydraOperation: $hideHydraOperation,
jsonStream: $jsonStream,
extraProperties: $extraProperties,
map: $map
);
}
}
2 changes: 2 additions & 0 deletions src/Metadata/GetCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ public function __construct(
?bool $jsonStream = null,
array $extraProperties = [],
private ?string $itemUriTemplate = null,
?bool $map = null,
) {
parent::__construct(
uriTemplate: $uriTemplate,
Expand Down Expand Up @@ -183,6 +184,7 @@ class: $class,
strictQueryParameterValidation: $strictQueryParameterValidation,
hideHydraOperation: $hideHydraOperation,
stateOptions: $stateOptions,
map: $map
);
}

Expand Down
4 changes: 3 additions & 1 deletion src/Metadata/GraphQl/Operation.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ public function __construct(
mixed $rules = null,
?string $policy = null,
array $extraProperties = [],
?bool $map = null,
) {
parent::__construct(
shortName: $shortName,
Expand Down Expand Up @@ -141,7 +142,8 @@ class: $class,
queryParameterValidationEnabled: $queryParameterValidationEnabled,
rules: $rules,
policy: $policy,
extraProperties: $extraProperties
extraProperties: $extraProperties,
map: $map
);
}

Expand Down
4 changes: 3 additions & 1 deletion src/Metadata/GraphQl/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ public function __construct(
array $extraProperties = [],

protected ?bool $nested = null,
?bool $map = null,
) {
parent::__construct(
resolver: $resolver,
Expand Down Expand Up @@ -132,7 +133,8 @@ class: $class,
queryParameterValidationEnabled: $queryParameterValidationEnabled,
policy: $policy,
rules: $rules,
extraProperties: $extraProperties
extraProperties: $extraProperties,
map: $map
);
}

Expand Down
2 changes: 2 additions & 0 deletions src/Metadata/GraphQl/Subscription.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ public function __construct(
mixed $rules = null,
?string $policy = null,
array $extraProperties = [],
?bool $map = null,
) {
parent::__construct(
resolver: $resolver,
Expand Down Expand Up @@ -131,6 +132,7 @@ class: $class,
policy: $policy,
rules: $rules,
extraProperties: $extraProperties,
map: $map
);
}
}
4 changes: 3 additions & 1 deletion src/Metadata/HttpOperation.php
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ public function __construct(
?bool $queryParameterValidationEnabled = null,
?bool $jsonStream = null,
array $extraProperties = [],
?bool $map = null,
) {
$this->formats = (null === $formats || \is_array($formats)) ? $formats : [$formats];
$this->inputFormats = (null === $inputFormats || \is_array($inputFormats)) ? $inputFormats : [$inputFormats];
Expand Down Expand Up @@ -279,7 +280,8 @@ class: $class,
strictQueryParameterValidation: $strictQueryParameterValidation,
hideHydraOperation: $hideHydraOperation,
jsonStream: $jsonStream,
extraProperties: $extraProperties
extraProperties: $extraProperties,
map: $map
);
}

Expand Down
14 changes: 14 additions & 0 deletions src/Metadata/Metadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ public function __construct(
protected ?bool $strictQueryParameterValidation = null,
protected ?bool $hideHydraOperation = null,
protected ?bool $jsonStream = null,
protected ?bool $map = null,
protected array $extraProperties = [],
) {
if (\is_array($parameters) && $parameters) {
Expand All @@ -90,6 +91,19 @@ public function __construct(
$this->parameters = $parameters;
}

public function canMap(): ?bool
{
return $this->map;
}

public function withMap(bool $map): static
{
$self = clone $this;
$self->map = $map;

return $self;
}

public function getShortName(): ?string
{
return $this->shortName;
Expand Down
2 changes: 2 additions & 0 deletions src/Metadata/NotExposed.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ public function __construct(
$processor = null,
array $extraProperties = [],
?OptionsInterface $stateOptions = null,
?bool $map = null,
) {
parent::__construct(
method: $method,
Expand Down Expand Up @@ -182,6 +183,7 @@ class: $class,
processor: $processor,
stateOptions: $stateOptions,
extraProperties: $extraProperties,
map: $map
);
}
}
2 changes: 2 additions & 0 deletions src/Metadata/Operation.php
Original file line number Diff line number Diff line change
Expand Up @@ -814,6 +814,7 @@ public function __construct(
protected ?bool $hideHydraOperation = null,
protected ?bool $jsonStream = null,
protected array $extraProperties = [],
?bool $map = null,
) {
parent::__construct(
shortName: $shortName,
Expand Down Expand Up @@ -861,6 +862,7 @@ class: $class,
hideHydraOperation: $hideHydraOperation,
jsonStream: $jsonStream,
extraProperties: $extraProperties,
map: $map
);
}

Expand Down
4 changes: 3 additions & 1 deletion src/Metadata/Patch.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ public function __construct(
?bool $hideHydraOperation = null,
?bool $jsonStream = null,
array $extraProperties = [],
?bool $map = null,
) {
parent::__construct(
method: 'PATCH',
Expand Down Expand Up @@ -182,7 +183,8 @@ class: $class,
strictQueryParameterValidation: $strictQueryParameterValidation,
hideHydraOperation: $hideHydraOperation,
jsonStream: $jsonStream,
extraProperties: $extraProperties
extraProperties: $extraProperties,
map: $map
);
}
}
4 changes: 3 additions & 1 deletion src/Metadata/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ public function __construct(
private ?string $itemUriTemplate = null,
?bool $strictQueryParameterValidation = null,
?bool $hideHydraOperation = null,
?bool $map = null,
) {
parent::__construct(
method: 'POST',
Expand Down Expand Up @@ -183,7 +184,8 @@ class: $class,
strictQueryParameterValidation: $strictQueryParameterValidation,
hideHydraOperation: $hideHydraOperation,
jsonStream: $jsonStream,
extraProperties: $extraProperties
extraProperties: $extraProperties,
map: $map
);
}

Expand Down
4 changes: 3 additions & 1 deletion src/Metadata/Put.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ public function __construct(
?bool $strictQueryParameterValidation = null,
?bool $hideHydraOperation = null,
private ?bool $allowCreate = null,
?bool $map = null,
) {
parent::__construct(
method: 'PUT',
Expand Down Expand Up @@ -183,7 +184,8 @@ class: $class,
strictQueryParameterValidation: $strictQueryParameterValidation,
hideHydraOperation: $hideHydraOperation,
jsonStream: $jsonStream,
extraProperties: $extraProperties
extraProperties: $extraProperties,
map: $map
);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<?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\Resource\Factory;

use ApiPlatform\Doctrine\Odm\State\Options as OdmOptions;
use ApiPlatform\Doctrine\Orm\State\Options;
use ApiPlatform\Metadata\Resource\ResourceMetadataCollection;
use Symfony\Component\ObjectMapper\Metadata\Mapping;
use Symfony\Component\ObjectMapper\Metadata\ObjectMapperMetadataFactoryInterface;

class ObjectMapperMetadataCollectionFactory implements ResourceMetadataCollectionFactoryInterface
{
public function __construct(
private readonly ResourceMetadataCollectionFactoryInterface $decorated,
private readonly ObjectMapperMetadataFactoryInterface $objectMapperMetadata,
) {
}

public function create(string $resourceClass): ResourceMetadataCollection
{
$resourceMetadataCollection = $this->decorated->create($resourceClass);

foreach ($resourceMetadataCollection as $key => $resourceMetadata) {
$operations = $resourceMetadata->getOperations();

if (!$operations) {
continue;
}

foreach ($operations as $operationKey => $operation) {
if (null !== $operation->canMap()) {
continue;
}

$entityClass = null;
if (($options = $operation->getStateOptions()) && $options instanceof Options && $options->getEntityClass()) {
$entityClass = $options->getEntityClass();
}

if (($options = $operation->getStateOptions()) && $options instanceof OdmOptions && $options->getDocumentClass()) {
$entityClass = $options->getDocumentClass();
}

$class = $operation->getInput()['class'] ?? $operation->getClass();
$entityMap = null;

// Look for Mapping metadata
if ($this->canBeMapped($class) || ($entityClass && ($entityMap = $this->canBeMapped($entityClass)))) {
$found = true;
if ($entityMap) {
foreach ($entityMap as $mapping) {
if ($found = ($mapping->source === $operation->getClass() || $mapping->target === $operation->getClass())) {
break;
}
}
}

if (!$found) {
continue;
}

$operations->add($operationKey, $operation->withMap(true));
}
}

$resourceMetadataCollection[$key] = $resourceMetadata->withOperations($operations);
}

return $resourceMetadataCollection;
}

/**
* @return bool|list<Mapping>
*/
private function canBeMapped(string $class): bool|array
{
try {
$r = new \ReflectionClass($class);
if (!$r->isInstantiable() || !($mapping = $this->objectMapperMetadata->create($r->newInstanceWithoutConstructor(), null, ['_api_check_can_be_mapped' => true]))) {
return false;
}
} catch (\ReflectionException $e) {
return false;
}

return $mapping;
}
}
Loading
Loading