PHP code example of koine / strong-parameters

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

    

koine / strong-parameters example snippets


use Koine\Parameters;

$params = new Parameters(array(
    'user' => array(
        'name'  => 'Foo',
        'email' => '[email protected]',
        'admin' => true
     )
));

// throws exception
$userParams = $params->ail' => '[email protected]')


// nested params

$params = new Params(array(
    'book' => array(
        'title'   => 'Some Title',
        'edition' => '3',
        'authors' => array(
            array(
                'name'     => 'Jon',
                'birthday' => '1960-01-02',
            ),
            array(
                'name'     => 'Daniel',
                'birthday' => '1960-01-02',
            ),
        )
    ),
    'foo' => 'bar',
    'bar' => 'foo'
));

$params->permit(array(
    'book' => array(
        'authors' => array('name'),
        'title'
    ),
    'foo'
))->toArray();

/**
    array(
        'book' => array(
            'title'   => 'Some Title',
            'authors' => array(
                array('name' => 'Jon'),
                array('name' => 'Daniel'),
            )
        ),
        'foo' => 'bar'
  )
*/


// array params

$params = new Params(array(
    'tags' => array('php', 'ruby')
));

$params->permit(array('tags' => array()))->toArray();
// array( 'tags' => array('php', 'ruby'))

// array params with invalid data

$params = new Params(array(
    'tags' => 'invalid'
));

$params->permit(array('tags' => array()))->toArray(); // array()


// do something with the values