1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* CancelButton.php |
5
|
|
|
* |
6
|
|
|
* @since 29/05/16 |
7
|
|
|
* @author gseidel |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Enhavo\Bundle\AppBundle\Action\Type; |
11
|
|
|
|
12
|
|
|
use Enhavo\Bundle\AppBundle\Action\AbstractActionType; |
13
|
|
|
use Enhavo\Bundle\AppBundle\Action\ActionTypeInterface; |
14
|
|
|
use Enhavo\Bundle\AppBundle\Util\ArrayUtil; |
15
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
16
|
|
|
use Symfony\Component\Routing\RouterInterface; |
17
|
|
|
use Symfony\Contracts\Translation\TranslatorInterface; |
18
|
|
|
|
19
|
|
|
class SaveActionType extends AbstractActionType implements ActionTypeInterface |
20
|
|
|
{ |
21
|
|
|
/** @var RouterInterface */ |
22
|
|
|
private $router; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* CreateAction constructor. |
26
|
|
|
* @param TranslatorInterface $translator |
27
|
|
|
* @param RouterInterface $router |
28
|
|
|
*/ |
29
|
|
|
public function __construct(TranslatorInterface $translator, RouterInterface $router) |
30
|
|
|
{ |
31
|
|
|
parent::__construct($translator); |
32
|
|
|
$this->router = $router; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function createViewData(array $options, $resource = null) |
36
|
|
|
{ |
37
|
|
|
$data = parent::createViewData($options, $resource); |
38
|
|
|
|
39
|
|
|
$url = null; |
40
|
|
|
if ($options['route']) { |
41
|
|
|
$url = $this->getUrl($options, $resource); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
$data = ArrayUtil::merge($data, [ |
45
|
|
|
'url' => $url |
46
|
|
|
]); |
47
|
|
|
|
48
|
|
|
return $data; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
private function getUrl(array $options, $resource = null) |
52
|
|
|
{ |
53
|
|
|
$parameters['id'] = $resource->getId(); |
|
|
|
|
54
|
|
|
$parameters = array_merge_recursive($parameters, $options['route_parameters']); |
55
|
|
|
return $this->router->generate($options['route'], $parameters); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function configureOptions(OptionsResolver $resolver) |
59
|
|
|
{ |
60
|
|
|
parent::configureOptions($resolver); |
61
|
|
|
|
62
|
|
|
$resolver->setDefaults([ |
63
|
|
|
'component' => 'save-action', |
64
|
|
|
'label' => 'label.save', |
65
|
|
|
'translation_domain' => 'EnhavoAppBundle', |
66
|
|
|
'icon' => 'save', |
67
|
|
|
'route' => null, |
68
|
|
|
'route_parameters' => [], |
69
|
|
|
]); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function getType() |
73
|
|
|
{ |
74
|
|
|
return 'save'; |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.