This commit is contained in:
Pitchaya Boonsarngsuk
2019-07-05 16:56:10 +01:00
parent c3d3dcbff7
commit 6b6c18c79e
9 changed files with 367 additions and 53 deletions

View File

@@ -0,0 +1,71 @@
<?php
namespace App\DataProvider;
use ApiPlatform\Core\DataProvider\CollectionDataProviderInterface;
use ApiPlatform\Core\DataProvider\ItemDataProviderInterface;
use ApiPlatform\Core\DataProvider\RestrictedDataProviderInterface;
use ApiPlatform\Core\DataProvider\SubresourceDataProviderInterface;
use ApiPlatform\Core\Exception\InvalidValueException;
use ApiPlatform\Core\Exception\ResourceClassNotSupportedException;
use App\Entity\Creation;
use Symfony\Component\HttpFoundation\RequestStack;
use App\Entity\Circle;
final class CircleDataProvider implements CollectionDataProviderInterface, ItemDataProviderInterface, SubresourceDataProviderInterface, RestrictedDataProviderInterface
{
private $requestStack;
/**
* CircleDataProvider constructor.
* @param $requestStack
*/
public function __construct(RequestStack $requestStack)
{
$this->requestStack = $requestStack;
}
public function supports(string $resourceClass, string $operationName = null, array $context = []): bool
{
return Circle::class === $resourceClass;
}
public function getCollection(string $resourceClass, string $operationName = null): \Generator
{
$query = $this->requestStack->getCurrentRequest()->query;
$ids = $query->get('ids');
if ($ids !== null) {
if (!preg_match('/^\d+(,\d+)*$/', $ids)) {
throw new InvalidValueException('ids must be comma-seperated integers', 400);
//BadQueryStringException('ids must be comma-seperated integer', 400);
}
$ids = explode(',', $ids);
foreach ($ids as $id) {
yield new Circle((int)$id, $operationName, 'bla');
}
}
else {
for($i = 0; $i< 10; $i++) {
yield new Circle($i, $operationName, json_encode($ids));
}
}
// Returning array works too
// Return empty array if no data
}
public function getItem(string $resourceClass, $id, string $operationName = null, array $context = [])
{
//return null; for 404
$tmp = new Circle($id, is_string($id)? 'true' : 'false', 'a01');
$tmp->creations = [new Creation(55,'55'), new Creation(66,'66')];
return $tmp;
}
public function getSubresource(string $resourceClass, array $identifiers, array $context, string $operationName = null)
{
return $context['property'];
// TODO: Implement getSubresource() method.
}
}

131
api/src/Entity/Circle.php Normal file
View File

@@ -0,0 +1,131 @@
<?php
namespace App\Entity;
use ApiPlatform\Core\Annotation\ApiProperty;
use ApiPlatform\Core\Annotation\ApiResource;
use ApiPlatform\Core\Annotation\ApiSubresource;
use Symfony\Component\Validator\Constraints as Assert;
/**
* A circle
* @ApiResource(
* attributes={"pagination_enabled"=false},
* collectionOperations={
* "get"={
* "method"="GET",
* "swagger_context"={
* "summary"="Get the list of circles",
* "description"="Get the list of circles",
* "parameters" = {
* {
* "name"="ids",
* "in"="query",
* "description"="Comma-seperated list of circle ids. If not specified, will return all circles",
* "example"="123,456",
* "pattern"="^\d+(,\d+)*$",
* "type":"string"
* }
* }
* },
* "requirements"={"ids"="\d+(,\d+)*"}
* }
* },
* itemOperations={
* "get"={
* "method"="GET",
* "requirements"={"id"="\d+"}
* }
* }
* )
*/
class Circle
{
/**
* @var int Circle Id
* @ApiProperty(identifier=true)
* @Assert\NotBlank
* @Assert\Type(type="int")
*/
private $id;
/**
* @var string Circle name
* @Assert\NotBlank
*/
public $name = '';
/**
* @var string Booth location
* @Assert\NotBlank
*/
public $booth = '';
/**
* @var string Description
*/
public $description;
/**
* @var string Cutout image url
*/
public $cutout;
/**
* @var string Menu image url
*/
public $menu;
/**
* @var string Cover image url
*/
public $cover;
/**
* @var int Spaces
* @Assert\Type(type="int")
*/
public $sp;
/**
* @var string Type of circle
*/
public $type;
// Members
/**
* @ApiSubresource()
* @var integer[] Members
*/
public $members;
// Creations
/**
* @ApiSubresource()
* @var Creation[] Creations
*/
public $creations;
/**
* Circle constructor.
* @param int $id
* @param string $name
* @param string $booth
*/
public function __construct(int $id, string $name, string $booth)
{
$this->id = $id;
$this->name = $name;
$this->booth = $booth;
}
/**
* @return integer
*/
public function getId(): int
{
return $this->id;
}
}

View File

@@ -0,0 +1,62 @@
<?php
namespace App\Entity;
use ApiPlatform\Core\Annotation\ApiProperty;
use ApiPlatform\Core\Annotation\ApiResource;
use Symfony\Component\Validator\Constraints as Assert;
/**
* A Creation
* @ApiResource(
* attributes={"pagination_enabled"=false},
* collectionOperations={"get"},
* itemOperations={
* "get"={
* "method"="GET",
* "requirements"={"id"="\d+"}
* }
* }
* )
*/
class Creation
{
/**
* @var int Circle Id
* @ApiProperty(identifier=true)
* @Assert\NotBlank
* @Assert\Type(type="int")
*/
private $id;
/**
* @var string Creation name
* @Assert\NotBlank
*/
public $name = '';
/**
* @var integer Circle ID
* @Assert\NotBlank
*/
public $circleId;
/**
* @param int $id
* @param string $name
*/
public function __construct(int $id, string $name)
{
$this->id = $id;
$this->name = $name;
}
/**
* @return integer
*/
public function getId(): int
{
return $this->id;
}
}

View File

@@ -1,38 +0,0 @@
<?php
namespace App\Entity;
use ApiPlatform\Core\Annotation\ApiResource;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
/**
* This is a dummy entity. Remove it!
*
* @ApiResource
* @ORM\Entity
*/
class Greeting
{
/**
* @var int The entity Id
*
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;
/**
* @var string A nice person
*
* @ORM\Column
* @Assert\NotBlank
*/
public $name = '';
public function getId(): int
{
return $this->id;
}
}