Backup of db before drupal security update
[yaffs-website] / vendor / symfony-cmf / routing / Enhancer / FieldMapEnhancer.php
1 <?php
2
3 /*
4  * This file is part of the Symfony CMF package.
5  *
6  * (c) 2011-2015 Symfony CMF
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Symfony\Cmf\Component\Routing\Enhancer;
13
14 use Symfony\Component\HttpFoundation\Request;
15
16 /**
17  * This enhancer can fill one field with the result of a hashmap lookup of
18  * another field. If the target field is already set, it does nothing.
19  *
20  * @author David Buchmann
21  */
22 class FieldMapEnhancer implements RouteEnhancerInterface
23 {
24     /**
25      * @var string field for key in hashmap lookup
26      */
27     protected $source;
28     /**
29      * @var string field to write hashmap lookup result into
30      */
31     protected $target;
32     /**
33      * @var array containing the mapping between the source field value and target field value
34      */
35     protected $hashmap;
36
37     /**
38      * @param string $source  the field to read
39      * @param string $target  the field to write the result of the lookup into
40      * @param array  $hashmap for looking up value from source and get value for target
41      */
42     public function __construct($source, $target, array $hashmap)
43     {
44         $this->source = $source;
45         $this->target = $target;
46         $this->hashmap = $hashmap;
47     }
48
49     /**
50      * If the target field is not set but the source field is, map the field.
51      *
52      * {@inheritdoc}
53      */
54     public function enhance(array $defaults, Request $request)
55     {
56         if (isset($defaults[$this->target])) {
57             return $defaults;
58         }
59         if (!isset($defaults[$this->source])) {
60             return $defaults;
61         }
62         if (!isset($this->hashmap[$defaults[$this->source]])) {
63             return $defaults;
64         }
65
66         $defaults[$this->target] = $this->hashmap[$defaults[$this->source]];
67
68         return $defaults;
69     }
70 }