1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Sylius package. |
5
|
|
|
* |
6
|
|
|
* (c) Paweł Jędrzejewski |
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 Sylius\Bundle\ProductBundle\Controller; |
13
|
|
|
|
14
|
|
|
use FOS\RestBundle\View\View; |
15
|
|
|
use Sylius\Bundle\ProductBundle\Form\Type\ProductAttributeChoiceType; |
16
|
|
|
use Sylius\Bundle\ResourceBundle\Controller\ResourceController; |
17
|
|
|
use Sylius\Component\Attribute\Model\AttributeInterface; |
18
|
|
|
use Symfony\Component\Form\FormView; |
19
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
20
|
|
|
use Symfony\Component\HttpFoundation\Request; |
21
|
|
|
use Symfony\Component\HttpFoundation\Response; |
22
|
|
|
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @author Mateusz Zalewski <[email protected]> |
26
|
|
|
*/ |
27
|
|
|
class ProductAttributeController extends ResourceController |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* @param Request $request |
31
|
|
|
* @param string $template |
32
|
|
|
* |
33
|
|
|
* @return Response |
34
|
|
|
*/ |
35
|
|
|
public function getAttributeTypesAction(Request $request, $template) |
36
|
|
|
{ |
37
|
|
|
$configuration = $this->requestConfigurationFactory->create($this->metadata, $request); |
38
|
|
|
|
39
|
|
|
$view = View::create() |
40
|
|
|
->setTemplate($template) |
41
|
|
|
->setTemplateVar($this->metadata->getPluralName()) |
42
|
|
|
->setData([ |
43
|
|
|
'types' => $this->get('sylius.registry.attribute_type')->all(), |
44
|
|
|
'metadata' => $this->metadata, |
45
|
|
|
]) |
46
|
|
|
; |
47
|
|
|
|
48
|
|
|
return $this->viewHandler->handle($configuration, $view); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @return Response |
53
|
|
|
*/ |
54
|
|
|
public function renderAttributesAction(Request $request) |
55
|
|
|
{ |
56
|
|
|
$template = $request->attributes->get('template', 'SyliusAttributeBundle::attributeChoice.html.twig'); |
57
|
|
|
|
58
|
|
|
$form = $this->get('form.factory')->create(ProductAttributeChoiceType::class, null, [ |
59
|
|
|
'multiple' => true, |
60
|
|
|
]); |
61
|
|
|
|
62
|
|
|
return $this->render($template, ['form' => $form->createView()]); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param Request $request |
67
|
|
|
* |
68
|
|
|
* @return Response |
69
|
|
|
*/ |
70
|
|
|
public function renderAttributeValueFormsAction(Request $request) |
71
|
|
|
{ |
72
|
|
|
$template = $request->attributes->get('template', 'SyliusAttributeBundle::attributeValueForms.html.twig'); |
73
|
|
|
|
74
|
|
|
$form = $this->get('form.factory')->create(ProductAttributeChoiceType::class, null, [ |
75
|
|
|
'multiple' => true, |
76
|
|
|
]); |
77
|
|
|
$form->handleRequest($request); |
78
|
|
|
|
79
|
|
|
$attributes = $form->getData(); |
80
|
|
|
if (null === $attributes) { |
81
|
|
|
throw new BadRequestHttpException(); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
foreach ($attributes as $attribute) { |
85
|
|
|
$forms[$attribute->getId()] = $this->getAttributeForm($attribute); |
|
|
|
|
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
return $this->render($template, [ |
89
|
|
|
'forms' => $forms, |
|
|
|
|
90
|
|
|
'metadata' => $this->metadata, |
91
|
|
|
]); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @param AttributeInterface $attribute |
96
|
|
|
* |
97
|
|
|
* @return FormView |
98
|
|
|
*/ |
99
|
|
|
protected function getAttributeForm(AttributeInterface $attribute) |
100
|
|
|
{ |
101
|
|
|
$attributeForm = $this->get('sylius.form_registry.attribute_type')->get($attribute->getType(), 'default'); |
102
|
|
|
|
103
|
|
|
$form = $this |
104
|
|
|
->get('form.factory') |
105
|
|
|
->createNamed('value', $attributeForm, null, ['label' => $attribute->getName()]) |
106
|
|
|
; |
107
|
|
|
|
108
|
|
|
return $form->createView(); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
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.