YamlSerializationVisitor::encode()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2.5

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 2
cts 4
cp 0.5
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 1
crap 2.5
1
<?php
2
3
/*
4
 * This file is part of the Ivory Serializer package.
5
 *
6
 * (c) Eric GELOEN <[email protected]>
7
 *
8
 * For the full copyright and license information, please read the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Ivory\Serializer\Visitor\Yaml;
13
14
use Ivory\Serializer\Accessor\AccessorInterface;
15
use Ivory\Serializer\Visitor\AbstractSerializationVisitor;
16
use Symfony\Component\Yaml\Yaml;
17
18
/**
19
 * @author GeLo <[email protected]>
20
 */
21
class YamlSerializationVisitor extends AbstractSerializationVisitor
22
{
23
    /**
24
     * @var int
25
     */
26
    private $inline;
27
28
    /**
29
     * @var int
30
     */
31
    private $indent;
32
33
    /**
34
     * @var int
35
     */
36
    private $options;
37
38
    /**
39
     * @param AccessorInterface $accessor
40
     * @param int               $inline
41
     * @param int               $indent
42
     * @param int               $options
43
     */
44 1492
    public function __construct(AccessorInterface $accessor, $inline = 2, $indent = 4, $options = 0)
45
    {
46 1492
        parent::__construct($accessor);
47
48 1492
        $this->inline = $inline;
49 1492
        $this->indent = $indent;
50 1492
        $this->options = $options;
51 1492
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56 220
    protected function encode($data)
57
    {
58
        try {
59 220
            return Yaml::dump($data, $this->inline, $this->indent, $this->options);
60
        } catch (\Exception $e) {
61
            throw new \InvalidArgumentException('Unable to serialize data.', 0, $e);
62
        }
63
    }
64
}
65