Skip to content

Commit

Permalink
refactor(Str): Improve case conversion methods
Browse files Browse the repository at this point in the history
- Add `pascal` method to convert a value to pascal case
- Refactor `HasOptions` trait to use `pascal` case conversion method
  • Loading branch information
guanguans committed Jan 26, 2024
1 parent 30012d5 commit 4b41193
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
12 changes: 12 additions & 0 deletions src/Foundation/Support/Str.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,18 @@ class Str
*/
protected static $studlyCache = [];

/**
* Convert a value to camel case.
*
* @param string $value
*
* @return string
*/
public static function pascal($value)
{
return ucfirst(static::camel($value));
}

/**
* Convert a value to camel case.
*
Expand Down
12 changes: 8 additions & 4 deletions src/Foundation/Traits/HasOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,11 +154,15 @@ public function __call($name, $arguments)
{
$defined = $this->defined ?? [];

foreach ([null, 'snake'] as $method) {
$name = $method ? Str::{$method}($name) : $name;
foreach ([null, 'snake', 'pascal'] as $case) {
$casedName = $case ? Str::{$case}($name) : $name;

if (in_array($name, $defined, true)) {
return $this->setOption($name, $arguments[0] ?? null);
if (in_array($casedName, $defined, true)) {
if (empty($arguments)) {
throw new \InvalidArgumentException(sprintf('Method %s::%s requires an argument', static::class, $name));
}

return $this->setOption($casedName, $arguments[0]);
}
}

Expand Down

0 comments on commit 4b41193

Please sign in to comment.