PHP code example of alexanevsky / input-manager-bundle
1. Go to this page and download the library: Download alexanevsky/input-manager-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/ */
alexanevsky / input-manager-bundle example snippets
use Alexanevsky\InputManagerBundle\InputManager;
public function __construct(
private InputManager $inputManager
) {}
class User
{
private string $firstName;
private string $lastName;
public function setFirstName(string $firstName): void
{
$this->firstName = $firstName;
}
public function setLastName(string $lastName): void
{
$this->lastName = $lastName;
}
}
use Alexanevsky\InputManagerBundle\Input\InputInterface;
class UserInput implements InputInterface
{
public string $firstName;
public string $lastName;
}
use Alexanevsky\InputManagerBundle\Input\InputInterface;
class UserInput implements InputInterface
{
private string $firstName;
private string $lastName;
public function getFirstName(): string
{
return $this->firstName;
}
public function setFirstName(string $firstName): void
{
$this->firstName = $firstName;
}
public function getLastName(): string
{
return $this->lastName;
}
public function setLastName(string $lastName): void
{
$this->lastName = $lastName;
}
}
class Address
{
private string $city;
private string $street;
private string $building;
// Setters and getters of properties...
}
class User
{
private Address $address;
// Setters and getters of properties...
}
class UserInput implements InputInterface
{
private Address $address;
}
class Category
{
private string $name;
// Getters and setters of properties...
}
class Article
{
private string $title;
private Category $category;
// Getters and setters of properties...
}
class CategoryInput implements InputInterface
{
public string $name;
}
class ArticleInput implements InputInterface
{
public string $title;
public CategoryInput $category;
}
use Alexanevsky\InputManagerBundle\Input\AbstractInputCollection;
use Alexanevsky\InputManagerBundle\Input\InputCollectionInterface;
class CategoryInput implements InputInterface
{
public string $name;
}
class CategoryInputCollection extends AbstractInputCollection
{
public function getClass(): string
{
return CategoryInput::class;
}
}
class ArticleInput implements InputInterface
{
public string $title;
public CategoryInputCollection $categories;
}
class Category
{
private string $id;
// Getters and setters of properties...
}
class Article
{
private Category $category;
// Getters and setters of properties...
}
use Alexanevsky\InputManagerBundle\Input\Attribute\EntityFromId;
class ArticleInput implements InputInterface
{
#[EntityFromId(Category::class)]
public Category $category;
}
$json = '{"category_id": 1}';
$json = '{"category": 1}';
class Category
{
private string $code;
// Getters and setters of properties...
}
class Article
{
private Category $category;
// Getters and setters of properties...
}
class ArticleInput implements InputInterface
{
#[EntityFromId(Category::class, 'code')]
public Category $category;
}
$json = '{"category_code": "cat"}';
$json = '{"category": "cat"}';
class ArticleInput implements InputInterface
{
#[EntityFromId(Category::class, 'code', 'identifier')]
public Category $category;
}
$json = '{"category_identifier": "cat"}';
class ArticleInput implements InputInterface
{
#[EntityFromId(Category::class, 'code', false)]
public Category $category;
}
class Category
{
private string $id;
// Getters and setters of properties...
}
class Article
{
private Collection $categories;
// Getters and setters of properties...
}
class ArticleInput implements InputInterface
{
#[EntityFromId(Category::class)]
public array $categories;
}
$json = '{"categories_ids": [1, 2]}';
$json = '{"categories": [1, 2]}';
use Alexanevsky\InputManagerBundle\Input\InputModifiableInterface;
class ArticleInput implements InputModifiableInterface
{
public string $title;
public \DateTime $createdAt;
public function modify(): void
{
$this->title .= ' from ' . $createdAt->format('m/d/Y');
}
}
echo $input->title; // 'Lorem Ipsum from 01/01/2023'
use Symfony\Component\Validator\Constraints as Assert;
class UserInput implements InputInterface
{
#[Assert\NotBlank]
public string $name;
#[Assert\NotBlank]
#[Assert\Email]
public string $email;
}
use Symfony\Component\Translation\TranslatableMessage;
/** @var TranslatableMessage[] $errors */
$errors = $this->inputManager->validate($input);
use App\Component\InputManager\InputValidator\AbstractInputValidator;
class UserInput implements InputInterface
{
public string $name;
public string $email;
}
class UserInputValidator extends AbstractInputValidator
{
public function validate(): array
{
$errors = [];
if (!$this->getInput()->name) {
$errors['name'] = new TranslatableMessage('Name is empty!');
}
if (!$this->getInput()->email) {
$errors['email'] = new TranslatableMessage('Email is empty!');
} elseif (filter_var($this->getInput()->email, FILTER_VALIDATE_EMAIL)) {
$errors['email'] = new TranslatableMessage('Email is incorrect!');
}
return $errors = [];
}
}
class UserInputValidator extends AbstractInputValidator
{
public function validateName(): ?TranslatableMessage
{
if (!$this->getInput()->name) {
return new TranslatableMessage('Name is empty!');
}
return null;
}
public function validateEmail(): ?TranslatableMessage
{
if (!$this->getInput()->email) {
return new TranslatableMessage('Email is empty!');
} elseif (filter_var($this->getInput()->email, FILTER_VALIDATE_EMAIL)) {
return new TranslatableMessage('Email is incorrect!');
}
return null;
}
}
use Alexanevsky\InputManagerBundle\InputValidator\Attribute\SetFromPayload;
class UserInputValidator extends AbstractInputValidator
{
#[SetFromPayload(true)]
public User $user;
public function __construct(
private UserRepository $usersRepository
) {
}
public function validateEmail(): ?TranslatableMessage
{
$foundedUser = $this->usersRepository->findOneBy(['email' => $this->getInput()->email]);
return !$foundedUser || $this->user === $foundedUser
? null
: new TranslatableMessage('User with this email already exists!');
}
}