MappingRule.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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\Extension\Validator\ViolationMapper;
  11. use Symfony\Component\Form\Exception\ErrorMappingException;
  12. use Symfony\Component\Form\FormInterface;
  13. /**
  14. * @author Bernhard Schussek <bschussek@gmail.com>
  15. */
  16. class MappingRule
  17. {
  18. private $origin;
  19. private $propertyPath;
  20. private $targetPath;
  21. /**
  22. * @param FormInterface $origin
  23. * @param string $propertyPath
  24. * @param string $targetPath
  25. */
  26. public function __construct(FormInterface $origin, $propertyPath, $targetPath)
  27. {
  28. $this->origin = $origin;
  29. $this->propertyPath = $propertyPath;
  30. $this->targetPath = $targetPath;
  31. }
  32. /**
  33. * @return FormInterface
  34. */
  35. public function getOrigin()
  36. {
  37. return $this->origin;
  38. }
  39. /**
  40. * Matches a property path against the rule path.
  41. *
  42. * If the rule matches, the form mapped by the rule is returned.
  43. * Otherwise this method returns false.
  44. *
  45. * @param string $propertyPath The property path to match against the rule
  46. *
  47. * @return FormInterface|null The mapped form or null
  48. */
  49. public function match($propertyPath)
  50. {
  51. if ($propertyPath === (string) $this->propertyPath) {
  52. return $this->getTarget();
  53. }
  54. }
  55. /**
  56. * Matches a property path against a prefix of the rule path.
  57. *
  58. * @param string $propertyPath The property path to match against the rule
  59. *
  60. * @return bool Whether the property path is a prefix of the rule or not
  61. */
  62. public function isPrefix($propertyPath)
  63. {
  64. $length = \strlen($propertyPath);
  65. $prefix = substr($this->propertyPath, 0, $length);
  66. $next = isset($this->propertyPath[$length]) ? $this->propertyPath[$length] : null;
  67. return $prefix === $propertyPath && ('[' === $next || '.' === $next);
  68. }
  69. /**
  70. * @return FormInterface
  71. *
  72. * @throws ErrorMappingException
  73. */
  74. public function getTarget()
  75. {
  76. $childNames = explode('.', $this->targetPath);
  77. $target = $this->origin;
  78. foreach ($childNames as $childName) {
  79. if (!$target->has($childName)) {
  80. throw new ErrorMappingException(sprintf('The child "%s" of "%s" mapped by the rule "%s" in "%s" does not exist.', $childName, $target->getName(), $this->targetPath, $this->origin->getName()));
  81. }
  82. $target = $target->get($childName);
  83. }
  84. return $target;
  85. }
  86. }