Skip to content

Commit ae37db4

Browse files
New InstantiateTransformer that can be used to create a new object with arguments from input.
1 parent d9fffdf commit ae37db4

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed
+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php declare(strict_types=1);
2+
/*
3+
* This file is part of the CleverAge/ProcessBundle package.
4+
*
5+
* Copyright (c) 2017-2023 Clever-Age
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
11+
namespace CleverAge\ProcessBundle\Transformer;
12+
13+
use Symfony\Component\OptionsResolver\OptionsResolver;
14+
15+
/**
16+
* Instantiate a new object with parameters from the input array
17+
*
18+
* @author Vincent Chalnot <[email protected]>
19+
*/
20+
class InstantiateTransformer implements ConfigurableTransformerInterface
21+
{
22+
public function transform(mixed $value, array $options = [])
23+
{
24+
if (!is_array($value)) {
25+
throw new \UnexpectedValueException('Input value must be an array for transformer instantiate');
26+
}
27+
28+
return (new \ReflectionClass($options['class']))->newInstanceArgs($value);
29+
}
30+
31+
public function configureOptions(OptionsResolver $resolver)
32+
{
33+
$resolver->setRequired(
34+
[
35+
'class',
36+
]
37+
);
38+
$resolver->setAllowedTypes('class', ['string']);
39+
}
40+
41+
public function getCode()
42+
{
43+
return 'instantiate';
44+
}
45+
}

0 commit comments

Comments
 (0)