PropertyPath.php 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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\Validator\Util;
  11. /**
  12. * Contains utility methods for dealing with property paths.
  13. *
  14. * For more extensive functionality, use Symfony's PropertyAccess component.
  15. *
  16. * @author Bernhard Schussek <bschussek@gmail.com>
  17. */
  18. class PropertyPath
  19. {
  20. /**
  21. * Appends a path to a given property path.
  22. *
  23. * If the base path is empty, the appended path will be returned unchanged.
  24. * If the base path is not empty, and the appended path starts with a
  25. * squared opening bracket ("["), the concatenation of the two paths is
  26. * returned. Otherwise, the concatenation of the two paths is returned,
  27. * separated by a dot (".").
  28. *
  29. * @return string The concatenation of the two property paths
  30. */
  31. public static function append(string $basePath, string $subPath)
  32. {
  33. if ('' !== $subPath) {
  34. if ('[' === $subPath[0]) {
  35. return $basePath.$subPath;
  36. }
  37. return '' !== $basePath ? $basePath.'.'.$subPath : $subPath;
  38. }
  39. return $basePath;
  40. }
  41. /**
  42. * Not instantiable.
  43. */
  44. private function __construct()
  45. {
  46. }
  47. }