4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Form\Extension\Validator\ViolationMapper
;
14 use Symfony\Component\Form\FormInterface
;
15 use Symfony\Component\Form\Exception\ErrorMappingException
;
18 * @author Bernhard Schussek <bschussek@gmail.com>
30 private $propertyPath;
37 public function __construct(FormInterface
$origin, $propertyPath, $targetPath)
39 $this->origin
= $origin;
40 $this->propertyPath
= $propertyPath;
41 $this->targetPath
= $targetPath;
45 * @return FormInterface
47 public function getOrigin()
53 * Matches a property path against the rule path.
55 * If the rule matches, the form mapped by the rule is returned.
56 * Otherwise this method returns false.
58 * @param string $propertyPath The property path to match against the rule.
60 * @return null|FormInterface The mapped form or null.
62 public function match($propertyPath)
64 if ($propertyPath === (string) $this->propertyPath
) {
65 return $this->getTarget();
72 * Matches a property path against a prefix of the rule path.
74 * @param string $propertyPath The property path to match against the rule.
76 * @return Boolean Whether the property path is a prefix of the rule or not.
78 public function isPrefix($propertyPath)
80 $length = strlen($propertyPath);
81 $prefix = substr($this->propertyPath
, 0, $length);
82 $next = isset($this->propertyPath
[$length]) ? $this->propertyPath
[$length] : null;
84 return $prefix === $propertyPath && ('[' === $next || '.' === $next);
88 * @return FormInterface
90 * @throws ErrorMappingException
92 public function getTarget()
94 $childNames = explode('.', $this->targetPath
);
95 $target = $this->origin
;
97 foreach ($childNames as $childName) {
98 if (!$target->has($childName)) {
99 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()));
101 $target = $target->get($childName);