StringUtil.php 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Form\Util;
  11. /**
  12. * @author Issei Murasawa <issei.m7@gmail.com>
  13. * @author Bernhard Schussek <bschussek@gmail.com>
  14. */
  15. class StringUtil
  16. {
  17. /**
  18. * This class should not be instantiated.
  19. */
  20. private function __construct()
  21. {
  22. }
  23. /**
  24. * Returns the trimmed data.
  25. *
  26. * @return string
  27. */
  28. public static function trim(string $string)
  29. {
  30. if (null !== $result = @preg_replace('/^[\pZ\pC]+|[\pZ\pC]+$/u', '', $string)) {
  31. return $result;
  32. }
  33. return trim($string);
  34. }
  35. /**
  36. * Converts a fully-qualified class name to a block prefix.
  37. *
  38. * @param string $fqcn The fully-qualified class name
  39. *
  40. * @return string|null The block prefix or null if not a valid FQCN
  41. */
  42. public static function fqcnToBlockPrefix(string $fqcn)
  43. {
  44. // Non-greedy ("+?") to match "type" suffix, if present
  45. if (preg_match('~([^\\\\]+?)(type)?$~i', $fqcn, $matches)) {
  46. return strtolower(preg_replace(['/([A-Z]+)([A-Z][a-z])/', '/([a-z\d])([A-Z])/'], ['\\1_\\2', '\\1_\\2'], $matches[1]));
  47. }
  48. return null;
  49. }
  50. }