summaryrefslogtreecommitdiffstats
path: root/vendor/ipl/stdlib/src/Data.php
blob: b12306c29c65f3abf15150342991ba94d4bab4d6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
<?php

namespace ipl\Stdlib;

class Data
{
    /** @var array<string, mixed> */
    protected $data = [];

    /**
     * Check whether there's any data
     *
     * @return bool
     */
    public function isEmpty()
    {
        return empty($this->data);
    }

    /**
     * Check whether the given data exists
     *
     * @param string $name The name of the data
     *
     * @return bool
     */
    public function has($name)
    {
        return array_key_exists($name, $this->data);
    }

    /**
     * Get the value of the given data
     *
     * @param string $name The name of the data
     * @param mixed $default The value to return if there's no such data
     *
     * @return mixed
     */
    public function get($name, $default = null)
    {
        if ($this->has($name)) {
            return $this->data[$name];
        }

        return $default;
    }

    /**
     * Set the value of the given data
     *
     * @param string $name The name of the data
     * @param mixed $value
     *
     * @return $this
     */
    public function set($name, $value)
    {
        $this->data[$name] = $value;

        return $this;
    }

    /**
     * Merge the given data
     *
     * @param Data $with
     *
     * @return $this
     */
    public function merge(self $with)
    {
        $this->data = array_merge($this->data, $with->data);

        return $this;
    }

    /**
     * Clear all data
     *
     * @return $this
     */
    public function clear()
    {
        $this->data = [];

        return $this;
    }
}