Skip to content

parfumix/php-function-arguments

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

23 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Argument helper

Argument helper is an class helper which can help wich help you to reduce the arguments of php function and easy convert it to class and can extracted and validated ..

    // From ..
    public function example($arg1, $arg2, $arg3, $arg4 = null, $arg5 = array());
    // To ..
    public function example(Argumentable $args);

Instalation

You can use the composer package manager to install. Either run:

$ php composer.phar require parfumix/php-function-arguments "dev-master"

or add:

"parfumix/php-function-arguments": "dev-master"

to your composer.json file

Usage

You can easy create an instance of arugments by using args helper functions

$args = args();

// or if you want to pass some values and rules
$args = args($values = array(), $rules = array());

// or you can use ::create static functions
$args = Argument::create($values = array(), $rules = array());

Argument implements php interfaces like "Argumentable, ArrayAccess, Iterator, IteratorAggregate, Countable" so you can easy iterate by arguments or access elements as array or count your arguments

$args = args($values = array(
    'key' => 'value'
));

// to access an arguments you can use
$args['key']
// or
$args->get('key')

// counting arguments
count($args)

Validation

You can easy validate arguments using validation function or call object as function (using __invoke)

$args = args();
$args->set('key', 'value');

// You can validate your args before
if( ! $args->validate(function($failedFields) {
    // you can do whetever with failed fields ..
}) )

class Example {
    public function needParams(Argumentable $args) {
        // Or you can validate here .
        if( ! $args->validate(function($failedFields) {
            // you can do whetever with failed fields ..
        }) )
    }
}

About

Php function argument helper

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages