Passed
Push — master ( cbe990...54413c )
by Evgeniy
53s queued 10s
created

KeyValue::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 2
Bugs 2 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 2
b 2
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cekta\DI\Provider;
6
7
use Cekta\DI\Loader\Alias;
8
use Cekta\DI\Provider\Exception\NotFound;
9
use Cekta\DI\ProviderInterface;
10
use InvalidArgumentException;
11
12
class KeyValue implements ProviderInterface
13
{
14
    /**
15
     * @var array
16
     */
17
    private $values;
18
19 24
    public function __construct(array $values)
20
    {
21 24
        $this->values = $values;
22 24
    }
23
24 3
    public static function stringToAlias(array $values): self
25
    {
26 3
        $result = [];
27 3
        foreach ($values as $key => $value) {
28 3
            if (is_string($value)) {
29 3
                $value = new Alias($value);
30
            }
31 3
            $result[$key] = $value;
32
        }
33 3
        return new static($result);
34
    }
35
36 3
    public static function stringToType(array $params): self
37
    {
38 3
        foreach ($params as $key => $value) {
39 3
            $params[$key] = self::transform($value);
40
        }
41 3
        return new static($params);
42
    }
43
44 9
    public static function compile(string $compiledFile, callable $callable): self
45
    {
46
        if (
47 9
            (!file_exists($compiledFile) && !is_writable(dirname($compiledFile)))
48 9
            || (file_exists($compiledFile) && !is_writable($compiledFile))
49
        ) {
50 3
            throw new InvalidArgumentException("`$compiledFile` must be writable");
51
        }
52 6
        if (!is_readable($compiledFile)) {
53 3
            file_put_contents($compiledFile, call_user_func($callable));
54
        }
55
        /** @noinspection PhpIncludeInspection */
56 6
        return new static(require $compiledFile);
57
    }
58
59 18
    public function provide(string $id)
60
    {
61 18
        if (!$this->canProvide($id)) {
62 3
            throw new NotFound($id);
63
        }
64 15
        return $this->values[$id];
65
    }
66
67 21
    public function canProvide(string $id): bool
68
    {
69 21
        return array_key_exists($id, $this->values);
70
    }
71
72 3
    private static function transform($value)
73
    {
74 3
        if (is_string($value)) {
75 3
            if (preg_match('/\A([\'"])(.*)\1\z/', $value, $matches)) {
76 3
                $value = $matches[2];
77
            }
78 3
            $value = self::transformString($value);
79
        }
80 3
        return $value;
81
    }
82
83 3
    private static function transformString(string $value)
84
    {
85 3
        $result = $value;
86 3
        $valueLower = strtolower($value);
87 3
        if (in_array($valueLower, ['true', '(true)'])) {
88 3
            $result = true;
89
        }
90 3
        if (in_array($valueLower, ['false', '(false)'])) {
91 3
            $result = false;
92
        }
93 3
        if (in_array($valueLower, ['empty', '(empty)'])) {
94 3
            $result = '';
95
        }
96 3
        if (in_array($valueLower, ['null', '(null)'])) {
97 3
            $result = null;
98
        }
99 3
        return $result;
100
    }
101
}
102