1. Go to this page and download the library: Download yapro/apiration-bundle library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
yapro / apiration-bundle example snippets
declare(strict_types=1);
namespace App;
use YaPro\ApiRationBundle\Marker\ApiRationObjectInterface;
class SimpleModel implements ApiRationObjectInterface
{
private string $varString;
private bool $varBoolean;
public function __construct(string $varString, bool $varBoolean) {
$this->varString = $varString;
$this->varBoolean = $varBoolean;
}
public function getVarString(): string
{
return $this->varString;
}
public function isVarBoolean(): bool
{
return $this->varBoolean;
}
}
declare(strict_types=1);
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
class AppController extends AbstractController
{
/**
* @Route("/api-json-test/simple-model")
*
* @param \App\SimpleModel $model
*
* @return SimpleModel
*/
public function getSimpleModel(SimpleModel $model, Request $request): SimpleModel
{
return $model;
}
}
declare(strict_types=1);
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\JsonResponse;
use YaPro\ApiRationBundle\Request\JsonRequest;
class AppController extends AbstractController
{
/**
* @Route("/search", methods={"POST"})
*
* @param JsonRequest $request
* @param ArticleRepository $articleRepository
*
* @return JsonResponse
*/
public function search(JsonRequest $request): JsonResponse
{
$userAddresses = $request->getArray(); // request: ["[email protected]", "[email protected]"]
// OR:
$myFieldValue = $request->getObject()->myField; // request: {"myField": "my value"}
return $this->json([]);
}
declare(strict_types=1);
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use YaPro\ApiRationBundle\Response\JsonLd\CollectionJsonLdResponse;
class AppController extends AbstractController
{
/**
* @Route("/list", methods={"GET"})
*/
public function list(Request $request, ArticleRepository $articleRepository, SerializerInterface $serializer)
{
$collection = $articleRepository->getList();
/*
function getList(): array {
return $this->createQueryBuilder('t')
->select('t')
->where('t.difficulty > 0')
->getQuery()
->getResult();
}
*/
$items = $serializer->normalize($collection, null, ['groups' => 'apiRead']);
return (new CollectionJsonLdResponse($request))->initData($items);
}
/**
* @Route("/search", methods={"GET"})
*
* @param Request $request
* @param ArticleRepository $articleRepository
*
* @return CollectionJsonLdResponse
*/
public function search(Request $request, ArticleRepository $articleRepository): CollectionJsonLdResponse
{
$response = new CollectionJsonLdResponse($request);
$searchValue = $request->query->get('searchValue', '');
if (empty($searchValue)) {
return $response;
}
$items = $this->getEntityManager()->getConnection()->fetchAll("
SELECT
id,
title
FROM Article
WHERE title LIKE :searchValue
ORDER BY createdAt DESC
LIMIT " . $response->getOffset() . ", " . $response->getLimit() . "
", [
'searchValue' => $searchValue,
]);
return $response->initData(
$items,
$this->getTotalItems()
);
}
}
$message = 'Validation errors';
$errors = [
'field_name' => 'The name cannot contain a number',
'field_lastname' => [
'The name cannot contain a number',
'Name must be at least 2 characters long',
],
];
throw new BadRequestException($message, $errors);