PHP code example of linio / input

1. Go to this page and download the library: Download linio/input 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/ */

    

linio / input example snippets




namespace Linio\Api\Handler;

use Linio\Component\Input\InputHandler;

class RegistrationHandler extends InputHandler
{
    public function define()
    {
        $this->add('referrer', 'string');
        $this->add('registration_date', 'datetime');

        $user = $this->add('user', 'Linio\Model\User');
        $user->add('name', 'string');
        $user->add('email', 'string');
        $user->add('age', 'integer');
    }
}



namespace Linio\Api\Controller;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

class RegistrationController
{
    public function registerAction(Request $request): Response
    {
        $input = new RegistrationHandler();
        $input->bind($request->request->all());

        if (!$input->isValid()) {
            return new Response($input->getErrorsAsString());
        }

        $data = $input->getData();
        $data['referrer']; // string
        $data['registration_date']; // \DateTime
        $data['user']; // Linio\Model\User

        return new Response(['message' => 'Valid!']);
    }
}



class GuidNode extends BaseNode
{
    public function __construct()
    {
        $this->addConstraint(new Linio\Component\Input\Constraint\GuidValue());
    }
}

$typeHandler = new Linio\Component\Input\TypeHandler();
$typeHandler->addType('guid', GuidNode::class);

$input = new RegistrationHandler();
$input->setTypeHandler($typeHandler);




use Linio\Component\Input\Constraint\Pattern;

class RegistrationHandler extends InputHandler
{
    public function define()
    {
        $this->add('referrer', 'string', ['ing', ['constraints' => [new Pattern('/^\S+@\S+\.\S+$/')]]);
        $user->add('age', 'integer');
    }
}



namespace Linio\Api\Handler\Transformer;

use Doctrine\Common\Persistence\ObjectRepository;
use Linio\Component\Input\Transformer\TransformerInterface;

class IdTransformer implements TransformerInterface
{
    /**
     * @var ObjectRepository
     */
    protected $repository;

    public function transform($value)
    {
        try {
            $entity = $this->repository->find($value);
        } catch (\Exception $e) {
            return null;
        }

        return $entity;
    }

    public function setRepository(ObjectRepository $repository)
    {
        $this->repository = $repository;
    }
}




use Linio\Api\Handler\Transformer\IdTransformer;

class RegistrationHandler extends InputHandler
{
    /**
     * @var IdTransformer
     */
    protected $idTransformer;

    public function define()
    {
        $this->add('store_id', 'string', ['transformer' => $this->idTransformer]);
    }

    public function setIdTransformer(IdTransformer $idTransformer)
    {
        $this->idTransformer = $idTransformer;
    }
}



use Linio\Component\Input\Instantiator\ConstructInstantiator;
use Linio\Component\Input\Instantiator\ReflectionInstantiator;

class RegistrationHandler extends InputHandler
{
    public function define()
    {
        $this->add('foobar', 'My\Foo\Class', ['instantiator' => new ConstructInstantiator()]);
        $this->add('barfoo', 'My\Bar\Class', ['instantiator' => new ReflectionInstantiator()]);
    }
}



class OrderHandler extends InputHandler
{
    public function define()
    {
        $address = $this->add('shipping_address', Address::class);
        $address->add('street', 'string');
        $address->add('city', 'string');
        $address->add('state', 'string');
        $address->add('zip_code', 'integer');
    }
}



class AddressHandler extends InputHandler
{
    public function define()
    {
        $address->add('street', 'string');
        $address->add('city', 'string');
        $address->add('state', 'string');
        $address->add('zip_code', 'integer');
    }
}

class OrderHandler extends InputHander
{
    public function define()
    {
        $this->add('shipping_address', Address::Class, ['handler' => new AddressHandler()]);
    }
}

class RegistrationHandler extends InputHander
{
    public function define()
    {
        $this->add('home_address', Address::Class, ['handler' => new AddressHandler()]);
    }
}