PHP code example of gocanto / relay

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

    

gocanto / relay example snippets


$payload = [
    'name' => 'Gustavo',
    'email' => '[email protected]',
    'profile_url' => 'http://foo.com/gocanto',
];

if (isset($payload['name']) && is_string($payload['name'])) {
    //do something amazing!
}

//is_valid_email is a imaginary function that should check whether a given email is valid or not.
if (isset($payload['email']) && is_valid_email($payload['email'])) { 
    //do something amazing!
}

//is_valid_url is a imaginary function that should check whether a given URL is valid or not.
if (isset($payload['profile_url']) && is_valid_email($payload['profile_url'])) { 
    //do something amazing!
}

declare(strict_types=1);

use Gocanto\Relay\Attributes;
use Gocanto\Relay\Types\Url;
use Gocanto\Relay\Types\Email;
use Gocanto\Relay\Types\Text;
use Gocanto\Relay\Promoter;

$data = [
    'name' => 'Gustavo',
    'email' => '[email protected]',
    'profile_url' => 'http://foo.com/gocanto',
];

class Payload extends Attributes
{
}

$payload = new Payload($data, [
    'name' => Promoter::make(Text::class),
    'email' => Promoter::make(Email::class),
    'profile_url' => Promoter::make(Url::class),
]);

/** @var Text $name */
$name = $payload->get('name');

/** @var Email $email */
$email = $payload->get('email');

/** @var Url $profileUrl */
$profileUrl = $payload->get('profile_url');