4 * This file is part of the Symfony CMF package.
6 * (c) 2011-2015 Symfony CMF
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Cmf\Component\Routing\Enhancer;
14 use Symfony\Component\HttpFoundation\Request;
17 * This enhancer sets a field to a fixed value. You can specify a source field
18 * name to only set the target field if the source is present.
20 * @author David Buchmann
22 class FieldPresenceEnhancer implements RouteEnhancerInterface
25 * Field name for the source field that must exist. If null, the target
26 * field is always set if not already present.
33 * Field name to write the value into.
40 * Value to set the target field to.
47 * @param null|string $source the field name of the class, null to disable the check
48 * @param string $target the field name to set from the map
49 * @param string $value value to set target field to if source field exists
51 public function __construct($source, $target, $value)
53 $this->source = $source;
54 $this->target = $target;
55 $this->value = $value;
61 public function enhance(array $defaults, Request $request)
63 if (isset($defaults[$this->target])) {
64 // no need to do anything
68 if (null !== $this->source && !isset($defaults[$this->source])) {
72 $defaults[$this->target] = $this->value;