StringsUtils.php 1008 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Service\Utils;
  4. use App\Tests\Service\Utils\StringsUtilsTest;
  5. /**
  6. * Class StringsUtils : méthodes d'aide pour la gestion de string.
  7. * @package App\Service\Utils
  8. */
  9. class StringsUtils
  10. {
  11. /**
  12. * Supprime les quotes d'une chaine de caractères
  13. * @param string $str
  14. * @return string
  15. * @see StringsUtilsTest::testUnquote()
  16. */
  17. public static function unquote(string $str): string {
  18. return str_replace("'", "", $str);
  19. }
  20. /**
  21. * Convert CamelCase formatted string into snake_case
  22. * @see https://stackoverflow.com/a/40514305/4279120
  23. *
  24. * @param string $string
  25. * @param string $sep
  26. * @return string
  27. */
  28. public static function camelToSnake(string $string, string $sep = "_"): string {
  29. return strtolower(
  30. preg_replace(
  31. '/(?<=\d)(?=[A-Za-z])|(?<=[A-Za-z])(?=\d)|(?<=[a-z])(?=[A-Z])/',
  32. $sep,
  33. $string
  34. )
  35. );
  36. }
  37. }