Skip to content

Commit

Permalink
[WIP] Try to use DataProvider instead of auto-binding with Doctrine a…
Browse files Browse the repository at this point in the history
…nnotat… (#118)

* Try to use DataProvider instead of auto-binding with Doctrine annotations

* fix partially #117
  • Loading branch information
Rebolon committed Feb 11, 2019
1 parent c9d119c commit 8248023
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 0 deletions.
2 changes: 2 additions & 0 deletions config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,5 @@ services:
# needs to alias the current security.user.provider.concrete.in_memory to the class (seems not done in sf or security bundle)
Symfony\Component\Security\Core\User\InMemoryUserProvider:
alias: security.user.provider.concrete.in_memory

App\DataProvider\PingDataProvider: ~
35 changes: 35 additions & 0 deletions src/DataProvider/PingDataProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php
namespace App\DataProvider;


use ApiPlatform\Core\DataProvider\CollectionDataProviderInterface;
use ApiPlatform\Core\DataProvider\ItemDataProviderInterface;
use ApiPlatform\Core\DataProvider\RestrictedDataProviderInterface;
use App\Entity\Ping;

class PingDataProvider implements ItemDataProviderInterface, CollectionDataProviderInterface, RestrictedDataProviderInterface
{
public function supports(string $resourceClass, string $operationName = null, array $context = []): bool
{
return Ping::class === $resourceClass;
}

public function getItem(string $resourceClass, $id, string $operationName = null, array $context = [])
{
$pong = new Ping();
$pong->setId(1);

return $pong;
}

public function getCollection(string $resourceClass, string $operationName = null, array $context = [])
{
$pongOne = new Ping();
$pongOne->setId(1);
$pongTwo = new Ping();
$pongTwo->setId(2);

return [$pongOne, $pongTwo];
}

}
60 changes: 60 additions & 0 deletions src/Entity/Ping.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php
namespace App\Entity;

use ApiPlatform\Core\Annotation\ApiProperty;
use ApiPlatform\Core\Annotation\ApiResource;
use Symfony\Component\Validator\Constraints as Assert;

/**
* Class Ping
* @ApiResource(
* itemOperations={
* "get"={"method"="GET"}
* },
* collectionOperations={
* "get"={"method"="GET"}
* }
* )
* @package App\Ping
*/
class Ping
{
/**
* @Assert\Uuid()
* @ApiProperty(identifier=true)
* @var int
*/
protected $id;

/**
* @var string
*/
protected $pong = 'pong';

/**
* @return int
*/
public function getId(): int
{
return $this->id;
}

/**
* @param int $id
* @return Ping
*/
public function setId(int $id): Ping
{
$this->id = $id;

return $this;
}

/**
* @return string
*/
public function getPong()
{
return $this->pong;
}
}

0 comments on commit 8248023

Please sign in to comment.