PHP code example of pollen-solutions / argument-resolver
1. Go to this page and download the library: Download pollen-solutions/argument-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/ */
pollen-solutions / argument-resolver example snippets
use Pollen\ArgumentResolver\ArgumentResolver;
use Pollen\ArgumentResolver\Resolvers\ParameterResolver;
class AcmeClass
{
public function __invoke(int $product_id, string $name, bool $in_stock, float $price, array $product_attrs)
{
var_dump($product_id, $in_stock, $price, $product_attrs);
exit;
}
}
$acmeCallable = new AcmeClass();
$parameters = [
'product_id' => 1,
'name' => 'My beautiful sneaker',
'in_stock' => true,
'price' => 156.30,
'product_attrs' => [
'size' => 12,
'color' => 'pink'
]
];
$arguments = (new ArgumentResolver([new ParameterResolver($parameters)]))->resolve($acmeCallable);
$acmeCallable(...$arguments);
use Pollen\Container\Container;
use Pollen\ArgumentResolver\ArgumentResolver;
use Pollen\ArgumentResolver\Resolvers\ContainerResolver;
class Foo {}
class Bar {}
$container = new Container();
$container->add(Foo::class, new Foo);
$container->add(Bar::class, new Bar);
$acmeCallable = static function (Foo $foo, Bar $bar) {
var_dump($foo, $bar);
exit;
};
$arguments = (new ArgumentResolver([new ContainerResolver($container)]))->resolve($acmeCallable);
$acmeCallable(...$arguments);
use Laminas\Diactoros\ServerRequestFactory;
use Pollen\ArgumentResolver\ArgumentResolver;
use Pollen\ArgumentResolver\Resolvers\RequestResolver;
// This code must be served by your app and visits :
// http://127.0.0.1/?product_id=1&name=My beautiful sneaker&in_stock=true&price=156.30&product_attrs[size]=12&product_attrs[color]=pink
$request = ServerRequestFactory::fromGlobals();
// also you can force $_GET attributes :
//$request = ServerRequestFactory::fromGlobals(null, [
// 'product_id' => 1,
// 'name' => 'My beautiful sneaker',
// 'in_stock' => true,
// 'price' => 156.30,
// 'product_attrs' => [
// 'size' => 12,
// 'color' => 'pink'
// ]
//]);
$acmeCallable = static function (int $product_id, string $name, bool $in_stock, float $price, array $product_attrs) {
var_dump($product_id, $in_stock, $price, $product_attrs);
exit;
};
$arguments = (new ArgumentResolver([new RequestResolver($request)]))->resolve($acmeCallable);
$acmeCallable(...$arguments);