Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
 
3
declare(strict_types=1);
4
 
5
namespace DI\Definition;
6
 
7
/**
8
 * Definition of an array containing values or references.
9
 *
10
 * @since 5.0
11
 * @author Matthieu Napoli <matthieu@mnapoli.fr>
12
 */
13
class ArrayDefinition implements Definition
14
{
15
    /** Entry name. */
16
    private string $name = '';
17
 
18
    public function __construct(
19
        private array $values,
20
    ) {
21
    }
22
 
23
    public function getName() : string
24
    {
25
        return $this->name;
26
    }
27
 
28
    public function setName(string $name) : void
29
    {
30
        $this->name = $name;
31
    }
32
 
33
    public function getValues() : array
34
    {
35
        return $this->values;
36
    }
37
 
38
    public function replaceNestedDefinitions(callable $replacer) : void
39
    {
40
        $this->values = array_map($replacer, $this->values);
41
    }
42
 
43
    public function __toString() : string
44
    {
45
        $str = '[' . \PHP_EOL;
46
 
47
        foreach ($this->values as $key => $value) {
48
            if (is_string($key)) {
49
                $key = "'" . $key . "'";
50
            }
51
 
52
            $str .= '    ' . $key . ' => ';
53
 
54
            if ($value instanceof Definition) {
55
                $str .= str_replace(\PHP_EOL, \PHP_EOL . '    ', (string) $value);
56
            } else {
57
                $str .= var_export($value, true);
58
            }
59
 
60
            $str .= ',' . \PHP_EOL;
61
        }
62
 
63
        return $str . ']';
64
    }
65
}