HtmlSerializer::active()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Ntb\RestAPI;
4
5
/**
6
 * Serializer for html.
7
 * @author Christian Blank <[email protected]>
8
 */
9
class HtmlSerializer extends \ViewableData implements IRestSerializer {
10
11
    /**
12
     * @config
13
     */
14
    private static $is_active = true;
15
16
    /**
17
     * The content type
18
     * @var string
19
     */
20
    private $contentType = "text/html";
21
22
    /**
23
     * The given data will be serialized into an html string using a Silverstripe template.
24
     *
25
     * @param array $data
26
     * @return string an html string
0 ignored issues
show
Documentation introduced by
Should the return type not be \HTMLText?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
27
     */
28
    public function serialize($data) {
29
        $list = $this->recursive($data, 1);
30
        return $this->renderWith(['Result', 'Controller'], ['Data' => \ArrayList::create($list)]);
31
    }
32
33
    public function contentType() {
34
        return $this->contentType;
35
    }
36
37
    private function recursive($data, $level) {
38
        $list = [];
39
        if(is_array($data)) {
40
            foreach ($data as $key => $value) {
41
                if(is_array($value)) {
42
                    $list[] = \ArrayData::create(['Key' => $key, 'Value' => '', 'Heading' => true, 'Level' => $level]);
43
                    $list = array_merge($list, $this->recursive($value, $level+1));
44
                } else {
45
                    $list[] = \ArrayData::create(['Key' => $key, 'Value' => $value, 'Level' => $level]);
46
                }
47
            }
48
        }
49
        return $list;
50
    }
51
52
    /**
53
     * Indicates if the serializer is active.
54
     * Serializers can be deactivated to use another implementation for the same mime type.
55
     *
56
     * @return boolean
57
     */
58
    public function active() {
59
        return $this->config()->get('is_active');
60
    }
61
}
62