Skip to content

Commit 9f77ab8

Browse files
committed
Returning $default if $key is not present. And only calling ArrayHelper if needed.
1 parent f066488 commit 9f77ab8

File tree

1 file changed

+81
-2
lines changed

1 file changed

+81
-2
lines changed

system/Context/Context.php

Lines changed: 81 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,24 @@ public function set(array|string $key, mixed $value = null): self
5252
{
5353
if (is_array($key)) {
5454
foreach ($key as $k => $v) {
55+
if (! $this->hasDotNotation($k)) {
56+
$this->data[$k] = $v;
57+
58+
continue;
59+
}
60+
5561
ArrayHelper::dotSet($this->data, $k, $v);
5662
}
5763

5864
return $this;
5965
}
6066

67+
if (! $this->hasDotNotation($key)) {
68+
$this->data[$key] = $value;
69+
70+
return $this;
71+
}
72+
6173
ArrayHelper::dotSet($this->data, $key, $value);
6274

6375
return $this;
@@ -76,12 +88,24 @@ public function setHidden(#[SensitiveParameter] array|string $key, #[SensitivePa
7688
{
7789
if (is_array($key)) {
7890
foreach ($key as $k => $v) {
91+
if (! $this->hasDotNotation($k)) {
92+
$this->hiddenData[$k] = $v;
93+
94+
continue;
95+
}
96+
7997
ArrayHelper::dotSet($this->hiddenData, $k, $v);
8098
}
8199

82100
return $this;
83101
}
84102

103+
if (! $this->hasDotNotation($key)) {
104+
$this->hiddenData[$key] = $value;
105+
106+
return $this;
107+
}
108+
85109
ArrayHelper::dotSet($this->hiddenData, $key, $value);
86110

87111
return $this;
@@ -98,7 +122,16 @@ public function setHidden(#[SensitiveParameter] array|string $key, #[SensitivePa
98122
*/
99123
public function get(string $key, mixed $default = null): mixed
100124
{
101-
return ArrayHelper::dotSearch($key, $this->data) ?? $default;
125+
if (! $this->has($key)) {
126+
return $default;
127+
}
128+
129+
// Exit early if the key is not a dot notation to avoid unnecessary processing in the common case.
130+
if (! $this->hasDotNotation($key)) {
131+
return $this->data[$key];
132+
}
133+
134+
return ArrayHelper::dotSearch($key, $this->data);
102135
}
103136

104137
/**
@@ -148,7 +181,16 @@ public function getAll(): array
148181
*/
149182
public function getHidden(#[SensitiveParameter] string $key, #[SensitiveParameter] mixed $default = null): mixed
150183
{
151-
return ArrayHelper::dotSearch($key, $this->hiddenData) ?? $default;
184+
if (! $this->hasHidden($key)) {
185+
return $default;
186+
}
187+
188+
// Exit early if the key is not a dot notation to avoid unnecessary processing in the common case.
189+
if (! $this->hasDotNotation($key)) {
190+
return $this->hiddenData[$key];
191+
}
192+
193+
return ArrayHelper::dotSearch($key, $this->hiddenData);
152194
}
153195

154196
/**
@@ -197,6 +239,10 @@ public function getAllHidden(): array
197239
*/
198240
public function has(string $key): bool
199241
{
242+
if (! $this->hasDotNotation($key)) {
243+
return array_key_exists($key, $this->data);
244+
}
245+
200246
return ArrayHelper::dotHas($key, $this->data);
201247
}
202248

@@ -210,6 +256,10 @@ public function has(string $key): bool
210256
*/
211257
public function hasHidden(string $key): bool
212258
{
259+
if (! $this->hasDotNotation($key)) {
260+
return array_key_exists($key, $this->hiddenData);
261+
}
262+
213263
return ArrayHelper::dotHas($key, $this->hiddenData);
214264
}
215265

@@ -225,12 +275,24 @@ public function remove(array|string $key): self
225275
{
226276
if (is_array($key)) {
227277
foreach ($key as $k) {
278+
if (! $this->hasDotNotation($k)) {
279+
unset($this->data[$k]);
280+
281+
continue;
282+
}
283+
228284
ArrayHelper::dotUnset($this->data, $k);
229285
}
230286

231287
return $this;
232288
}
233289

290+
if (! $this->hasDotNotation($key)) {
291+
unset($this->data[$key]);
292+
293+
return $this;
294+
}
295+
234296
ArrayHelper::dotUnset($this->data, $key);
235297

236298
return $this;
@@ -248,12 +310,24 @@ public function removeHidden(#[SensitiveParameter] array|string $key): self
248310
{
249311
if (is_array($key)) {
250312
foreach ($key as $k) {
313+
if (! $this->hasDotNotation($k)) {
314+
unset($this->hiddenData[$k]);
315+
316+
continue;
317+
}
318+
251319
ArrayHelper::dotUnset($this->hiddenData, $k);
252320
}
253321

254322
return $this;
255323
}
256324

325+
if (! $this->hasDotNotation($key)) {
326+
unset($this->hiddenData[$key]);
327+
328+
return $this;
329+
}
330+
257331
ArrayHelper::dotUnset($this->hiddenData, $key);
258332

259333
return $this;
@@ -324,4 +398,9 @@ public function __unserialize(array $data): void
324398
$this->data = $data['data'] ?? [];
325399
$this->hiddenData = [];
326400
}
401+
402+
private function hasDotNotation(string $key): bool
403+
{
404+
return str_contains($key, '.');
405+
}
327406
}

0 commit comments

Comments
 (0)