koine/strong-parameters

Rails like Strong parameters for php

0.9.3 2014-09-26 14:29 UTC

This package is not auto-updated.

Last update: 2024-11-09 16:27:15 UTC


README

Rails-like strong parameters for php

Work in progress

Code information:

Build Status Coverage Status Code Climate Scrutinizer Code Quality

Package information:

Latest Stable Version Total Downloads Latest Unstable Version License Dependency Status

Usage

use Koine\Parameters;

$params = new Parameters(array(
    'user' => array(
        'name'  => 'Foo',
        'email' => 'Foo@bar.com',
        'admin' => true
     )
));

// throws exception
$userParams = $params->requireParam('user')->permit(array(
    'name',
    'email',
));

// filters value
Parameters::$throwsException = false;

$userParams = $params->requireParam('user')->permit(array(
    'name',
    'email',
))->toArray(); // array('name' => 'Foo', 'email' => 'Foo@bar.com')


// 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

Installing

Via Composer

Append the lib to your requirements key in your composer.json.

{
    // composer.json
    // [..]
    require: {
        // append this line to your requirements
        "koine/strong-parameters": "~0.9.3"
    }
}

Alternative install

Issues/Features proposals

Here is the issue tracker.

Contributing

Please refer to the contribuiting guide.

License

MIT

Authors