PHP code example of incoming / incoming

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

    

incoming / incoming example snippets


class UserHydrator implements Incoming\Hydrator\Hydrator
{
    public function hydrate($input, $model)
    {
        $model->setName($input['name']);
        $model->setGender($input['gender']);
        $model->setFavoriteColor($input['favorite_color']);

        return $model;
    }
}

// Create our incoming processor
$incoming = new Incoming\Processor();

// Process our raw form/request input into a User model
$user = $incoming->processForModel(
    $_POST,            // Our HTTP form-data array
    new User(),        // Our model to hydrate
    new UserHydrator() // The hydrator above
);

// Validate and save the user
// ...

class BlogPostHydrator implements Incoming\Hydrator\ContextualHydrator
{
    const USER_CONTEXT_KEY = 'user';

    public function hydrate($input, $model, Map $context = null)
    {
        $model->setBody($input['body']);
        $model->setCategories($input['categories']);
        $model->setTags($input['tags']);

        // Only allow admin users to publish posts
        if (null !== $context && $context->exists(self::USER_CONTEXT_KEY)) {
            $user = $context->get(self::USER_CONTEXT_KEY);

            $model->setAuthor($user->getName());

            if ($user->isAdmin()) {
                $model->setPublished($input['published']);
            }
        }

        return $model;
    }
}

// Create our incoming processor
$incoming = new Incoming\Processor();

// Create a context for the hydrator with active data
$context = Map::fromArray([
    BlogPostHydrator::USER_CONTEXT_KEY => $this->getCurrentUser() // A user context
]);

// Process our raw form/request input to update our BlogPost model
$post = $incoming->processForModel(
    $_POST,                      // Our HTTP form-data array
    BlogPost::find($_GET['id']), // Fetch our blog post to update and pass it in
    new BlogPostHydrator(),      // The hydrator above
    $context                     // Context data to enable more powerful conditional processing
);

// Validate and save the blog post
// ...

class SpecialCharacterFilterTransformer implements Incoming\Transformer\Transformer
{
    public function transform($input)
    {
        $transformed = [];

        foreach($input as $key => $string) {
            $transformed[$key] = filter_var($string, FILTER_SANITIZE_STRING);
        }

        return $transformed;
    }
}

class UserHydrator implements Incoming\Hydrator\Hydrator
{
    // Same as previous examples...
}

// Create our incoming processor
$incoming = new Incoming\Processor(
    new SpecialCharacterFilterTransformer()
);

// Process our raw form/request input into a User model
$user = $incoming->processForModel(
    $_POST,            // Our HTTP form-data array
    new User(),        // Our model to hydrate
    new UserHydrator() // The hydrator above
);

// Validate and save the user
// ...

class UserHydrator extends Incoming\Hydrator\AbstractDelegateHydrator
{
    // Boom! Type-hintable arguments!
    // (For more info, see the `AbstractDelegateHydrator` class doc-block)
    public function hydrateModel(Incoming\Structure\Map $input, User $model)
    {
        $model->setName($input['name']);
        // ...

        return $model;
    }
}

// Create our incoming processor
$incoming = new Incoming\Processor();

// Process our raw form/request input into a User model
$user = $incoming->processForModel(
    $_POST,            // Our HTTP form-data array
    new User(),        // Our model to hydrate
    new UserHydrator() // The hydrator above
);

// Validate and save the user
// ...

class User
{
    private $first_name;
    private $last_name;

    public function __construct(string $first_name, string $last_name)
    {
        $this->first_name = $first_name;
        $this->last_name = $last_name;
    }
}

class UserBuilder extends Incoming\Hydrator\AbstractDelegateBuilder
{
    public function buildModel(Incoming\Structure\Map $input): User
    {
        return new User($input['first_name'], $input['last_name']);
    }
}

// Create our incoming processor
$incoming = new Incoming\Processor();

// Process our raw form/request input into a new User model
$user = $incoming->processForType(
    $_POST,            // Our HTTP form-data array
    User::class,       // Our type to build
    new UserBuilder()  // The builder above
);

// Validate and save the user
// ...