Registry::has()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 7
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Flextype\Component\Registry;
6
7
use Flextype\Component\Arrays\Arrays;
8
9
class Registry
10
{
11
    /**
12
     * Registry of variables
13
     *
14
     * @var array
15
     */
16
    private $registry = [];
17
18
    /**
19
     * Get all values in the register.
20
     *
21
     * @return array
22
     */
23
    public function all() : array
24
    {
25
        return $this->registry;
26
    }
27
28
    /**
29
     * Determine if the registry has a value for the given name.
30
     *
31
     * @param  string $key The key of the registry item to check for existence.
32
     */
33
    public function has(string $key) : bool
34
    {
35
        if (Arrays::has($this->registry, $key)) {
36
            return true;
37
        }
38
39
        return false;
40
    }
41
42
    /**
43
     * Set a value in the registry.
44
     *
45
     * @param  string          $key   The key of the value to store.
46
     * @param  string|int|null $value The value that needs to be stored.
47
     */
48
    public function set(string $key, $value = null) : void
49
    {
50
        Arrays::set($this->registry, $key, $value);
51
    }
52
53
    /**
54
     * Flush all values from the registry.
55
     */
56
    public function flush() : void
57
    {
58
        $this->registry = [];
59
    }
60
61
    /**
62
     * Delete a value from the registry.
63
     *
64
     * @param  string $key The key of the value to store.
65
     */
66
    public function delete(string $key) : void
67
    {
68
        Arrays::delete($this->registry, $key);
69
    }
70
71
    /**
72
     * Get item from the registry.
73
     *
74
     * @param  string $key     The name of the item to fetch.
75
     * @param  mixed  $default Default value
76
     *
77
     * @return string|array|null
78
     */
79
    public function get(string $key, $default = null)
80
    {
81
        return $this->has($key)
82
            ? Arrays::get($this->registry, $key, $default)
83
            : $default;
84
    }
85
}
86