Completed
Pull Request — master (#1)
by Billie
02:33
created

YamlDecode::contextToOptions()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 26
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 26
rs 8.5806
cc 4
eloc 15
nc 4
nop 1
1
<?php
2
3
namespace Fitbug\SymfonySerializer\YamlEncoderDecoder;
4
5
use Symfony\Component\Serializer\Encoder\DecoderInterface;
6
use Symfony\Component\Serializer\Exception\UnexpectedValueException;
7
use Symfony\Component\Yaml\Yaml;
8
9
class YamlDecode implements DecoderInterface
10
{
11
    const OPTION_EXCEPTION_ON_INVALID_TYPE = 'yaml_decode_exception_on_invalid_type';
12
    const OPTION_OBJECT                    = 'yaml_decode_object';
13
    const OPTION_OBJECT_FOR_MAP            = 'yaml_decode_object_for_map';
14
    const OPTION_DATE_TIME                 = 'yaml_decode_date_time';
15
    const SUPPORTED_ENCODING_YAML          = "yaml";
16
    /**
17
     * @var bool
18
     */
19
    private $exceptionOnInvalidType;
20
    /**
21
     * @var bool
22
     */
23
    private $object;
24
    /**
25
     * @var bool
26
     */
27
    private $objectForMap;
28
    /**
29
     * @var bool
30
     */
31
    private $dateTime;
32
33
    /**
34
     * Constructs a new YamlDecode instance.
35
     *
36
     * @param bool $exceptionOnInvalidType
37
     * @param bool $object
38
     * @param bool $objectForMap
39
     * @param bool $dateTime
40
     */
41
    public function __construct(
42
        $exceptionOnInvalidType = false,
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $exceptionOnInvalidType exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
43
        $object = false,
44
        $objectForMap = false,
45
        $dateTime = false
46
    ) {
47
        $this->exceptionOnInvalidType = $exceptionOnInvalidType;
48
        $this->object                 = $object;
49
        $this->objectForMap           = $objectForMap;
50
        $this->dateTime               = $dateTime;
51
    }
52
53
    /**
54
     * Decodes a string into PHP data.
55
     *
56
     * @param string $data    Data to decode
57
     * @param string $format  Format name
58
     * @param array  $context options that decoders have access to
59
     *
60
     * The format parameter specifies which format the data is in; valid values
61
     * depend on the specific implementation. The only format we support is 'yaml'
62
     *
63
     * @return mixed
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use array|string|\stdClass.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
64
     *
65
     * @throws UnexpectedValueException
66
     */
67
    public function decode($data, $format, array $context = [])
68
    {
69
        $context = $this->resolveContext($context);
70
        $options = $this->contextToOptions($context);
71
72
        return Yaml::parse($data, $options);
73
    }
74
75
    /**
76
     * Checks whether the deserializer can decode from given format.
77
     *
78
     * We only support yaml.
79
     *
80
     * @param string $format format name
81
     *
82
     * @return bool
83
     */
84
    public function supportsDecoding($format)
85
    {
86
        return $format == self::SUPPORTED_ENCODING_YAML;
87
    }
88
89
    /**
90
     * Merges the default options of the Yaml Decoder with the passed context.
91
     *
92
     * @param array $context
93
     *
94
     * @return array
95
     */
96
    private function resolveContext(array $context)
97
    {
98
        $defaultOptions = [
99
            self::OPTION_EXCEPTION_ON_INVALID_TYPE => $this->exceptionOnInvalidType,
100
            self::OPTION_OBJECT                    => $this->object,
101
            self::OPTION_OBJECT_FOR_MAP            => $this->objectForMap,
102
            self::OPTION_DATE_TIME                 => $this->dateTime,
103
        ];
104
105
        return array_merge($defaultOptions, $context);
106
    }
107
108
    /**
109
     * Convert the context to options understood by the parser
110
     *
111
     * @param array $options
112
     *
113
     * @return int
114
     */
115
    private function contextToOptions(array $options)
116
    {
117
        $optionToBitMap = [
118
            self::OPTION_EXCEPTION_ON_INVALID_TYPE => "PARSE_EXCEPTION_ON_INVALID_TYPE",
119
            self::OPTION_OBJECT                    => "PARSE_OBJECT",
120
            self::OPTION_OBJECT_FOR_MAP            => "PARSE_OBJECT_FOR_MAP",
121
            self::OPTION_DATE_TIME                 => "PARSE_DATETIME",
122
        ];
123
124
        $yamlClass       = "Symfony\\Component\\Yaml\\Yaml";
125
        $bitMaskedOption = 0;
126
127
        foreach ($optionToBitMap as $option => $bitMastConstant) {
128
129
            $fullConstant = $yamlClass . "::" . $bitMastConstant;
130
            if (defined($fullConstant)) {
131
                $bitMask = constant($fullConstant);
132
133
                if ($options[ $option ]) {
134
                    $bitMaskedOption = $bitMaskedOption | $bitMask;
135
                }   
136
            }
137
        }
138
139
        return $bitMaskedOption;
140
    }
141
}
142