1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Vlaswinkel\Lua\JMS; |
4
|
|
|
|
5
|
|
|
use JMS\Serializer\GenericSerializationVisitor; |
6
|
|
|
use JMS\Serializer\GraphNavigator; |
7
|
|
|
use JMS\Serializer\Handler\SubscribingHandlerInterface; |
8
|
|
|
use Symfony\Component\Form\Form; |
9
|
|
|
use Symfony\Component\Form\FormError; |
10
|
|
|
use Symfony\Component\Translation\TranslatorInterface; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class LuaFormHandler |
14
|
|
|
* |
15
|
|
|
* @see https://github.com/schmittjoh/serializer/blob/1.1.0/src/JMS/Serializer/Handler/FormErrorHandler.php |
16
|
|
|
* |
17
|
|
|
* @author Johannes M. Schmitt <[email protected]> |
18
|
|
|
* @author Koen Vlaswinkel <[email protected]> |
19
|
|
|
* @package Vlaswinkel\Lua\JMS |
20
|
|
|
*/ |
21
|
|
|
class LuaFormHandler implements SubscribingHandlerInterface { |
22
|
|
|
private $translator; |
23
|
|
|
|
24
|
|
|
public static function getSubscribingMethods() { |
25
|
|
|
$methods = []; |
26
|
|
|
$methods[] = [ |
27
|
|
|
'direction' => GraphNavigator::DIRECTION_SERIALIZATION, |
28
|
|
|
'type' => 'Symfony\Component\Form\Form', |
29
|
|
|
'format' => 'lua', |
30
|
|
|
]; |
31
|
|
|
$methods[] = [ |
32
|
|
|
'direction' => GraphNavigator::DIRECTION_SERIALIZATION, |
33
|
|
|
'type' => 'Symfony\Component\Form\FormError', |
34
|
|
|
'format' => 'lua', |
35
|
|
|
]; |
36
|
|
|
return $methods; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function __construct(TranslatorInterface $translator) { |
40
|
|
|
$this->translator = $translator; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function serializeFormToLua(LuaSerializationVisitor $visitor, Form $form, array $type) { |
44
|
|
|
return $this->convertFormToArray($visitor, $form); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function serializeFormErrorToLua(LuaSerializationVisitor $visitor, FormError $formError, array $type) { |
48
|
|
|
return $this->getErrorMessage($formError); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
private function getErrorMessage(FormError $error) { |
52
|
|
|
if (null !== $error->getMessagePluralization()) { |
53
|
|
|
return $this->translator->transChoice( |
54
|
|
|
$error->getMessageTemplate(), |
55
|
|
|
$error->getMessagePluralization(), |
56
|
|
|
$error->getMessageParameters(), |
57
|
|
|
'validators' |
58
|
|
|
); |
59
|
|
|
} |
60
|
|
|
return $this->translator->trans($error->getMessageTemplate(), $error->getMessageParameters(), 'validators'); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
private function convertFormToArray(GenericSerializationVisitor $visitor, Form $data) { |
64
|
|
|
$isRoot = null === $visitor->getRoot(); |
65
|
|
|
$form = new \ArrayObject(); |
66
|
|
|
$errors = []; |
67
|
|
|
foreach ($data->getErrors() as $error) { |
68
|
|
|
$errors[] = $this->getErrorMessage($error); |
69
|
|
|
} |
70
|
|
|
if (!empty($errors)) { |
71
|
|
|
$form['errors'] = $errors; |
72
|
|
|
} |
73
|
|
|
$children = []; |
74
|
|
|
foreach ($data->all() as $child) { |
75
|
|
|
if ($child instanceof Form) { |
76
|
|
|
$children[$child->getName()] = $this->convertFormToArray($visitor, $child); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
if (!empty($children)) { |
80
|
|
|
$form['children'] = $children; |
81
|
|
|
} |
82
|
|
|
if ($isRoot) { |
83
|
|
|
$visitor->setRoot($form); |
84
|
|
|
} |
85
|
|
|
return $form; |
86
|
|
|
} |
87
|
|
|
} |