|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace CodeIgniter\Context; |
| 4 | + |
| 5 | +class Context |
| 6 | +{ |
| 7 | + /** |
| 8 | + * The data stored in the context. |
| 9 | + * |
| 10 | + * @var array<string, mixed> |
| 11 | + */ |
| 12 | + protected array $data; |
| 13 | + |
| 14 | + /** |
| 15 | + * The data that is stored, but not included in logs. |
| 16 | + * |
| 17 | + * @var array<string, mixed> |
| 18 | + */ |
| 19 | + private array $hiddenData; |
| 20 | + |
| 21 | + /** |
| 22 | + * Constructor |
| 23 | + */ |
| 24 | + public function __construct() |
| 25 | + { |
| 26 | + $this->data = []; |
| 27 | + $this->hiddenData = []; |
| 28 | + } |
| 29 | + |
| 30 | + /** |
| 31 | + * Set a key-value pair to the context. |
| 32 | + * |
| 33 | + * @param string|array<string, mixed> $key The key to identify the data. Can be a string or an array of key-value pairs. |
| 34 | + * @param mixed $value The value to be stored in the context. |
| 35 | + * @return $this |
| 36 | + */ |
| 37 | + public function set(string|array $key, mixed $value): self |
| 38 | + { |
| 39 | + if (is_array($key)) { |
| 40 | + $this->data = array_merge($this->data, $key); |
| 41 | + return $this; |
| 42 | + } |
| 43 | + |
| 44 | + $this->data[$key] = $value; |
| 45 | + return $this; |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * Set a hidden key-value pair to the context. This data will not be included in logs. |
| 50 | + * |
| 51 | + * @param string|array<string, mixed> $key The key to identify the data. Can be a string or an array of key-value pairs. |
| 52 | + * @param mixed $value The value to be stored in the context. |
| 53 | + * @return $this |
| 54 | + */ |
| 55 | + public function setHidden(string|array $key, mixed $value): self |
| 56 | + { |
| 57 | + if (is_array($key)) { |
| 58 | + $this->hiddenData = array_merge($this->hiddenData, $key); |
| 59 | + return $this; |
| 60 | + } |
| 61 | + |
| 62 | + $this->hiddenData[$key] = $value; |
| 63 | + return $this; |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Get a value from the context by its key, or return a default value if the key does not exist. |
| 68 | + * |
| 69 | + * @param string $key The key to identify the data. |
| 70 | + * @param mixed|null $default The default value to return if the key does not exist in the context. |
| 71 | + * @return mixed The value associated with the key, or the default value if the key does not exist. |
| 72 | + */ |
| 73 | + public function get(string $key, mixed $default = null): mixed |
| 74 | + { |
| 75 | + return $this->data[$key] ?? $default; |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * Get all data from the context |
| 80 | + * |
| 81 | + * @return array<string, mixed> An array of all key-value pairs in the context. |
| 82 | + */ |
| 83 | + public function getAll(): array |
| 84 | + { |
| 85 | + return $this->data; |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * Get a hidden value from the context by its key, or return a default value if the key does not exist. |
| 90 | + * |
| 91 | + * @param string $key The key to identify the data. |
| 92 | + * @param mixed|null $default The default value to return if the key does not exist in the context. |
| 93 | + * @return mixed The value associated with the key, or the default value if the key does not exist. |
| 94 | + */ |
| 95 | + public function getHidden(string $key, mixed $default = null): mixed |
| 96 | + { |
| 97 | + return $this->hiddenData[$key] ?? $default; |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * Get all hidden data from the context |
| 102 | + * |
| 103 | + * @return array<string, mixed> An array of all key-value pairs in the hidden context. |
| 104 | + */ |
| 105 | + public function getAllHidden(): array |
| 106 | + { |
| 107 | + return $this->hiddenData; |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * Check if a key exists in the context. |
| 112 | + * |
| 113 | + * @param string $key The key to check for existence in the context. |
| 114 | + * @return bool True if the key exists in the context, false otherwise. |
| 115 | + */ |
| 116 | + public function has(string $key): bool |
| 117 | + { |
| 118 | + return array_key_exists($key, $this->data); |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * Check if a key exists in the hidden context. |
| 123 | + * |
| 124 | + * @param string $key The key to check for existence in the hidden context. |
| 125 | + * @return bool True if the key exists in the hidden context, false otherwise. |
| 126 | + */ |
| 127 | + public function hasHidden(string $key): bool |
| 128 | + { |
| 129 | + return array_key_exists($key, $this->hiddenData); |
| 130 | + } |
| 131 | + |
| 132 | + /** |
| 133 | + * Remove a key-value pair from the context by its key. |
| 134 | + * |
| 135 | + * @param string $key The key to identify the data to be removed from the context. |
| 136 | + * @return $this |
| 137 | + */ |
| 138 | + public function remove(string $key): self |
| 139 | + { |
| 140 | + unset($this->data[$key]); |
| 141 | + return $this; |
| 142 | + } |
| 143 | + |
| 144 | + /** |
| 145 | + * Remove a key-value pair from the hidden context by its key. |
| 146 | + * |
| 147 | + * @param string $key The key to identify the data to be removed from the hidden context. |
| 148 | + * @return $this |
| 149 | + */ |
| 150 | + public function removeHidden(string $key): self |
| 151 | + { |
| 152 | + unset($this->hiddenData[$key]); |
| 153 | + return $this; |
| 154 | + } |
| 155 | + |
| 156 | + /** |
| 157 | + * Clear all data from the context, including hidden data. |
| 158 | + * |
| 159 | + * @return $this |
| 160 | + */ |
| 161 | + public function clearAll(): self |
| 162 | + { |
| 163 | + $this->clear(); |
| 164 | + $this->clearHidden(); |
| 165 | + return $this; |
| 166 | + } |
| 167 | + |
| 168 | + /** |
| 169 | + * Clear all data from the context. |
| 170 | + * |
| 171 | + * @return $this |
| 172 | + */ |
| 173 | + public function clear(): self |
| 174 | + { |
| 175 | + $this->data = []; |
| 176 | + return $this; |
| 177 | + } |
| 178 | + |
| 179 | + /** |
| 180 | + * Clear all hidden data from the context. |
| 181 | + * |
| 182 | + * @return $this |
| 183 | + */ |
| 184 | + public function clearHidden(): self |
| 185 | + { |
| 186 | + $this->hiddenData = []; |
| 187 | + return $this; |
| 188 | + } |
| 189 | +} |
0 commit comments