PropertyPath.php 1.5 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\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. * @param string $basePath The base path
  30. * @param string $subPath The path to append
  31. *
  32. * @return string The concatenation of the two property paths
  33. */
  34. public static function append($basePath, $subPath)
  35. {
  36. if ('' !== (string) $subPath) {
  37. if ('[' === $subPath[0]) {
  38. return $basePath.$subPath;
  39. }
  40. return '' !== (string) $basePath ? $basePath.'.'.$subPath : $subPath;
  41. }
  42. return $basePath;
  43. }
  44. /**
  45. * Not instantiable.
  46. */
  47. private function __construct()
  48. {
  49. }
  50. }