SettingsMap::get()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 1
nc 2
nop 2
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace EngineWorks\DBAL\Abstracts;
6
7
use EngineWorks\DBAL\Settings as SettingsInterface;
8
use InvalidArgumentException;
9
10
/**
11
 * This is a utility class to implement the Settings interface, it is used
12
 * on Mysqli and Sqlite implementations.
13
 * On the extended class define the map of properties
14
 *
15
 * @package EngineWorks\DBAL\Abstracts
16
 */
17
class SettingsMap implements SettingsInterface
18
{
19
    /**
20
     * map of settings with default values
21
     * @var array<string, scalar|null>
22
     */
23
    protected $map = [];
24
25 397
    public function __construct(array $settings = [])
26
    {
27 397
        $this->setAll($settings);
28
    }
29
30
    /**
31
     * Get all the settings
32
     *
33
     * @return array<string, mixed>
34
     */
35 4
    public function all(): array
36
    {
37 4
        return $this->map;
38
    }
39
40
    /**
41
     * Set a setting, if it does dot exists then throws an exception
42
     *
43
     * @param string $name
44
     * @param scalar|null $value
45
     * @throws InvalidArgumentException if setting does not exist
46
     */
47 3
    public function set(string $name, $value): void
48
    {
49 3
        if (! array_key_exists($name, $this->map)) {
50 1
            throw new InvalidArgumentException("Setting $name does not exists");
51
        }
52 2
        $this->map[$name] = $value;
53
    }
54
55
    /**
56
     * Set an array of settings, ignores non-existent or non-string-key elements
57
     *
58
     * @param array<string, scalar|null> $settings
59
     */
60 397
    public function setAll(array $settings): void
61
    {
62 397
        foreach ($settings as $name => $value) {
63 348
            if (is_string($name) && array_key_exists($name, $this->map)) {
64 346
                $this->map[$name] = $value;
65
            }
66
        }
67
    }
68
69
    /**
70
     * Obtain a setting
71
     *
72
     * @param string $name
73
     * @param mixed|null $default
74
     * @return mixed|null
75
     */
76 305
    public function get(string $name, $default = null)
77
    {
78 305
        return ($this->exists($name)) ? $this->map[$name] : $default;
79
    }
80
81 306
    public function exists(string $name): bool
82
    {
83 306
        return array_key_exists($name, $this->map);
84
    }
85
}
86