Add Str::dotNotationToBrackets()

This commit is contained in:
Giuseppe Criscione 2022-11-19 23:50:19 +01:00
parent a81a076c8d
commit cd03ccba05

View File

@ -154,4 +154,13 @@ class Str
{
return static::endsWith($haystack, $needle) ? substr($haystack, 0, -strlen($needle)) : $haystack;
}
/**
* Convert dot notation to brackets notation
*/
public static function dotNotationToBrackets(string $string): string
{
$segments = explode('.', $string);
return array_shift($segments) . implode('', Arr::map($segments, fn ($segment) => '[' . $segment . ']'));
}
}