bd29ac08a7e8521f27a3bec9a25bdab218aba499
[yaffs-website] / vendor / symfony-cmf / routing / Enhancer / ContentRepositoryEnhancer.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\Cmf\Component\Routing\ContentRepositoryInterface;
15 use Symfony\Cmf\Component\Routing\RouteObjectInterface;
16 use Symfony\Component\HttpFoundation\Request;
17
18 /**
19  * This enhancer uses a ContentRepositoryInterface to load a content if $target
20  * is empty.
21  *
22  * $source specifies the field that contains the ID to load, $target specifies
23  * the field where to put the content returned by the repository.
24  *
25  * @author Samusev Andrey
26  */
27 class ContentRepositoryEnhancer implements RouteEnhancerInterface
28 {
29     /**
30      * @var ContentRepositoryInterface
31      */
32     private $contentRepository;
33
34     /**
35      * @var string
36      */
37     private $target;
38
39     /**
40      * @var string
41      */
42     private $source;
43
44     /**
45      * @param ContentRepositoryInterface $contentRepository repository to search for the content
46      * @param string                     $target            the field name to set content
47      * @param string                     $source            the field name of the request parameter that contains the id
48      */
49     public function __construct(
50         ContentRepositoryInterface $contentRepository,
51         $target = RouteObjectInterface::CONTENT_OBJECT,
52         $source = RouteObjectInterface::CONTENT_ID
53     ) {
54         $this->contentRepository = $contentRepository;
55         $this->target = $target;
56         $this->source = $source;
57     }
58
59     /**
60      * {@inheritdoc}
61      */
62     public function enhance(array $defaults, Request $request)
63     {
64         if (!isset($defaults[$this->target]) && isset($defaults[$this->source])) {
65             $defaults[$this->target] = $this->contentRepository->findById($defaults[$this->source]);
66         }
67
68         return $defaults;
69     }
70 }