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 if not yet existing from the class of an object
|
Chris@0
|
18 * in another field.
|
Chris@0
|
19 *
|
Chris@0
|
20 * The comparison is done with instanceof to support proxy classes and such.
|
Chris@0
|
21 *
|
Chris@0
|
22 * Only works with RouteObjectInterface routes that can return a referenced
|
Chris@0
|
23 * content.
|
Chris@0
|
24 *
|
Chris@0
|
25 * @author David Buchmann
|
Chris@0
|
26 */
|
Chris@0
|
27 class FieldByClassEnhancer implements RouteEnhancerInterface
|
Chris@0
|
28 {
|
Chris@0
|
29 /**
|
Chris@0
|
30 * @var string field for the source class
|
Chris@0
|
31 */
|
Chris@0
|
32 protected $source;
|
Chris@0
|
33 /**
|
Chris@0
|
34 * @var string field to write hashmap lookup result into
|
Chris@0
|
35 */
|
Chris@0
|
36 protected $target;
|
Chris@0
|
37 /**
|
Chris@0
|
38 * @var array containing the mapping between a class name and the target value
|
Chris@0
|
39 */
|
Chris@0
|
40 protected $map;
|
Chris@0
|
41
|
Chris@0
|
42 /**
|
Chris@0
|
43 * @param string $source the field name of the class
|
Chris@0
|
44 * @param string $target the field name to set from the map
|
Chris@0
|
45 * @param array $map the map of class names to field values
|
Chris@0
|
46 */
|
Chris@0
|
47 public function __construct($source, $target, $map)
|
Chris@0
|
48 {
|
Chris@0
|
49 $this->source = $source;
|
Chris@0
|
50 $this->target = $target;
|
Chris@0
|
51 $this->map = $map;
|
Chris@0
|
52 }
|
Chris@0
|
53
|
Chris@0
|
54 /**
|
Chris@0
|
55 * If the source field is instance of one of the entries in the map,
|
Chris@0
|
56 * target is set to the value of that map entry.
|
Chris@0
|
57 *
|
Chris@0
|
58 * {@inheritdoc}
|
Chris@0
|
59 */
|
Chris@0
|
60 public function enhance(array $defaults, Request $request)
|
Chris@0
|
61 {
|
Chris@0
|
62 if (isset($defaults[$this->target])) {
|
Chris@0
|
63 // no need to do anything
|
Chris@0
|
64 return $defaults;
|
Chris@0
|
65 }
|
Chris@0
|
66
|
Chris@0
|
67 if (!isset($defaults[$this->source])) {
|
Chris@0
|
68 return $defaults;
|
Chris@0
|
69 }
|
Chris@0
|
70
|
Chris@0
|
71 // we need to loop over the array and do instanceof in case the content
|
Chris@0
|
72 // class extends the specified class
|
Chris@0
|
73 // i.e. phpcr-odm generates proxy class for the content.
|
Chris@0
|
74 foreach ($this->map as $class => $value) {
|
Chris@0
|
75 if ($defaults[$this->source] instanceof $class) {
|
Chris@0
|
76 // found a matching entry in the map
|
Chris@0
|
77 $defaults[$this->target] = $value;
|
Chris@0
|
78
|
Chris@0
|
79 return $defaults;
|
Chris@0
|
80 }
|
Chris@0
|
81 }
|
Chris@0
|
82
|
Chris@0
|
83 return $defaults;
|
Chris@0
|
84 }
|
Chris@0
|
85 }
|