PHP code example of codexsoft / transmission-schema

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

    

codexsoft / transmission-schema example snippets


$input = [
    'name' => 'John Doe',
    'email' => '[email protected]',
    'pets' => [
        [
            'kind' => 42,
            'name' => 'Rex',
        ],
        [
            'kind' => 34,
            'name' => 'Sunny',
        ]
    ],
];

use CodexSoft\Transmission\Schema\Accept;

$schema = Accept::json([
    'name' => Accept::string()->minLength(3),
    'email' => Accept::email(),
    'pets' => Accept::collection(
        Accept::json([
            'kind' => Accept::integer()->choices([15, 34, 42]),
            'name' => Accept::string(),
        ])
    ),
]);

$result = $schema->validateNormalizedData($userInput);
if ($result->getViolations()->count()) {
    return new JsonResponse($result->getViolations());
}

$data = $result->getData();

use CodexSoft\Transmission\Schema\Elements\BoolElement;

$element = new BoolElement();

use CodexSoft\Transmission\Schema\Accept;

$boolean = Accept::bool();

use CodexSoft\Transmission\Schema\Accept;

$userInput = 'test';

$boolean = Accept::bool();
$result = $boolean->validateNormalizedData($userInput);
$result->getViolations()->count(); // 0
$result->getData(); // true

$strictBoolean = Accept::bool()->strict();
$result = $boolean->validateNormalizedData($userInput);
$result->getViolations()->count(); // 1
$result->getData(); // 'test'
shell script
php ./vendor/bin/phpunit