comparison vendor/symfony-cmf/routing/Enhancer/FieldPresenceEnhancer.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:4c8ae668cc8c
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 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.
19 *
20 * @author David Buchmann
21 */
22 class FieldPresenceEnhancer implements RouteEnhancerInterface
23 {
24 /**
25 * Field name for the source field that must exist. If null, the target
26 * field is always set if not already present.
27 *
28 * @var string|null
29 */
30 protected $source;
31
32 /**
33 * Field name to write the value into.
34 *
35 * @var string
36 */
37 protected $target;
38
39 /**
40 * Value to set the target field to.
41 *
42 * @var string
43 */
44 private $value;
45
46 /**
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
50 */
51 public function __construct($source, $target, $value)
52 {
53 $this->source = $source;
54 $this->target = $target;
55 $this->value = $value;
56 }
57
58 /**
59 * {@inheritdoc}
60 */
61 public function enhance(array $defaults, Request $request)
62 {
63 if (isset($defaults[$this->target])) {
64 // no need to do anything
65 return $defaults;
66 }
67
68 if (null !== $this->source && !isset($defaults[$this->source])) {
69 return $defaults;
70 }
71
72 $defaults[$this->target] = $this->value;
73
74 return $defaults;
75 }
76 }