Skip to content

Commit e006023

Browse files
author
abluchet
committed
Ability to specify identifier property of custom item operations
1 parent 797cea6 commit e006023

File tree

4 files changed

+66
-6
lines changed

4 files changed

+66
-6
lines changed

src/DataProvider/OperationDataProviderTrait.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,14 +86,16 @@ private function getSubresourceData($identifiers, array $attributes, array $cont
8686
private function extractIdentifiers(array $parameters, array $attributes)
8787
{
8888
if (isset($attributes['item_operation_name'])) {
89-
if (!isset($parameters['id'])) {
90-
throw new InvalidIdentifierException('Parameter "id" not found');
89+
$property = $attributes['identified_by'] ?? 'id';
90+
91+
if (!isset($parameters[$property])) {
92+
throw new InvalidIdentifierException(sprintf('Parameter "%s" not found', $property));
9193
}
9294

93-
$id = $parameters['id'];
95+
$id = $parameters[$property];
9496

9597
if (null !== $this->identifierConverter) {
96-
return $this->identifierConverter->convert((string) $id, $attributes['resource_class']);
98+
return $this->identifierConverter->convert((string) $id, $attributes['resource_class'], $attributes['identified_by'] ? [$attributes['identified_by']] : null);
9799
}
98100

99101
return $id;

src/Identifier/IdentifierConverter.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@ public function __construct(IdentifiersExtractorInterface $identifiersExtractor,
3939
/**
4040
* {@inheritdoc}
4141
*/
42-
public function convert(string $data, string $class): array
42+
public function convert(string $data, string $class, array $keys = null): array
4343
{
44-
$keys = $this->identifiersExtractor->getIdentifiersFromResourceClass($class);
44+
$keys = $keys ?: $this->identifiersExtractor->getIdentifiersFromResourceClass($class);
4545

4646
if (($numIdentifiers = \count($keys)) > 1) {
4747
$identifiers = CompositeIdentifierParser::parse($data);

src/Util/AttributesExtractor.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ public static function extractAttributes(array $attributes): array
6363
'persist' => (bool) ($attributes['_api_persist'] ?? true),
6464
];
6565

66+
if (isset($attributes['identifiedBy'])) {
67+
$result['identified_by'] = $attributes['identifiedBy'];
68+
}
69+
6670
return $result;
6771
}
6872
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity;
15+
16+
use ApiPlatform\Core\Annotation\ApiResource;
17+
use Doctrine\ORM\Mapping as ORM;
18+
19+
/**
20+
* Book.
21+
*
22+
* @author Antoine Bluchet <[email protected]>
23+
*
24+
* @ApiResource(itemOperations={
25+
* "get",
26+
* "put",
27+
* "get_by_isbn"={"method"="GET", "path"="/books/by_isbn/{isbn}.{_format}", "requirements"={"isbn"=".+"}, "defaults"={"identifiedBy"="isbn"}}
28+
* })
29+
* @ORM\Entity
30+
*/
31+
class Book
32+
{
33+
/**
34+
* @ORM\Column(type="integer")
35+
* @ORM\Id
36+
* @ORM\GeneratedValue(strategy="AUTO")
37+
*/
38+
private $id;
39+
40+
/**
41+
* @ORM\Column
42+
*/
43+
public $name;
44+
45+
/**
46+
* @ORM\Column(unique=true)
47+
*/
48+
public $isbn;
49+
50+
public function getId()
51+
{
52+
return $this->id;
53+
}
54+
}

0 commit comments

Comments
 (0)