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