-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGaufretteParamConverter.php
59 lines (47 loc) · 1.48 KB
/
GaufretteParamConverter.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
<?php
namespace rs\GaufretteBrowserBundle\ParamConverter;
use rs\GaufretteBrowserBundle\Entity\GaufretteRepository;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
use Sensio\Bundle\FrameworkExtraBundle\Request\ParamConverter\ParamConverterInterface;
use Symfony\Component\HttpFoundation\Request;
abstract class GaufretteParamConverter implements ParamConverterInterface
{
/**
* @var GaufretteRepository
*/
private $repository;
private $class;
public function __construct(GaufretteRepository $repository, $class)
{
$this->repository = $repository;
$this->class = $class;
}
/**
* {@inheritDoc}
*/
public function apply(Request $request, ParamConverter $configuration)
{
/** @var $configuration ParamConverter */
$name = $configuration->getName();
$options = $configuration->getOptions();
$object = $this->repository->findOneBy(array('slug' => $request->get($options['id'])));
if ($object) {
$request->attributes->set($name, $object);
return true;
}
return false;
}
/**
* {@inheritDoc}
*/
public function supports(ParamConverter $configuration)
{
if (null === $configuration->getClass()) {
return false;
}
if (!array_key_exists('id', $configuration->getOptions())) {
return false;
}
return $this->class == $configuration->getClass();
}
}