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 if not yet existing from the class of an object
20 * The comparison is done with instanceof to support proxy classes and such.
22 * Only works with RouteObjectInterface routes that can return a referenced
25 * @author David Buchmann
27 class FieldByClassEnhancer implements RouteEnhancerInterface
30 * @var string field for the source class
34 * @var string field to write hashmap lookup result into
38 * @var array containing the mapping between a class name and the target value
43 * @param string $source the field name of the class
44 * @param string $target the field name to set from the map
45 * @param array $map the map of class names to field values
47 public function __construct($source, $target, $map)
49 $this->source = $source;
50 $this->target = $target;
55 * If the source field is instance of one of the entries in the map,
56 * target is set to the value of that map entry.
60 public function enhance(array $defaults, Request $request)
62 if (isset($defaults[$this->target])) {
63 // no need to do anything
67 if (!isset($defaults[$this->source])) {
71 // we need to loop over the array and do instanceof in case the content
72 // class extends the specified class
73 // i.e. phpcr-odm generates proxy class for the content.
74 foreach ($this->map as $class => $value) {
75 if ($defaults[$this->source] instanceof $class) {
76 // found a matching entry in the map
77 $defaults[$this->target] = $value;