8000 Alternative signature · symfony/symfony@6eacb76 · GitHub
[go: up one dir, main page]

Skip to content

Commit 6eacb76

Browse files
Alternative signature
1 parent e5d0442 commit 6eacb76

File tree

1 file changed

+14
-20
lines changed

1 file changed

+14
-20
lines changed

src/Symfony/Component/VarExporter/Instantiator.php

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,14 @@ final class Instantiator
2929
* For example:
3030
*
3131
* // creates an empty instance of Foo
32-
* Instantiator::instantiate([Foo::class => []]);
32+
* Instantiator::instantiate(Foo::class);
3333
*
34-
* // creates a Foo instance and sets one of its public, protected or private properties
35-
* Instantiator::instantiate([Foo::class => ['propertyName' => $propertyValue]]);
34+
* // creates a Foo instance and sets one of its properties
35+
* Instantiator::instantiate(Foo::class, ['propertyName' => $propertyValue]);
3636
*
3737
* // creates a Foo instance and sets a private property defined on its parent Bar class
38-
* Instantiator::instantiate([
39-
* Foo::class => [],
40-
* Bar::class => ['privatePropertyName' => $propertyValue],
38+
* Instantiator::instantiate(Foo::class, [], [
39+
* Bar::class => ['privateBarProperty' => $propertyValue],
4140
* ]);
4241
*
4342
* Instances of ArrayObject, ArrayIterator and SplObjectHash can be created
@@ -49,24 +48,17 @@ final class Instantiator
4948
* // creates an ArrayObject populated with $inputArray
5049
* Instantiator::instantiate([ArrayObject::class => ["\0" => $inputArray]]);
5150
*
52-
* @param array $propertiesByClass The properties to set on the object, keyed by the class to bind to
53-
* when setting them. The first key of the array defines the class
54-
* of the returned instance.
51+
* @param string $class The class of the instance to create
52+
* @param array $properties The properties to set on the instance
53+
* @param array $privateProperties The private properties to set on the instance,
54+
* keyed by their declaring class
5555
*
5656
* @return object The created instance
5757
*
5858
* @throws ExceptionInterface When the instance cannot be created
5959
*/
60-
public static function instantiate(array $propertiesByClass)
60+
public static function instantiate(string $class, array $properties = array(), array $privateProperties = array())
6161
{
62-
if (!$propertiesByClass) {
63-
throw new \ReflectionException('No class provided.');
64-
}
65-
66-
foreach ($propertiesByClass as $class => $properties) {
67-
break;
68-
}
69-
7062
$reflector = Registry::$reflectors[$class] ?? Registry::getClassReflector($class);
7163

7264
if (Registry::$cloneable[$class]) {
@@ -81,9 +73,11 @@ public static function instantiate(array $propertiesByClass)
8173
$wrappedInstance = array(unserialize('O:'.\strlen($class).':"'.$class.'":0:{}'));
8274
}
8375

84-
$instance = array($instance);
76+
if ($properties) {
77+
$privateProperties[$class] = isset($privateProperties[$class]) ? $properties + $privateProperties[$class] : $properties;
78+
}
8579

86-
foreach ($propertiesByClass as $class => $properties) {
80+
foreach ($privateProperties as $class => $properties) {
8781
if (!$properties) {
8882
continue;
8983
}

0 commit comments

Comments
 (0)
0