Passed
Pull Request — master (#83)
by Evgeniy
05:15 queued 02:25
created

KeyValue   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 4
Bugs 2 Features 0
Metric Value
eloc 38
c 4
b 2
f 0
dl 0
loc 87
ccs 44
cts 44
cp 1
rs 10
wmc 23

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A compile() 0 13 6
A stringToType() 0 6 2
A canProvide() 0 3 1
A provide() 0 6 2
A stringToAlias() 0 10 3
A transform() 0 9 3
A transformString() 0 16 5
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
        $valueLower = strtolower($value);
86 3
        if (in_array($valueLower, ['true', '(true)'])) {
87 3
            return true;
88
        }
89 3
        if (in_array($valueLower, ['false', '(false)'])) {
90 3
            return false;
91
        }
92 3
        if (in_array($valueLower, ['empty', '(empty)'])) {
93 3
            return '';
94
        }
95 3
        if (in_array($valueLower, ['null', '(null)'])) {
96 3
            return null;
97
        }
98 3
        return $value;
99
    }
100
}
101