Completed
Push — 2.x ( 369149...9ea46e )
by Guilhem
12:14 queued 10:32
created

ViewHandler::__construct()   B

Complexity

Conditions 6
Paths 4

Size

Total Lines 38

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 6.0052

Importance

Changes 0
Metric Value
dl 0
loc 38
ccs 18
cts 19
cp 0.9474
rs 8.6897
c 0
b 0
f 0
cc 6
nc 4
nop 11
crap 6.0052

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

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\View;
13
14
use FOS\RestBundle\Context\Context;
15
use FOS\RestBundle\Serializer\Serializer;
16
use Symfony\Component\Form\FormInterface;
17
use Symfony\Component\HttpFoundation\RedirectResponse;
18
use Symfony\Component\HttpFoundation\Request;
19
use Symfony\Component\HttpFoundation\RequestStack;
20
use Symfony\Component\HttpFoundation\Response;
21
use Symfony\Component\HttpKernel\Exception\UnsupportedMediaTypeHttpException;
22
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
23
use Symfony\Component\Templating\EngineInterface;
24
use Symfony\Component\Templating\TemplateReferenceInterface;
25
use Twig\Environment;
26
27
/**
28
 * View may be used in controllers to build up a response in a format agnostic way
29
 * The View class takes care of encoding your data in json, xml, or renders a
30
 * template for html via the Serializer component.
31
 *
32
 * @author Jordi Boggiano <[email protected]>
33
 * @author Lukas K. Smith <[email protected]>
34
 *
35
 * @final since 2.8
36
 */
37
class ViewHandler implements ConfigurableViewHandlerInterface
38
{
39
    /**
40
     * Key format, value a callable that returns a Response instance.
41
     *
42
     * @var array
43
     */
44
    protected $customHandlers = [];
45
46
    /**
47
     * The supported formats as keys and if the given formats
48
     * uses templating is denoted by a true value.
49
     *
50
     * @var array
51
     */
52
    protected $formats;
53
54
    /**
55
     * @var int
56
     */
57
    protected $failedValidationCode;
58
59
    /**
60
     * @var int
61
     */
62
    protected $emptyContentCode;
63
64
    /**
65
     * @var bool
66
     */
67
    protected $serializeNull;
68
69
    /**
70
     * If to force a redirect for the given key format,
71
     * with value being the status code to use.
72
     *
73
     * @var array<string,int>
74
     */
75
    protected $forceRedirects;
76
77
    /**
78
     * @var string|null
79
     */
80
    protected $defaultEngine;
81
82
    /**
83
     * @var array
84
     */
85
    protected $exclusionStrategyGroups = [];
86
87
    /**
88
     * @var string
89
     */
90
    protected $exclusionStrategyVersion;
91
92
    /**
93
     * @var bool
94
     */
95
    protected $serializeNullStrategy;
96
97
    private $urlGenerator;
98
    private $serializer;
99
    private $templating;
100
    private $requestStack;
101
102
    private $options;
103
104
    /**
105
     * @param EngineInterface|Environment $templating The configured templating engine
106
     */
107 87
    public function __construct(
108
        UrlGeneratorInterface $urlGenerator,
109
        Serializer $serializer,
110
        $templating,
111
        RequestStack $requestStack,
112
        array $formats = null,
113
        int $failedValidationCode = Response::HTTP_BAD_REQUEST,
114
        int $emptyContentCode = Response::HTTP_NO_CONTENT,
115
        bool $serializeNull = false,
116
        array $forceRedirects = null,
117
        ?string $defaultEngine = 'twig',
118
        array $options = []
119
    ) {
120 87
        if (11 >= func_num_args() || func_get_arg(11)) {
121 14
            @trigger_error(sprintf('The constructor of the %s class is deprecated, use the static create() factory method instead.', __CLASS__), E_USER_DEPRECATED);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
122
        }
123
124 87
        if (null !== $templating && !$templating instanceof EngineInterface && !$templating instanceof Environment) {
125
            throw new \TypeError(sprintf('If provided, the templating engine must be an instance of %s or %s, but %s was given.', EngineInterface::class, Environment::class, get_class($templating)));
0 ignored issues
show
Unused Code introduced by
The call to TypeError::__construct() has too many arguments starting with sprintf('If provided, th...get_class($templating)).

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
126
        }
127
128 87
        $this->urlGenerator = $urlGenerator;
129 87
        $this->serializer = $serializer;
130 87
        $this->templating = $templating;
131 87
        $this->requestStack = $requestStack;
132 87
        $this->formats = (array) $formats;
133 87
        $this->failedValidationCode = $failedValidationCode;
134 87
        $this->emptyContentCode = $emptyContentCode;
135 87
        $this->serializeNull = $serializeNull;
136 87
        $this->forceRedirects = (array) $forceRedirects;
137 87
        $this->defaultEngine = $defaultEngine;
138 87
        $this->options = $options + [
139 87
            'exclusionStrategyGroups' => [],
140
            'exclusionStrategyVersion' => null,
141
            'serializeNullStrategy' => null,
142
            ];
143 87
        $this->reset();
144 87
    }
145
146 51
    public static function create(
147
        UrlGeneratorInterface $urlGenerator,
148
        Serializer $serializer,
149
        RequestStack $requestStack,
150
        array $formats = null,
151
        int $failedValidationCode = Response::HTTP_BAD_REQUEST,
152
        int $emptyContentCode = Response::HTTP_NO_CONTENT,
153
        bool $serializeNull = false,
154
        array $options = []
155
    ): self {
156 51
        return new self($urlGenerator, $serializer, null, $requestStack, $formats, $failedValidationCode, $emptyContentCode, $serializeNull, [], 'twig', $options, false);
157
    }
158
159
    /**
160
     * @param string[]|string $groups
161
     */
162 1
    public function setExclusionStrategyGroups($groups)
163
    {
164 1
        $this->exclusionStrategyGroups = (array) $groups;
165 1
    }
166
167
    /**
168
     * @param string $version
169
     */
170 8
    public function setExclusionStrategyVersion($version)
171
    {
172 8
        $this->exclusionStrategyVersion = $version;
173 8
    }
174
175
    /**
176
     * @param bool $isEnabled
177
     */
178 3
    public function setSerializeNullStrategy($isEnabled)
179
    {
180 3
        $this->serializeNullStrategy = $isEnabled;
181 3
    }
182
183
    /**
184
     * {@inheritdoc}
185
     */
186 49
    public function supports($format)
187
    {
188 49
        return isset($this->customHandlers[$format]) || isset($this->formats[$format]);
189
    }
190
191
    /**
192
     * Registers a custom handler.
193
     *
194
     * The handler must have the following signature: handler(ViewHandler $viewHandler, View $view, Request $request, $format)
195
     * It can use the public methods of this class to retrieve the needed data and return a
196
     * Response object ready to be sent.
197
     *
198
     * @param string   $format
199
     * @param callable $callable
200
     *
201
     * @throws \InvalidArgumentException
202
     */
203 15
    public function registerHandler($format, $callable)
204
    {
205 15
        if (!is_callable($callable)) {
206 1
            throw new \InvalidArgumentException('Registered view callback must be callable.');
207
        }
208
209 14
        $this->customHandlers[$format] = $callable;
210 14
    }
211
212
    /**
213
     * Gets a response HTTP status code from a View instance.
214
     *
215
     * By default it will return 200. However if there is a FormInterface stored for
216
     * the key 'form' in the View's data it will return the failed_validation
217
     * configuration if the form instance has errors.
218
     *
219
     * @param string|false|null
220
     *
221
     * @return int HTTP status code
222
     */
223 61
    protected function getStatusCode(View $view, $content = null)
224
    {
225 61
        $form = $this->getFormFromView($view);
226
227 61
        if ($form && $form->isSubmitted() && !$form->isValid()) {
228 8
            return $this->failedValidationCode;
229
        }
230
231 53
        $statusCode = $view->getStatusCode();
232 53
        if (null !== $statusCode) {
233 17
            return $statusCode;
234
        }
235
236 36
        return null !== $content ? Response::HTTP_OK : $this->emptyContentCode;
237
    }
238
239
    /**
240
     * @deprecated since 2.8
241
     *
242
     * @param string $format
243
     *
244
     * @return bool
245
     */
246 59
    public function isFormatTemplating($format)
247
    {
248 59
        if (1 === func_num_args() || func_get_arg(1)) {
249 1
            @trigger_error(sprintf('The %s() method is deprecated since FOSRestBundle 2.8.', __METHOD__), E_USER_DEPRECATED);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
250
        }
251
252 59
        return !empty($this->formats[$format]);
253
    }
254
255
    /**
256
     * @return Context
257
     */
258 42
    protected function getSerializationContext(View $view)
259
    {
260 42
        $context = $view->getContext();
261
262 42
        $groups = $context->getGroups();
263 42
        if (empty($groups) && $this->exclusionStrategyGroups) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->exclusionStrategyGroups of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
264 1
            $context->setGroups($this->exclusionStrategyGroups);
265
        }
266
267 42
        if (null === $context->getVersion() && $this->exclusionStrategyVersion) {
268 8
            $context->setVersion($this->exclusionStrategyVersion);
269
        }
270
271 42
        if (null === $context->getSerializeNull() && null !== $this->serializeNullStrategy) {
272 21
            $context->setSerializeNull($this->serializeNullStrategy);
273
        }
274
275 42
        if (null !== $view->getStatusCode() && !$context->hasAttribute('status_code')) {
276 11
            $context->setAttribute('status_code', $view->getStatusCode());
277
        }
278
279 42
        return $context;
280
    }
281
282
    /**
283
     * Handles a request with the proper handler.
284
     *
285
     * Decides on which handler to use based on the request format.
286
     *
287
     * @throws UnsupportedMediaTypeHttpException
288
     *
289
     * @return Response
290
     */
291 45
    public function handle(View $view, Request $request = null)
292
    {
293 45
        if (null === $request) {
294 10
            $request = $this->requestStack->getCurrentRequest();
295
        }
296
297 45
        $format = $view->getFormat() ?: $request->getRequestFormat();
0 ignored issues
show
Bug introduced by
It seems like $request is not always an object, but can also be of type null. Maybe add an additional type check?

If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe:

function someFunction(A $objectMaybe = null)
{
    if ($objectMaybe instanceof A) {
        $objectMaybe->doSomething();
    }
}
Loading history...
298
299 45
        if (!$this->supports($format)) {
300 1
            $msg = "Format '$format' not supported, handler must be implemented";
301
302 1
            throw new UnsupportedMediaTypeHttpException($msg);
303
        }
304
305 44
        if (isset($this->customHandlers[$format])) {
306 10
            return call_user_func($this->customHandlers[$format], $this, $view, $request, $format);
307
        }
308
309 34
        return $this->createResponse($view, $request, $format);
0 ignored issues
show
Bug introduced by
It seems like $request can be null; however, createResponse() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
310
    }
311
312
    /**
313
     * @param string $location
314
     * @param string $format
315
     *
316
     * @return Response
317
     */
318 9
    public function createRedirectResponse(View $view, $location, $format)
319
    {
320 9
        $content = null;
321 9
        if ((Response::HTTP_CREATED === $view->getStatusCode() || Response::HTTP_ACCEPTED === $view->getStatusCode()) && null !== $view->getData()) {
322 1
            $response = $this->initResponse($view, $format);
323
        } else {
324 8
            $response = $view->getResponse();
325 8
            if ('html' === $format && isset($this->forceRedirects[$format])) {
326 2
                $redirect = new RedirectResponse($location);
327 2
                $content = $redirect->getContent();
328 2
                $response->setContent($content);
329
            }
330
        }
331
332 9
        $code = isset($this->forceRedirects[$format])
333 9
            ? $this->forceRedirects[$format] : $this->getStatusCode($view, $content);
334
335 9
        $response->setStatusCode($code);
336 9
        $response->headers->set('Location', $location);
337
338 9
        return $response;
339
    }
340
341
    /**
342
     * @deprecated since 2.8
343
     *
344
     * @param string $format
345
     *
346
     * @return string
347
     */
348 12
    public function renderTemplate(View $view, $format)
349
    {
350 12
        if (2 === func_num_args() || func_get_arg(2)) {
351 1
            @trigger_error(sprintf('The %s() method is deprecated since FOSRestBundle 2.8.', __METHOD__), E_USER_DEPRECATED);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
352
        }
353
354 12
        if (null === $this->templating) {
355 1
            throw new \LogicException(sprintf('An instance of %s or %s must be injected in %s to render templates.', EngineInterface::class, Environment::class, __CLASS__));
356
        }
357
358 11
        $data = $this->prepareTemplateParameters($view, false);
0 ignored issues
show
Deprecated Code introduced by
The method FOS\RestBundle\View\View...areTemplateParameters() has been deprecated with message: since 2.8

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...
359
360 11
        $template = $view->getTemplate(false);
0 ignored issues
show
Deprecated Code introduced by
The method FOS\RestBundle\View\View::getTemplate() has been deprecated with message: since 2.8

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...
361 11
        if ($template instanceof TemplateReferenceInterface) {
362
            if (null === $template->get('format')) {
363
                $template->set('format', $format);
364
            }
365
366
            if (null === $template->get('engine')) {
367
                $engine = $view->getEngine() ?: $this->defaultEngine;
0 ignored issues
show
Deprecated Code introduced by
The method FOS\RestBundle\View\View::getEngine() has been deprecated with message: since 2.8

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...
368
                $template->set('engine', $engine);
369
            }
370
        }
371
372 11
        return $this->templating->render($template, $data);
0 ignored issues
show
Bug introduced by
It seems like $template defined by $view->getTemplate(false) on line 360 can also be of type null; however, Symfony\Component\Templa...gineInterface::render() does only seem to accept string|object<Symfony\Co...lateReferenceInterface>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
Bug introduced by
It seems like $template defined by $view->getTemplate(false) on line 360 can also be of type null; however, Twig\Environment::render() does only seem to accept string|object<Twig\TemplateWrapper>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
373
    }
374
375
    /**
376
     * @deprecated since 2.8
377
     *
378
     * @return array
379
     */
380 18
    public function prepareTemplateParameters(View $view)
381
    {
382 18
        if (1 === func_num_args() || func_get_arg(1)) {
383 7
            @trigger_error(sprintf('The %s() method is deprecated since FOSRestBundle 2.8.', __METHOD__), E_USER_DEPRECATED);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
384
        }
385
386 18
        $data = $view->getData();
387
388 18
        if ($data instanceof FormInterface) {
389 2
            $data = [$view->getTemplateVar(false) => $data->getData(), 'form' => $data];
0 ignored issues
show
Deprecated Code introduced by
The method FOS\RestBundle\View\View::getTemplateVar() has been deprecated with message: since 2.8

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...
390 16
        } elseif (empty($data) || !is_array($data) || is_numeric((key($data)))) {
391 10
            $data = [$view->getTemplateVar(false) => $data];
0 ignored issues
show
Deprecated Code introduced by
The method FOS\RestBundle\View\View::getTemplateVar() has been deprecated with message: since 2.8

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...
392
        }
393
394 18
        if (isset($data['form']) && $data['form'] instanceof FormInterface) {
395 2
            $data['form'] = $data['form']->createView();
396
        }
397
398 18
        $templateData = $view->getTemplateData(false);
0 ignored issues
show
Deprecated Code introduced by
The method FOS\RestBundle\View\View::getTemplateData() has been deprecated with message: since 2.8

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...
399 18
        if (is_callable($templateData)) {
400 2
            $templateData = call_user_func($templateData, $this, $view);
401
        }
402
403 18
        return array_merge($data, $templateData);
404
    }
405
406
    /**
407
     * @param string $format
408
     *
409
     * @return Response
410
     */
411 64
    public function createResponse(View $view, Request $request, $format)
412
    {
413 64
        $route = $view->getRoute();
414
415 64
        $location = $route
416 3
            ? $this->urlGenerator->generate($route, (array) $view->getRouteParameters(), UrlGeneratorInterface::ABSOLUTE_URL)
417 64
            : $view->getLocation();
418
419 64
        if ($location) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $location of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
420 9
            return $this->createRedirectResponse($view, $location, $format);
421
        }
422
423 55
        $response = $this->initResponse($view, $format);
424
425 55
        if (!$response->headers->has('Content-Type')) {
426 55
            $mimeType = $request->attributes->get('media_type');
427 55
            if (null === $mimeType) {
428 48
                $mimeType = $request->getMimeType($format);
429
            }
430
431 55
            $response->headers->set('Content-Type', $mimeType);
432
        }
433
434 55
        return $response;
435
    }
436
437
    /**
438
     * @param string $format
439
     *
440
     * @return Response
441
     */
442 56
    private function initResponse(View $view, $format)
443
    {
444 56
        $content = null;
445 56
        if ($this->isFormatTemplating($format, false)) {
0 ignored issues
show
Deprecated Code introduced by
The method FOS\RestBundle\View\View...r::isFormatTemplating() has been deprecated with message: since 2.8

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...
446 11
            $content = $this->renderTemplate($view, $format, false);
0 ignored issues
show
Deprecated Code introduced by
The method FOS\RestBundle\View\ViewHandler::renderTemplate() has been deprecated with message: since 2.8

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...
447 45
        } elseif ($this->serializeNull || null !== $view->getData()) {
448 39
            $data = $this->getDataFromView($view);
449
450 39
            if ($data instanceof FormInterface && $data->isSubmitted() && !$data->isValid()) {
451 8
                $view->getContext()->setAttribute('status_code', $this->failedValidationCode);
452
            }
453
454 39
            $context = $this->getSerializationContext($view);
455 39
            $context->setAttribute('template_data', $view->getTemplateData(false));
0 ignored issues
show
Deprecated Code introduced by
The method FOS\RestBundle\View\View::getTemplateData() has been deprecated with message: since 2.8

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...
456
457 39
            $content = $this->serializer->serialize($data, $format, $context);
458
        }
459
460 56
        $response = $view->getResponse();
461 56
        $response->setStatusCode($this->getStatusCode($view, $content));
462
463 56
        if (null !== $content) {
464 45
            $response->setContent($content);
465
        }
466
467 56
        return $response;
468
    }
469
470
    /**
471
     * @return bool|FormInterface
472
     */
473 61
    protected function getFormFromView(View $view)
474
    {
475 61
        $data = $view->getData();
476
477 61
        if ($data instanceof FormInterface) {
478 7
            return $data;
479
        }
480
481 54
        if (is_array($data) && isset($data['form']) && $data['form'] instanceof FormInterface) {
482 5
            return $data['form'];
483
        }
484
485 49
        return false;
486
    }
487
488 39
    private function getDataFromView(View $view)
489
    {
490 39
        $form = $this->getFormFromView($view);
491
492 39
        if (false === $form) {
493 28
            return $view->getData();
494
        }
495
496 11
        return $form;
497
    }
498
499 87
    public function reset()
500
    {
501 87
        $this->exclusionStrategyGroups = $this->options['exclusionStrategyGroups'];
502 87
        $this->exclusionStrategyVersion = $this->options['exclusionStrategyVersion'];
503 87
        $this->serializeNullStrategy = $this->options['serializeNullStrategy'];
504 87
    }
505
}
506