Completed
Push — master ( e1388e...5e3850 )
by
unknown
19:37
created

Serializer/Normalizer/FormErrorHandler.php (3 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
/*
4
 * This file is part of the FOSRestBundle package.
5
 *
6
 * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
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 FOS\RestBundle\Serializer\Normalizer;
13
14
use JMS\Serializer\Context;
15
use JMS\Serializer\Handler\FormErrorHandler as JMSFormErrorHandler;
16
use JMS\Serializer\Handler\SubscribingHandlerInterface;
17
use JMS\Serializer\JsonSerializationVisitor;
18
use JMS\Serializer\XmlSerializationVisitor;
19
use Symfony\Component\Form\Form;
20
use JMS\Serializer\YamlSerializationVisitor;
21
22
/**
23
 * Extend the JMS FormErrorHandler to include more informations when using the ViewHandler.
24
 */
25
class FormErrorHandler implements SubscribingHandlerInterface
26
{
27
    private $formErrorHandler;
28
29 2
    public function __construct(JMSFormErrorHandler $formErrorHandler)
30
    {
31 2
        $this->formErrorHandler = $formErrorHandler;
32 2
    }
33 2
34
    public static function getSubscribingMethods()
35 1
    {
36
        return JMSFormErrorHandler::getSubscribingMethods();
37 1
    }
38 1
39 1
    public function serializeFormToXml(XmlSerializationVisitor $visitor, Form $form, array $type, Context $context = null)
40 1
    {
41 1
        if ($context) {
42 1
            $statusCode = $context->attributes->get('status_code');
0 ignored issues
show
Deprecated Code introduced by
The property JMS\Serializer\Context::$attributes has been deprecated with message: use has/get/set attribute methods

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
43
            if ($statusCode->isDefined()) {
44 1 View Code Duplication
                if (null === $visitor->document) {
45 1
                    $visitor->document = $visitor->createDocument(null, null, true);
0 ignored issues
show
Deprecated Code introduced by
The method JMS\Serializer\XmlSerial...sitor::createDocument() has been deprecated with message: Use $this->getDocument(true) instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
46 1
                }
47
48 1
                $codeNode = $visitor->document->createElement('code');
49 1
                $visitor->getCurrentNode()->appendChild($codeNode);
50 1
                $codeNode->appendChild($context->getNavigator()->accept($statusCode->get(), null, $context));
51
52 1
                $messageNode = $visitor->document->createElement('message');
53 1
                $visitor->getCurrentNode()->appendChild($messageNode);
54 1
                $messageNode->appendChild($context->getNavigator()->accept('Validation Failed', null, $context));
55 1
56 1
                $errorsNode = $visitor->document->createElement('errors');
57
                $visitor->getCurrentNode()->appendChild($errorsNode);
58 1
                $visitor->setCurrentNode($errorsNode);
59
                $this->formErrorHandler->serializeFormToXml($visitor, $form, $type);
60
                $visitor->revertCurrentNode();
61
62 1
                return;
63
            }
64
        }
65 1
66
        return $this->formErrorHandler->serializeFormToXml($visitor, $form, $type);
67 1
    }
68 1
69 View Code Duplication
    public function serializeFormToJson(JsonSerializationVisitor $visitor, Form $form, array $type, Context $context = null)
70 1
    {
71 1
        $isRoot = null === $visitor->getRoot();
72 1
        $result = $this->adaptFormArray($this->formErrorHandler->serializeFormToJson($visitor, $form, $type), $context);
73
74 1
        if ($isRoot) {
75
            $visitor->setRoot($result);
76
        }
77
78
        return $result;
79
    }
80
81 View Code Duplication
    public function serializeFormToYml(YamlSerializationVisitor $visitor, Form $form, array $type, Context $context = null)
82
    {
83
        $isRoot = null === $visitor->getRoot();
84
        $result = $this->adaptFormArray($this->formErrorHandler->serializeFormToYml($visitor, $form, $type), $context);
85
86
        if ($isRoot) {
87
            $visitor->setRoot($result);
88
        }
89 1
90
        return $result;
91 1
    }
92 1
93
    public function __call($name, $arguments)
94 1
    {
95 1
        return call_user_func_array([$this->formErrorHandler, $name], $arguments);
96 1
    }
97 1
98
    private function adaptFormArray(\ArrayObject $serializedForm, Context $context = null)
99
    {
100
        $statusCode = $this->getStatusCode($context);
101
        if (null !== $statusCode) {
102
            return [
103 1
                'code' => $statusCode,
104
                'message' => 'Validation Failed',
105 1
                'errors' => $serializedForm,
106
            ];
107
        }
108
109 1
        return $serializedForm;
110 1
    }
111 1
112
    private function getStatusCode(Context $context = null)
113
    {
114
        if (null === $context) {
115
            return;
116
        }
117
118
        $statusCode = $context->attributes->get('status_code');
0 ignored issues
show
Deprecated Code introduced by
The property JMS\Serializer\Context::$attributes has been deprecated with message: use has/get/set attribute methods

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
119
        if ($statusCode->isDefined()) {
120
            return $statusCode->get();
121
        }
122
    }
123
}
124