Completed
Push — master ( 8ab030...ee21ce )
by Andrii
14:10
created

ArrayDefinition::getParams()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
namespace yii\di\definitions;
3
4
use Psr\Container\ContainerInterface;
5
use yii\di\contracts\Definition;
6
use yii\di\exceptions\NotInstantiableException;
7
8
/**
9
 * Builds object by array config.
10
 * @package yii\di
11
 */
12
class ArrayDefinition implements Definition
13
{
14
    private $class;
15
    private $params;
16
    private $config;
17
18
    /**
19
     * @param string $class class name, must not be empty
20
     * @param array $params
21
     * @param array $config
22
     */
23
    public function __construct(string $class, array $params = [], array $config = [])
24
    {
25
        if (empty($class)) {
26
            throw Exception('class name not given');
0 ignored issues
show
Bug introduced by
The function Exception was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

26
            throw /** @scrutinizer ignore-call */ Exception('class name not given');
Loading history...
27
        }
28
        $this->class  = $class;
29
        $this->params = $params;
30
        $this->config = $config;
31
    }
32
33
    /**
34
     * @return string
35
     */
36
    public function getClass(): string
37
    {
38
        return $this->class;
39
    }
40
41
    /**
42
     * @return array
43
     */
44
    public function getParams(): array
45
    {
46
        return $this->params;
47
    }
48
49
    /**
50
     * @return array
51
     */
52
    public function getConfig(): array
53
    {
54
        return $this->config;
55
    }
56
57
    private const CLASS_KEY = '__class';
58
    private const PARAMS_KEY = '__construct()';
59
60
    /**
61
     * @param string $class class name
62
     * @param array $params
63
     * @param array $config
64
     * @return self
65
     */
66
    public static function fromArray(string $class = null, array $params = [], array $config = []): self
67
    {
68
        $class  = $config[self::CLASS_KEY] ?? $class;
69
        $params = $config[self::PARAMS_KEY] ?? $params;
70
71
        unset($config[self::CLASS_KEY]);
72
        unset($config[self::PARAMS_KEY]);
73
74
        if (empty($class)) {
75
            throw new NotInstantiableException(var_export($config, true));
76
        }
77
78
        return new static($class, $params, $config);
79
    }
80
81
    public function resolve(ContainerInterface $container, array $params = [])
82
    {
83
        if (!empty($params)) {
84
            $this->params = array_merge($this->params, $params);
85
        }
86
87
        return $this->getBuilder()->build($container, $this);
88
    }
89
90
    private static $builder;
91
92
    private function getBuilder()
93
    {
94
        if (static::$builder === null) {
0 ignored issues
show
Bug introduced by
Since $builder is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $builder to at least protected.
Loading history...
95
            static::$builder = new ArrayBuilder();
96
        }
97
98
        return static::$builder;
99
    }
100
}
101