PHP code example of xactsystems / type-hint-hydrator
1. Go to this page and download the library: Download xactsystems/type-hint-hydrator 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/ */
xactsystems / type-hint-hydrator example snippets
return [
...
Xact\TypeHintHydrator\XactTypeHintHydrator::class => ['all' => true],
];
namespace App;
class Author
{
/**
* @var int|null
*/
public $objectId;
public int $noneNullId;
/**
* @var string|null
*/
public $fullName;
public float $myFloat;
/**
* @var \App\Book[]
*/
public $books;
/**
* @var \App\Authors[]
*/
public array $authors;
/**
* @var array<\App\References>
*/
public array $references;
}
use Xact\TypeHintHydrator\TypeHintHydrator;
...
public function update(Request $request, EntityManagerInterface $em, TypeHintHydrator $hydrator): JsonResponse
{
$author = new Author();
$hydrator->handleRequest($request, $author);
if (!$hydrator->isValid()) {
return JsonResponse::fromJsonString($hydrator->getJsonErrors(), JsonResponse::HTTP_BAD_REQUEST);
}
$em->persist($author);
$em->flush();
return new JsonResponse::fromJsonString(json_encode($author));
}
use Xact\TypeHintHydrator\TypeHintHydrator;
...
/**
* @Route("/author/{id}", methods={"POST"})
* @ParamConverter("author", class="App\Entity\Author")
*/
public function update(Author $author, Request $request, TypeHintHydrator $hydrator): Response
{
$hydrator->handleRequest($request, $author);
if (!$hydrator->isValid()) {
return JsonResponse::fromJsonString($hydrator->getJsonErrors(), JsonResponse::HTTP_BAD_REQUEST);
}
$em->persist($author);
$em->flush();
return new JsonResponse::fromJsonString(json_encode($author));
}
hydrateObject(array $values, object $target, bool $validate = true, $constraints = null, $groups = null): object
handleRequest(Request $request, object $target, bool $validate = true, $constraints = null, $groups = null): object
isValid()
getErrors()
getJsonErrors()