Passed
Push — master ( 1478fc...b77d73 )
by Evgeniy
01:04 queued 12s
created

KeyValue::transformString()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

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