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 sets a field to a fixed value. You can specify a source field
|
Chris@0
|
18 * name to only set the target field if the source is present.
|
Chris@0
|
19 *
|
Chris@0
|
20 * @author David Buchmann
|
Chris@0
|
21 */
|
Chris@0
|
22 class FieldPresenceEnhancer implements RouteEnhancerInterface
|
Chris@0
|
23 {
|
Chris@0
|
24 /**
|
Chris@0
|
25 * Field name for the source field that must exist. If null, the target
|
Chris@0
|
26 * field is always set if not already present.
|
Chris@0
|
27 *
|
Chris@0
|
28 * @var string|null
|
Chris@0
|
29 */
|
Chris@0
|
30 protected $source;
|
Chris@0
|
31
|
Chris@0
|
32 /**
|
Chris@0
|
33 * Field name to write the value into.
|
Chris@0
|
34 *
|
Chris@0
|
35 * @var string
|
Chris@0
|
36 */
|
Chris@0
|
37 protected $target;
|
Chris@0
|
38
|
Chris@0
|
39 /**
|
Chris@0
|
40 * Value to set the target field to.
|
Chris@0
|
41 *
|
Chris@0
|
42 * @var string
|
Chris@0
|
43 */
|
Chris@0
|
44 private $value;
|
Chris@0
|
45
|
Chris@0
|
46 /**
|
Chris@0
|
47 * @param null|string $source the field name of the class, null to disable the check
|
Chris@0
|
48 * @param string $target the field name to set from the map
|
Chris@0
|
49 * @param string $value value to set target field to if source field exists
|
Chris@0
|
50 */
|
Chris@0
|
51 public function __construct($source, $target, $value)
|
Chris@0
|
52 {
|
Chris@0
|
53 $this->source = $source;
|
Chris@0
|
54 $this->target = $target;
|
Chris@0
|
55 $this->value = $value;
|
Chris@0
|
56 }
|
Chris@0
|
57
|
Chris@0
|
58 /**
|
Chris@0
|
59 * {@inheritdoc}
|
Chris@0
|
60 */
|
Chris@0
|
61 public function enhance(array $defaults, Request $request)
|
Chris@0
|
62 {
|
Chris@0
|
63 if (isset($defaults[$this->target])) {
|
Chris@0
|
64 // no need to do anything
|
Chris@0
|
65 return $defaults;
|
Chris@0
|
66 }
|
Chris@0
|
67
|
Chris@0
|
68 if (null !== $this->source && !isset($defaults[$this->source])) {
|
Chris@0
|
69 return $defaults;
|
Chris@0
|
70 }
|
Chris@0
|
71
|
Chris@0
|
72 $defaults[$this->target] = $this->value;
|
Chris@0
|
73
|
Chris@0
|
74 return $defaults;
|
Chris@0
|
75 }
|
Chris@0
|
76 }
|