Skip to content

Commit

Permalink
refactor: don't convert uppercase keys to camel
Browse files Browse the repository at this point in the history
  • Loading branch information
021-projects committed Feb 23, 2024
1 parent 9d16ea1 commit 7d5ef35
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 10 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
},
"require": {
"illuminate/support": ">=7.0 <11.0",
"php": ">=8.0 <8.4",
"php": ">=8.1 <8.4",
"psr/http-message": "^2.0"
},
"minimum-stability": "dev",
Expand Down
23 changes: 16 additions & 7 deletions src/BaseEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ public function __construct(array|string|ResponseInterface $props = [])
}

if ($props) {
// convert all keys to camel case
$keys = array_map([Str::class, 'camel'], array_keys($props));
$keys = array_map($this->propKey(...), array_keys($props));
$props = array_combine($keys, $props);
}

Expand Down Expand Up @@ -51,7 +50,7 @@ public function __get($key)

public function __set(string $name, $value): void
{
$this->put(Str::camel($name), $value);
$this->put($this->propKey($name), $value);
}

public function offsetGet($key): mixed
Expand All @@ -63,7 +62,7 @@ public function offsetGet($key): mixed
public function offsetSet($key, $value): void
{
$this->assertValidPropertyKey($key);
parent::offsetSet(Str::camel($key), $value);
parent::offsetSet($this->propKey($key), $value);
}

public function offsetExists($key): bool
Expand All @@ -75,7 +74,7 @@ public function offsetExists($key): bool
public function offsetUnset($key): void
{
$this->assertValidPropertyKey($key);
parent::offsetUnset(Str::camel($key));
parent::offsetUnset($this->propKey($key));
}

public function __isset(string $name): bool
Expand All @@ -93,7 +92,7 @@ public function __isset(string $name): bool
*/
protected function getPropertyValue(string $key, mixed $default = null): mixed
{
$property = Str::camel($key);
$property = $this->propKey($key);

$valueRaw = $this->offsetExists($property)
? $this->items[$property]
Expand All @@ -112,7 +111,7 @@ protected function getPropertyValue(string $key, mixed $default = null): mixed

protected function hasPropertyValue(string $key): bool
{
return isset($this->items[Str::camel($key)]) || $this->hasGetter($key);
return isset($this->items[$this->propKey($key)]) || $this->hasGetter($key);
}

protected function assertValidPropertyKey(string $key): void
Expand All @@ -121,4 +120,14 @@ protected function assertValidPropertyKey(string $key): void
throw new \InvalidArgumentException('Key must be a string');
}
}

protected function propKey(string $key): string
{
// ignore uppercase keys
if (strtoupper($key) === $key) {
return $key;
}

return Str::camel($key);
}
}
4 changes: 2 additions & 2 deletions src/Concerns/HasGetters.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public function hasGetter($key): bool
return static::$getterCache[get_class($this)][$key];
}

if (! method_exists($this, $method = Str::camel($key))) {
if (! method_exists($this, $method = $this->propKey($key))) {
return static::$getterCache[get_class($this)][$key] = false;
}

Expand All @@ -36,7 +36,7 @@ protected function extractGetter($key, $valueRaw): mixed
return $this->extractedGetters[$key];
}

$getterMethod = Str::camel($key);
$getterMethod = $this->propKey($key);

/** @var \O21\ApiEntity\Casts\Getter $getter */
$getter = $this->$getterMethod();
Expand Down
7 changes: 7 additions & 0 deletions tests/BaseEntityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,4 +150,11 @@ public function test_array_access(): void
$this->assertInstanceOf(Collection::class, $this->user['settings']);
$this->assertInstanceOf(Dog::class, $this->user['dogs'][0]);
}

public function test_upper_case_keys_not_converting_to_snake(): void
{
$user = new User(['ID' => 1, 'FirstName' => 'John']);
$this->assertEquals(1, $user->ID);
$this->assertEquals('John', $user->firstName);
}
}

0 comments on commit 7d5ef35

Please sign in to comment.