StrCache.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * This file is part of Hyperf.
  5. *
  6. * @link https://www.hyperf.io
  7. * @document https://hyperf.wiki
  8. * @contact group@hyperf.io
  9. * @license https://github.com/hyperf/hyperf/blob/master/LICENSE
  10. */
  11. namespace Hyperf\Stringable;
  12. class StrCache
  13. {
  14. /**
  15. * The cache of snake-cased words.
  16. */
  17. protected static array $snakeCache = [];
  18. /**
  19. * The cache of camel-cased words.
  20. */
  21. protected static array $camelCache = [];
  22. /**
  23. * The cache of studly-cased words.
  24. */
  25. protected static array $studlyCache = [];
  26. public static function camel($value)
  27. {
  28. if (isset(static::$camelCache[$value])) {
  29. return static::$camelCache[$value];
  30. }
  31. return static::$camelCache[$value] = Str::camel($value);
  32. }
  33. public static function snake(string $value, string $delimiter = '_'): string
  34. {
  35. if (isset(static::$snakeCache[$value][$delimiter])) {
  36. return static::$snakeCache[$value][$delimiter];
  37. }
  38. return static::$snakeCache[$value][$delimiter] = Str::snake($value, $delimiter);
  39. }
  40. public static function studly(string $value, string $gap = ''): string
  41. {
  42. if (isset(static::$studlyCache[$value][$gap])) {
  43. return static::$studlyCache[$value][$gap];
  44. }
  45. return static::$studlyCache[$value][$gap] = Str::studly($value, $gap);
  46. }
  47. }