Skip to content

Commit 8248023

Browse files
authored
[WIP] Try to use DataProvider instead of auto-binding with Doctrine annotat… (#118)
* Try to use DataProvider instead of auto-binding with Doctrine annotations * fix partially #117
1 parent c9d119c commit 8248023

File tree

3 files changed

+97
-0
lines changed

3 files changed

+97
-0
lines changed

config/services.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,3 +108,5 @@ services:
108108
# needs to alias the current security.user.provider.concrete.in_memory to the class (seems not done in sf or security bundle)
109109
Symfony\Component\Security\Core\User\InMemoryUserProvider:
110110
alias: security.user.provider.concrete.in_memory
111+
112+
App\DataProvider\PingDataProvider: ~
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
namespace App\DataProvider;
3+
4+
5+
use ApiPlatform\Core\DataProvider\CollectionDataProviderInterface;
6+
use ApiPlatform\Core\DataProvider\ItemDataProviderInterface;
7+
use ApiPlatform\Core\DataProvider\RestrictedDataProviderInterface;
8+
use App\Entity\Ping;
9+
10+
class PingDataProvider implements ItemDataProviderInterface, CollectionDataProviderInterface, RestrictedDataProviderInterface
11+
{
12+
public function supports(string $resourceClass, string $operationName = null, array $context = []): bool
13+
{
14+
return Ping::class === $resourceClass;
15+
}
16+
17+
public function getItem(string $resourceClass, $id, string $operationName = null, array $context = [])
18+
{
19+
$pong = new Ping();
20+
$pong->setId(1);
21+
22+
return $pong;
23+
}
24+
25+
public function getCollection(string $resourceClass, string $operationName = null, array $context = [])
26+
{
27+
$pongOne = new Ping();
28+
$pongOne->setId(1);
29+
$pongTwo = new Ping();
30+
$pongTwo->setId(2);
31+
32+
return [$pongOne, $pongTwo];
33+
}
34+
35+
}

src/Entity/Ping.php

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
namespace App\Entity;
3+
4+
use ApiPlatform\Core\Annotation\ApiProperty;
5+
use ApiPlatform\Core\Annotation\ApiResource;
6+
use Symfony\Component\Validator\Constraints as Assert;
7+
8+
/**
9+
* Class Ping
10+
* @ApiResource(
11+
* itemOperations={
12+
* "get"={"method"="GET"}
13+
* },
14+
* collectionOperations={
15+
* "get"={"method"="GET"}
16+
* }
17+
* )
18+
* @package App\Ping
19+
*/
20+
class Ping
21+
{
22+
/**
23+
* @Assert\Uuid()
24+
* @ApiProperty(identifier=true)
25+
* @var int
26+
*/
27+
protected $id;
28+
29+
/**
30+
* @var string
31+
*/
32+
protected $pong = 'pong';
33+
34+
/**
35+
* @return int
36+
*/
37+
public function getId(): int
38+
{
39+
return $this->id;
40+
}
41+
42+
/**
43+
* @param int $id
44+
* @return Ping
45+
*/
46+
public function setId(int $id): Ping
47+
{
48+
$this->id = $id;
49+
50+
return $this;
51+
}
52+
53+
/**
54+
* @return string
55+
*/
56+
public function getPong()
57+
{
58+
return $this->pong;
59+
}
60+
}

0 commit comments

Comments
 (0)