JsonSerializer::getHttpHeader()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
    namespace rAPId\Data\Serialization;
4
5
    class JsonSerializer implements Serializer
6
    {
7
        public static function serialize($data) {
8
            return self::convert_to_json($data);
9
        }
10
11
        /**
12
         * Deserialize a json/xml string to an array
13
         *
14
         * @param string $xml_string
15
         *
16
         * @return mixed
17
         */
18
        public static function deserialize($xml_string) {
19
20
            return json_decode($xml_string, true);
21
        }
22
23
        /**
24
         * Get the correct HTTP header for the given serialized output
25
         *
26
         * @return string
27
         */
28
        public static function getHttpHeader() {
29
            return 'Content-Type: application/json; charset=utf-8';
30
        }
31
32
        /**
33
         * @param mixed $data
34
         *
35
         * @return string
36
         */
37
        private static function convert_to_json($data) {
38
            if (is_object($data) && !is_a($data, \JsonSerializable::class)) {
39
                $data = get_object_as_array($data);
40
            }
41
42
            return json_encode($data, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
43
        }
44
45
    }