Completed
Push — master ( a7faa2...c7788e )
by Christian
9s
created

HtmlSerializer::active()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
/**
4
 * Serializer for html.
5
 * @author Christian Blank <[email protected]>
6
 */
7
class HtmlSerializer extends ViewableData implements IRestSerializer {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
8
9
    /**
10
     * @config
11
     */
12
    private static $is_active = true;
0 ignored issues
show
Unused Code introduced by
The property $is_active is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
13
14
    /**
15
     * The content type
16
     * @var string
17
     */
18
    private $contentType = "text/html";
19
20
    /**
21
     * The given data will be serialized into an html string using a Silverstripe template.
22
     *
23
     * @param array $data
24
     * @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...
25
     */
26
    public function serialize($data) {
27
        $list = $this->recursive($data, 1);
28
        return $this->renderWith(['Result', 'Controller'], ['Data' => ArrayList::create($list)]);
29
    }
30
31
    public function contentType() {
32
        return $this->contentType;
33
    }
34
35
    private function recursive($data, $level) {
36
        $list = [];
37
        if(is_array($data)) {
38
            foreach ($data as $key => $value) {
39
                if(is_array($value)) {
40
                    $list[] = ArrayData::create(['Key' => $key, 'Value' => '', 'Heading' => true, 'Level' => $level]);
41
                    $list = array_merge($list, $this->recursive($value, $level+1));
42
                } else {
43
                    $list[] = ArrayData::create(['Key' => $key, 'Value' => $value, 'Level' => $level]);
44
                }
45
            }
46
        }
47
        return $list;
48
    }
49
50
    /**
51
     * Indicates if the serializer is active.
52
     * Serializers can be deactivated to use another implementation for the same mime type.
53
     *
54
     * @return boolean
0 ignored issues
show
Documentation introduced by
Should the return type not be array|integer|double|string|boolean?

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...
55
     */
56
    public function active() {
57
        return $this->config()->get('is_active');
58
    }
59
}
60