1. Go to this page and download the library: Download paysera/lib-api-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/ */
paysera / lib-api-bundle example snippets
declare(strict_types=1);
use Paysera\Component\Normalization\ObjectDenormalizerInterface;
use Paysera\Component\Normalization\NormalizerInterface;
use Paysera\Component\Normalization\TypeAwareInterface;
use Paysera\Component\Normalization\DenormalizationContext;
use Paysera\Component\Normalization\NormalizationContext;
use Paysera\Component\ObjectWrapper\ObjectWrapper;
class UserNormalizer implements ObjectDenormalizerInterface, NormalizerInterface, TypeAwareInterface
{
public function denormalize(ObjectWrapper $input, DenormalizationContext $context)
{
return (new User())
->setEmail($input->getRequiredString('email'))
->setPlainPassword($input->getRequiredString('password'))
->setAddress($context->denormalize($input->getObject('address'), Address::class))
;
}
public function normalize($user, NormalizationContext $normalizationContext)
{
return [
'id' => $user->getId(),
'email' => $user->getEmail(),
'address' => $user->getAddress(), // will be mapped automatically if type is classname
];
}
public function getType(): string
{
return User::class; // you can use anything here, but types can be guessed if FQCN are used
}
}
declare(strict_types=1);
use Symfony\Component\Routing\Annotation\Route;
use Paysera\Bundle\ApiBundle\Annotation\Body;
class ApiController
{
// ...
/**
* @Route("/users", methods="POST")
* @Body(parameterName="user")
*
* @param User $user
* @return User
*/
public function register(User $user)
{
$this->securityChecker->checkPermissions(Permissions::REGISTER_USER, $user);
$this->userManager->registerUser($user);
$this->entityManager->flush();
return $user;
}
}
declare(strict_types=1);
use Symfony\Component\Routing\Annotation\Route;
use Paysera\Bundle\ApiBundle\Attribute\Body;
class ApiController
{
// ...
#[Route(path: '/users', methods: 'POST')]
#[Body(parameterName: 'user')]
public function register(User $user): User
{
$this->securityChecker->checkPermissions(Permissions::REGISTER_USER, $user);
$this->userManager->registerUser($user);
$this->entityManager->flush();
return $user;
}
}
declare(strict_types=1);
use Symfony\Component\Routing\Annotation\Route;
use Paysera\Bundle\ApiBundle\Annotation\PathAttribute;
class ApiController
{
// ...
/**
* @Route("/users/{userId}", methods="GET")
* @PathAttribute(parameterName="user", pathPartName="userId")
*
* @param User $user
* @return User
*/
public function getUser(User $user)
{
$this->securityChecker->checkPermissions(Permissions::ACCESS_USER, $user);
return $user;
}
}
declare(strict_types=1);
use Symfony\Component\Routing\Annotation\Route;
use Paysera\Bundle\ApiBundle\Attribute\PathAttribute;
class ApiController
{
// ...
#[Route(path: '/users/{userId}', methods: 'GET')]
#[PathAttribute(parameterName: 'user', pathPartName: 'userId')]
public function getUser(User $user): User
{
$this->securityChecker->checkPermissions(Permissions::ACCESS_USER, $user);
return $user;
}
}
declare(strict_types=1);
use Paysera\Bundle\ApiBundle\Service\PathAttributeResolver\PathAttributeResolverInterface;
class FindUserPathAttributeResolver implements PathAttributeResolverInterface
{
// ...
public function resolveFromAttribute($attributeValue)
{
return $this->repository->find($attributeValue);
}
}
declare(strict_types=1);
use Symfony\Component\Routing\Annotation\Route;
use Paysera\Bundle\ApiBundle\Annotation\Query;
use Paysera\Pagination\Entity\Pager;
use Paysera\Bundle\ApiBundle\Entity\PagedQuery;
class ApiController
{
// ...
/**
* @Route("/users", methods="GET")
* @Query(parameterName="filter")
* @Query(parameterName="pager")
*
* @param UserFilter $filter
* @param Pager $pager
* @return PagedQuery
*/
public function getUsers(UserFilter $filter, Pager $pager)
{
$this->securityChecker->checkPermissions(Permissions::SEARCH_USERS, $filter);
$configuredQuery = $this->userRepository->buildConfiguredQuery($filter);
return new PagedQuery($configuredQuery, $pager);
}
}
declare(strict_types=1);
use Symfony\Component\Routing\Annotation\Route;
use Paysera\Bundle\ApiBundle\Attribute\Query;
use Paysera\Pagination\Entity\Pager;
use Paysera\Bundle\ApiBundle\Entity\PagedQuery;
class ApiController
{
// ...
#[Route(path: '/users', methods: 'GET')]
#[Query(parameterName: 'filter')]
#[Query(parameterName: 'pager')]
public function getUsers(UserFilter $filter, Pager $pager): PagedQuery
{
$this->securityChecker->checkPermissions(Permissions::SEARCH_USERS, $filter);
$configuredQuery = $this->userRepository->buildConfiguredQuery($filter);
return new PagedQuery($configuredQuery, $pager);
}
}
declare(strict_types=1);
use Paysera\Component\Normalization\ObjectDenormalizerInterface;
use Paysera\Component\Normalization\TypeAwareInterface;
use Paysera\Component\Normalization\DenormalizationContext;
use Paysera\Component\ObjectWrapper\ObjectWrapper;
class UserFilterDenormalizer implements ObjectDenormalizerInterface, TypeAwareInterface
{
public function denormalize(ObjectWrapper $input, DenormalizationContext $context)
{
return (new UserFilter())
->setEmail($input->getString('email'))
->setCountryCode($input->getString('country_code'))
;
}
public function getType(): string
{
return UserFilter::class; // you can use anything here, but types can be guessed if FQCN are used
}
}
declare(strict_types=1);
use Paysera\Pagination\Entity\OrderingConfiguration;
use Paysera\Pagination\Entity\Doctrine\ConfiguredQuery;
class UserRepository extends Repository
{
public function buildConfiguredQuery(UserFilter $filter)
{
// just an example – should add conditions only when they're set
$queryBuilder = $this->createQueryBuilder('u')
->join('u.address', 'a')
->join('a.country', 'c')
->andWhere('u.email = :email')
->andWhere('c.code = :countryCode')
->setParameter('email', $filter->getEmail())
->setParameter('countryCode', $filter->getCountryCode())
;
return (new ConfiguredQuery($queryBuilder))
->addOrderingConfiguration('email', new OrderingConfiguration('u.email', 'email'))
->addOrderingConfiguration(
'country_code',
new OrderingConfiguration('c.code', 'address.country.code')
)
;
}
}
// ... begining of controller action
$configuredQuery = $this->userRepository->buildConfiguredQuery($filter);
$configuredQuery->setMaximumOffset(1000); // optionally override maximum offset
return (new PagedQuery($configuredQuery, $pager))
// optionally override total count strategy
->setTotalCountStrategy(PagedQuery::TOTAL_COUNT_STRATEGY_OPTIONAL)
;
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.