PHP code example of rybakit / arguments-resolver

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

    

rybakit / arguments-resolver example snippets


use ArgumentsResolver\InDepthArgumentsResolver;

$greet = function ($username, DateTime $date, $greeting = 'Hello %s!') {
    // ...
};

$parameters = [
    'Welcome %s!',
    ['foo'],
    new DateTime(),
    'username' => 'Stranger',
    'bar',
];

$arguments = (new InDepthArgumentsResolver($greet))->resolve($parameters);
print_r($arguments);

Array
(
    [0] => Stranger
    [1] => DateTime Object (...)
    [2] => Welcome %s!
)

new InDepthArgumentsResolver(['MyClass', 'myMethod']);
new InDepthArgumentsResolver([new MyClass(), 'myMethod']);
new InDepthArgumentsResolver(['MyClass', 'myStaticMethod']);
new InDepthArgumentsResolver('MyClass::myStaticMethod');
new InDepthArgumentsResolver('MyClass::__construct');
new InDepthArgumentsResolver(['MyClass', '__construct']);
new InDepthArgumentsResolver(new MyInvokableClass());
new InDepthArgumentsResolver(function ($foo) {});
new InDepthArgumentsResolver('MyNamespace\my_function');
new InDepthArgumentsResolver(new ReflectionMethod('MyClass', 'myMethod'));
new InDepthArgumentsResolver(new ReflectionFunction('MyNamespace\my_function'));

use ArgumentsResolver\ReflectionFactory;

$reflection = ReflectionFactory::create('MyClass::__construct');
$resolver = new InDepthArgumentsResolver($reflection);

function foo(array $array, stdClass $object, callable $callable) {}

(new InDepthArgumentsResolver('foo'))->resolve([
    ...
    function () {},    // $callable
    ...
    new stdClass(),    // $object
    ...
    [42],              // $array
    ...
]);

function foo(Exception $e, RuntimeException $re) {}

(new InDepthArgumentsResolver('foo'))->resolve([
    ...
    new RuntimeException(),    // $re
    ...
    new Exception(),           // $e
    ...
]);

function foo($a, $b) {}

(new InDepthArgumentsResolver('foo'))->resolve([
    ...
    'c' => 3,
    'b' => 2,    // $b
    'a' => 1,    // $a
    ...
]);

function foo($a, $b) {}

(new InDepthArgumentsResolver('foo'))->resolve([
    1,   // $a
    2,   // $b
    ...
]);

function foo($a, array $b, $c = null) {}

(new NamedArgumentsResolver('foo'))->resolve([
    ...
    'b' => [],       // $b
    'a' => 1,        // $a
    'c' => 'bar',    // $c
    ...
]);