Completed
Push — master ( 4d66a1...40cfc1 )
by Matthieu
01:32
created

ArrayDefinitionExtension::setExtendedDefinition()   A

Complexity

Conditions 2
Paths 2

Duplication

Lines 0
Ratio 0 %

Size

Total Lines 11
Code Lines 6

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 1
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace DI\Definition;
6
7
use DI\Definition\Exception\InvalidDefinition;
8
9
/**
10
 * Extends an array definition by adding new elements into it.
11
 *
12
 * @since 5.0
13
 * @author Matthieu Napoli <[email protected]>
14
 */
15
class ArrayDefinitionExtension extends ArrayDefinition implements ExtendsPreviousDefinition
16
{
17
    /**
18
     * @var ArrayDefinition
19
     */
20
    private $subDefinition;
21
22
    public function getValues() : array
23
    {
24
        if (! $this->subDefinition) {
25
            return parent::getValues();
26
        }
27
28
        return array_merge($this->subDefinition->getValues(), parent::getValues());
29
    }
30
31
    public function setExtendedDefinition(Definition $definition)
32
    {
33
        if (! $definition instanceof ArrayDefinition) {
34
            throw new InvalidDefinition(sprintf(
35
                'Definition %s tries to add array entries but the previous definition is not an array',
36
                $this->getName()
37
            ));
38
        }
39
40
        $this->subDefinition = $definition;
41
    }
42
}
43