Debian Codegraph @

vendor/ipl/stdlib/src/Data.php

raw
1 <?php
2
3 namespace ipl\Stdlib;
4
5 /**
6 * Store and retrieve arbitrary key-value data
7 */
8 class Data
9 {
10 /** @var array<string, mixed> */
11 protected array $data = [];
12
13 /**
14 * Check whether there's any data
15 *
16 * @return bool
17 */
18 public function isEmpty(): bool
19 {
20 return empty($this->data);
21 }
22
23 /**
24 * Check whether the given data exists
25 *
26 * @param string $name The name of the data
27 *
28 * @return bool
29 */
30 public function has(string $name): bool
31 {
32 return array_key_exists($name, $this->data);
33 }
34
35 /**
36 * Get the value of the given data
37 *
38 * @param string $name The name of the data
39 * @param mixed $default The value to return if there's no such data
40 *
41 * @return mixed
42 */
43 public function get(string $name, mixed $default = null): mixed
44 {
45 if ($this->has($name)) {
46 return $this->data[$name];
47 }
48
49 return $default;
50 }
51
52 /**
53 * Set the value of the given data
54 *
55 * @param string $name The name of the data
56 * @param mixed $value
57 *
58 * @return $this
59 */
60 public function set(string $name, mixed $value): static
61 {
62 $this->data[$name] = $value;
63
64 return $this;
65 }
66
67 /**
68 * Merge the given data
69 *
70 * @param Data $with
71 *
72 * @return $this
73 */
74 public function merge(self $with): static
75 {
76 $this->data = array_merge($this->data, $with->data);
77
78 return $this;
79 }
80
81 /**
82 * Clear all data
83 *
84 * @return $this
85 */
86 public function clear(): static
87 {
88 $this->data = [];
89
90 return $this;
91 }
92 }