-
-
Notifications
You must be signed in to change notification settings - Fork 329
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fd8f9a4
commit 1cc70b4
Showing
18 changed files
with
763 additions
and
138 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Alice package. | ||
* | ||
* (c) Nelmio <hello@nelm.io> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Nelmio\Alice\Entity; | ||
|
||
class DummyWithMethods | ||
{ | ||
private $foo1; | ||
private $foo2; | ||
private $bar1; | ||
private $bar2; | ||
private $baz1; | ||
private $baz2; | ||
private $baz3; | ||
|
||
public function __construct(string $foo1, string $foo2) | ||
{ | ||
$this->foo1 = $foo1; | ||
$this->foo2 = $foo2; | ||
} | ||
|
||
public static function create(string $foo1, string $foo2) | ||
{ | ||
return new self($foo1, $foo2); | ||
} | ||
|
||
public function bar(string $bar1, string $bar2) | ||
{ | ||
$this->bar1 = $bar1; | ||
$this->bar2 = $bar2; | ||
} | ||
|
||
public function methodWithVariadic(string $baz1, string $baz2, array ...$baz3) | ||
{ | ||
$this->baz1 = $baz1; | ||
$this->baz2 = $baz2; | ||
$this->baz3 = $baz3; | ||
} | ||
|
||
public function methodWithDefaultValues(string $baz1 = 'value 1', string $baz2 = 'value 2', string $baz3 = 'value 3') | ||
{ | ||
$this->baz1 = $baz1; | ||
$this->baz2 = $baz2; | ||
$this->baz3 = $baz3; | ||
} | ||
|
||
public function methodWithNullables(?string $bar1, ?string $bar2) | ||
{ | ||
$this->bar1 = $bar1; | ||
$this->bar2 = $bar2; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{ | ||
"Nelmio\\Alice\\DummyWithMethods": { | ||
"dummy_with_methods": { | ||
"__construct": { | ||
"$foo1": "foo1", | ||
"$foo2": "foo2" | ||
}, | ||
"__calls": [ | ||
{ | ||
"bar": { | ||
"$bar1": "bar1", | ||
"$bar2": "bar2" | ||
} | ||
} | ||
] | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Alice package. | ||
* | ||
* (c) Nelmio <hello@nelm.io> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
return [ | ||
'Nelmio\Alice\DummyWithMethods' =>[ | ||
'dummy_with_methods' =>[ | ||
'__construct' =>[ | ||
'$foo1' =>'foo1', | ||
'$foo2' =>'foo2' | ||
], | ||
'__calls' =>[ | ||
[ | ||
'bar' =>[ | ||
'$bar1' =>'bar1', | ||
'$bar2' =>'bar2' | ||
] | ||
] | ||
] | ||
] | ||
] | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
Nelmio\Alice\DummyWithMethods: | ||
dummy_with_methods: | ||
__construct: | ||
$foo1: foo1 | ||
$foo2: foo2 | ||
__calls: | ||
- bar: | ||
$bar1: bar1 | ||
$bar2: bar2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
src/Bridge/Symfony/Resources/config/generator/resolver/named_arguments.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<!-- | ||
~ This file is part of the Alice package. | ||
~ | ||
~ (c) Nelmio <hello@nelm.io> | ||
~ | ||
~ For the full copyright and license information, please view the LICENSE | ||
~ file that was distributed with this source code. | ||
--> | ||
|
||
<container xmlns="http://symfony.com/schema/dic/services" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"> | ||
|
||
<services> | ||
|
||
<service id="nelmio_alice.generator.named_arguments_resolver" class="Nelmio\Alice\Generator\NamedArgumentsResolver" /> | ||
|
||
</services> | ||
|
||
</container> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Alice package. | ||
* | ||
* (c) Nelmio <hello@nelm.io> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Nelmio\Alice\Generator; | ||
|
||
class NamedArgumentsResolver | ||
{ | ||
public function resolveArguments(array $arguments, string $className, string $methodName): array | ||
{ | ||
try { | ||
$method = new \ReflectionMethod($className, $methodName); | ||
} catch (\ReflectionException $exception) { | ||
return $arguments; | ||
} | ||
|
||
$sortedArguments = []; | ||
$buffer = []; | ||
|
||
foreach ($method->getParameters() as $parameter) { | ||
$name = $parameter->getName(); | ||
|
||
if ($parameter->isVariadic() && [] !== $arguments) { | ||
$sortedArguments = array_merge($sortedArguments, $buffer, array_values($arguments)); | ||
$arguments = []; | ||
$buffer = []; | ||
|
||
break; | ||
} | ||
|
||
if (array_key_exists($name, $arguments)) { | ||
$sortedArguments = array_merge($sortedArguments, $buffer, [$name => $arguments[$name]]); | ||
unset($arguments[$name]); | ||
$buffer = []; | ||
|
||
continue; | ||
} | ||
|
||
foreach ($arguments as $key => $value) { | ||
if (is_int($key)) { | ||
$sortedArguments = array_merge($sortedArguments, $buffer, [$arguments[$key]]); | ||
unset($arguments[$key]); | ||
$buffer = []; | ||
|
||
continue 2; | ||
} | ||
} | ||
|
||
if (!$parameter->isDefaultValueAvailable()) { | ||
throw new \RuntimeException(sprintf( | ||
'Argument $%s of %s::%s() is not passed a value and does not define a default one.', | ||
$name, | ||
$className, | ||
$methodName | ||
)); | ||
} | ||
|
||
$buffer[] = $parameter->getDefaultValue(); | ||
} | ||
|
||
$unknownNamedParameters = array_filter(array_keys($arguments), static function ($key) { | ||
return is_string($key); | ||
}); | ||
|
||
if ([] !== $unknownNamedParameters) { | ||
throw new \RuntimeException(sprintf( | ||
'Unknown arguments for %s::%s(): $%s.', | ||
$className, | ||
$methodName, | ||
implode(', $', $unknownNamedParameters) | ||
)); | ||
} | ||
|
||
return $sortedArguments; | ||
} | ||
} |
Oops, something went wrong.