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