LuaConstraintViolationHandler::serializeList()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
c 0
b 0
f 0
ccs 0
cts 8
cp 0
rs 9.4285
nc 1
cc 1
eloc 6
nop 4
crap 2
1
<?php
2
3
namespace Vlaswinkel\Lua\JMS;
4
5
use JMS\Serializer\Context;
6
use JMS\Serializer\GraphNavigator;
7
use JMS\Serializer\Handler\SubscribingHandlerInterface;
8
use Symfony\Component\Validator\ConstraintViolation;
9
use Symfony\Component\Validator\ConstraintViolationList;
10
11
/**
12
 * Class LuaConstraintViolationHandler
13
 *
14
 * @see     https://github.com/schmittjoh/serializer/blob/1.1.0/src/JMS/Serializer/Handler/ConstraintViolationHandler.php
15
 *
16
 * @author  Johannes M. Schmitt <[email protected]>
17
 * @author  Koen Vlaswinkel <[email protected]>
18
 * @package Vlaswinkel\Lua\JMS
19
 */
20
class LuaConstraintViolationHandler implements SubscribingHandlerInterface {
21
    public static function getSubscribingMethods() {
22
        $methods = [];
23
        $types   = [
24
            'Symfony\Component\Validator\ConstraintViolationList' => 'serializeList',
25
            'Symfony\Component\Validator\ConstraintViolation'     => 'serializeViolation',
26
        ];
27 View Code Duplication
        foreach ($types as $type => $method) {
28
            $methods[] = [
29
                'direction' => GraphNavigator::DIRECTION_SERIALIZATION,
30
                'type'      => $type,
31
                'format'    => 'lua',
32
                'method'    => $method,
33
            ];
34
        }
35
        return $methods;
36
    }
37
38
    public function serializeList(
39
        LuaSerializationVisitor $visitor,
40
        ConstraintViolationList $list,
41
        array $type,
42
        Context $context
43
    ) {
44
        return $visitor->visitArray(iterator_to_array($list), $type, $context);
45
    }
46
47
    public function serializeViolation(
48
        LuaSerializationVisitor $visitor,
49
        ConstraintViolation $violation,
50
        array $type = null
51
    ) {
52
        $data = [
53
            'property_path' => $violation->getPropertyPath(),
54
            'message'       => $violation->getMessage(),
55
        ];
56
        if (null === $visitor->getRoot()) {
57
            $visitor->setRoot($data);
58
        }
59
        return $data;
60
    }
61
}