Passed
Push — scrutinizer-to-10 ( 64f0d0 )
by Evgeniy
06:00
created

KeyValue::canProvide()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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