/
UrlProperty.php
66 lines (55 loc) · 1.67 KB
/
UrlProperty.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
60
61
62
63
64
65
66
<?php
namespace Oro\Bundle\DataGridBundle\Extension\Formatter\Property;
use Symfony\Component\Routing\RouterInterface;
use Oro\Bundle\DataGridBundle\Datasource\ResultRecordInterface;
class UrlProperty extends AbstractProperty
{
const ROUTE_KEY = 'route';
const IS_ABSOLUTE_KEY = 'isAbsolute';
const ANCHOR_KEY = 'anchor';
const PARAMS_KEY = 'params';
const DIRECT_PARAMS_KEY = 'direct_params';
/** @var array */
protected $excludeParams = [self::ROUTE_KEY, self::IS_ABSOLUTE_KEY, self::ANCHOR_KEY, self::PARAMS_KEY];
/**
* @var RouterInterface
*/
protected $router;
/**
* @param RouterInterface $router
*/
public function __construct(RouterInterface $router)
{
$this->router = $router;
}
/**
* {@inheritdoc}
*/
public function getRawValue(ResultRecordInterface $record)
{
$route = $this->router->generate(
$this->get(self::ROUTE_KEY),
$this->getParameters($record),
$this->getOr(self::IS_ABSOLUTE_KEY, false)
);
return $route . $this->getOr(self::ANCHOR_KEY);
}
/**
* Get route parameters from record
*
* @param ResultRecordInterface $record
*
* @return array
*/
protected function getParameters(ResultRecordInterface $record)
{
$result = [];
foreach ($this->getOr(self::PARAMS_KEY, []) as $name => $dataKey) {
if (is_numeric($name)) {
$name = $dataKey;
}
$result[$name] = $record->getValue($dataKey);
}
return array_merge($result, $this->getOr(static::DIRECT_PARAMS_KEY, []));
}
}