-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathClassServiceCaseConverter.php
More file actions
53 lines (42 loc) · 1.69 KB
/
ClassServiceCaseConverter.php
File metadata and controls
53 lines (42 loc) · 1.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<?php
declare(strict_types=1);
namespace Symplify\PhpConfigPrinter\CaseConverter;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\Expression;
use Symplify\PhpConfigPrinter\Contract\CaseConverterInterface;
use Symplify\PhpConfigPrinter\NodeFactory\ArgsNodeFactory;
use Symplify\PhpConfigPrinter\NodeFactory\Service\ServiceOptionNodeFactory;
use Symplify\PhpConfigPrinter\ValueObject\MethodName;
use Symplify\PhpConfigPrinter\ValueObject\VariableName;
use Symplify\PhpConfigPrinter\ValueObject\YamlKey;
final readonly class ClassServiceCaseConverter implements CaseConverterInterface
{
public function __construct(
private ArgsNodeFactory $argsNodeFactory,
private ServiceOptionNodeFactory $serviceOptionNodeFactory
) {
}
public function convertToMethodCallStmt(mixed $key, mixed $values): Stmt
{
$args = $this->argsNodeFactory->createFromValues([$key, $values[YamlKey::CLASS_KEY]]);
$methodCall = new MethodCall(new Variable(VariableName::SERVICES), MethodName::SET, $args);
unset($values[YamlKey::CLASS_KEY]);
$decoratedMethodCall = $this->serviceOptionNodeFactory->convertServiceOptionsToNodes($values, $methodCall);
return new Expression($decoratedMethodCall);
}
public function match(string $rootKey, mixed $key, mixed $values): bool
{
if ($rootKey !== YamlKey::SERVICES) {
return false;
}
if (is_array($values) && count($values) !== 1) {
return false;
}
if (! isset($values[YamlKey::CLASS_KEY])) {
return false;
}
return ! isset($values[YamlKey::ALIAS]);
}
}