Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0f5359e

Browse files
committed
Add FixedValueResolver
1 parent ff7b0b9 commit 0f5359e

2 files changed

Lines changed: 148 additions & 0 deletions

File tree

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Console\ArgumentResolver\ValueResolver;
13+
14+
use Symfony\Component\Console\Attribute\Reflection\ReflectionMember;
15+
use Symfony\Component\Console\Input\InputInterface;
16+
use function Symfony\Component\Translation\t;
17+
18+
/**
19+
* Yields a value that matches class or type, or argument name.
20+
* Value can be provided as a Closure, and if done so and the argument type is *not* a closure, then the closure will be executed at runtime to fetch the internal value.
21+
*
22+
* @author Alex "Pierstoval" Rock <[email protected]>
23+
*/
24+
final class FixedValueResolver implements ValueResolverInterface
25+
{
26+
public function __construct(
27+
private readonly mixed $value,
28+
private readonly ?string $type = null,
29+
private readonly ?string $argumentName = null,
30+
) {
31+
if (!$this->type && !$this->argumentName) {
32+
throw new \InvalidArgumentException('Please specify at least a type or an argument name to resolve.');
33+
}
34+
}
35+
36+
public function resolve(string $argumentName, InputInterface $input, ReflectionMember $member): iterable
37+
{
38+
if (
39+
(!$this->argumentName || ($this->argumentName === $argumentName))
40+
&& (!$this->type || ($this->type === $member->getType()?->getName()))
41+
) {
42+
return [$this->getValue()];
43+
}
44+
45+
return [];
46+
}
47+
48+
private function getValue(): mixed
49+
{
50+
if ($this->value instanceof \Closure && $this->type !== \Closure::class) {
51+
return ($this->value)();
52+
}
53+
54+
return $this->value;
55+
}
56+
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Console\Tests\ArgumentResolver\ValueResolver;
13+
14+
use PHPUnit\Framework\Attributes\DataProvider;
15+
use PHPUnit\Framework\TestCase;
16+
use Symfony\Component\Console\ArgumentResolver\ValueResolver\FixedValueResolver;
17+
use Symfony\Component\Console\Attribute\Reflection\ReflectionMember;
18+
use Symfony\Component\Console\Input\ArrayInput;
19+
20+
class FixedValueResolverTest extends TestCase
21+
{
22+
public function testMissingTypeAndArgumentName(): void
23+
{
24+
$this->expectExceptionMessage('Please specify at least a type or an argument name to resolve.');
25+
26+
new FixedValueResolver('Some value');
27+
}
28+
29+
public function testResolverWithClosureValue(): void
30+
{
31+
$resolverFunction = static fn () => 'Value A';
32+
$commandFunction = static fn (string $a) => null;
33+
34+
$resolver = new FixedValueResolver($resolverFunction, 'string', 'a');
35+
$parameters = new \ReflectionFunction($commandFunction)->getParameters();
36+
$member = new ReflectionMember($parameters[0]);
37+
38+
$result = $resolver->resolve($parameters[0]->getName(), new ArrayInput([]), $member);
39+
40+
$this->assertSame([$resolverFunction()], $result);
41+
}
42+
43+
public function testResolverWithClosureValueAndType(): void
44+
{
45+
$resolverFunction = static fn () => 'Value A';
46+
$commandFunction = static fn (\Closure $a) => null;
47+
48+
$resolver = new FixedValueResolver($resolverFunction, \Closure::class, 'a');
49+
$parameters = new \ReflectionFunction($commandFunction)->getParameters();
50+
$member = new ReflectionMember($parameters[0]);
51+
52+
$result = $resolver->resolve($parameters[0]->getName(), new ArrayInput([]), $member);
53+
54+
$this->assertSame([$resolverFunction], $result);
55+
}
56+
57+
#[DataProvider('provideTypeNames')]
58+
public function testResolverWithTypeName(callable $function, ?string $type, ?string $argumentName, mixed $expectedValue): void
59+
{
60+
$resolver = new FixedValueResolver($expectedValue, $type, $argumentName);
61+
$parameters = new \ReflectionFunction($function)->getParameters();
62+
$member = new ReflectionMember($parameters[0]);
63+
64+
$result = $resolver->resolve($parameters[0]->getName(), new ArrayInput([]), $member);
65+
66+
$this->assertSame([$expectedValue], $result);
67+
}
68+
69+
public static function provideTypeNames(): iterable
70+
{
71+
yield 'only type "string"' => [static fn (string $a) => null, 'string', null, 'Some string'];
72+
yield 'only type "bool"' => [static fn (bool $a) => null, 'bool', null, true];
73+
yield 'only type "int"' => [static fn (int $a) => null, 'int', null, 1];
74+
yield 'only type "float"' => [static fn (float $a) => null, 'float', null, 1.0];
75+
yield 'only type stdClass' => [static fn (\stdClass $a) => null, \stdClass::class, null, (object) ['a' => true]];
76+
77+
yield 'only argument name $a' => [static fn (string $a) => null, null, 'a', 'Value A'];
78+
yield 'only argument name $πŸ’ͺ' => [static fn (string $πŸ’ͺ) => null, null, 'πŸ’ͺ', 'Value πŸ’ͺ'];
79+
80+
yield 'both type "string" and argument name $a' => [static fn (string $a) => null, null, 'a', 'Value A'];
81+
yield 'both type "bool" and argument name $a' => [static fn (string $a) => null, null, 'a', 'Value A'];
82+
yield 'both type "int" and argument name $a' => [static fn (string $a) => null, null, 'a', 'Value A'];
83+
yield 'both type "float" and argument name $a' => [static fn (string $a) => null, null, 'a', 'Value A'];
84+
yield 'both type stdClass and argument name $a' => [static fn (string $a) => null, null, 'a', 'Value A'];
85+
86+
yield 'both type "string" and argument name $πŸ’ͺ' => [static fn (string $πŸ’ͺ) => null, null, 'πŸ’ͺ', 'Value πŸ’ͺ'];
87+
yield 'both type "bool" and argument name $πŸ’ͺ' => [static fn (string $πŸ’ͺ) => null, null, 'πŸ’ͺ', 'Value πŸ’ͺ'];
88+
yield 'both type "int" and argument name $πŸ’ͺ' => [static fn (string $πŸ’ͺ) => null, null, 'πŸ’ͺ', 'Value πŸ’ͺ'];
89+
yield 'both type "float" and argument name $πŸ’ͺ' => [static fn (string $πŸ’ͺ) => null, null, 'πŸ’ͺ', 'Value πŸ’ͺ'];
90+
yield 'both type stdClass and argument name $πŸ’ͺ' => [static fn (string $πŸ’ͺ) => null, null, 'πŸ’ͺ', 'Value πŸ’ͺ'];
91+
}
92+
}

0 commit comments

Comments
Β (0)